Last active
May 22, 2018 09:47
-
-
Save josuigoa/e2fff58cea8105bd50f0c27cf9b87209 to your computer and use it in GitHub Desktop.
Macro to extract the selectors (id, class, tag) from a html file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-main Main | |
-js main.js | |
-extraCls ezkutua,botoia | |
-dce full |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src='https://code.jquery.com/jquery-3.2.1.slim.min.js'></script> | |
<script src='main.js'></script> | |
<link rel='stylesheet' href='style.css' /> | |
</head> | |
<body> | |
<div id='eszena'></div> | |
<textarea id='kode' class='testua'></textarea> | |
<button id='exekutatu' class='botoia'>Hasi</button> | |
<textarea id='erroreak' class='akatsa testua'></textarea> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package; | |
import Selectors; | |
class Main { | |
static function main() { | |
Cls.akatsa.jq().fadeOut(); | |
Id.eszena.jq().addClass(Cls.ezkutua); | |
Id.exekutatu.jq().click(function(_) { | |
trace( 'btn clicked!' ); | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package; | |
import haxe.macro.Context; | |
import haxe.macro.Expr; | |
using StringTools; | |
enum SelType { | |
id; | |
cls; | |
tag; | |
} | |
class SelectorBuilder { | |
static public function build_id() | |
return build(~/id=["']([A-Za-z0-9_-]+)["']/, id); | |
static public function build_cls() | |
return build(~/class=["']([A-Za-z0-9_\s-]+)["']/, cls); | |
static public function build_tag() | |
return build(~/<([A-Za-z0-9]+)/, tag); | |
static public function build(ereg:EReg, type:SelType):Array<Field> { | |
var path = Context.definedValue("indexPath"); | |
if( path == null ) path = "index.html"; | |
var extra = null; | |
var extra_def_name = switch type { | |
case id: 'extraId'; | |
case cls: 'extraCls'; | |
case tag: 'extraTag'; | |
} | |
var extra_string = Context.definedValue(extra_def_name); | |
if( extra_string != null ) | |
extra = [for (e in extra_string.split(',')) if (e.trim() != '') e.trim()]; | |
var file_content = get_fitx_edukia(path); | |
var elements = [], el, cls_el; | |
while( ereg.match(file_content) ) { | |
el = ereg.matched(1); | |
if (type == cls) { | |
cls_el = el.split(' '); | |
for (ce in cls_el) { | |
elements.remove(ce); | |
elements.push(ce); | |
} | |
} else { | |
elements.remove(el); | |
elements.push(el); | |
} | |
file_content = ereg.matchedRight(); | |
} | |
if (extra != null) { | |
for (e in extra) { | |
if (StringTools.trim(e) == '') continue; | |
elements.remove(e); | |
elements.push(e); | |
} | |
} | |
return Context.getBuildFields().concat(build_fields(elements, type)); | |
} | |
static function build_fields(_field_names:Array<String>, type:SelType):Array<Field> { | |
var fields = []; | |
var pos = Context.currentPos(); | |
var var_complex_type = switch type { | |
case id: macro :Selectors.IdJQ; | |
case cls: macro :Selectors.ClsJQ; | |
case tag: macro :Selectors.TagJQ; | |
} | |
for (f in _field_names) { | |
fields.push({ | |
name : AE.karak_baliogabeak.replace(f, '_'), | |
access : [APublic, AStatic, AInline], | |
meta : [{ name : ':dce', params : [], pos : pos}], | |
pos : pos, | |
kind : FVar(var_complex_type, macro $v{f}) | |
}); | |
} | |
return fields; | |
} | |
static function get_fitx_edukia(_path:String) { | |
try { | |
var p = Context.resolvePath(_path); | |
return sys.io.File.getContent(p); | |
} catch(e:Dynamic) { | |
Context.error('$_path fitxategia kargatzean errorea: $e', Context.currentPos()); | |
return ''; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package; | |
import js.jquery.Helper.J; | |
typedef Selectors = {} | |
@:build(SelectorBuilder.build_id()) | |
class Id{} | |
@:build(SelectorBuilder.build_cls()) | |
class Cls{} | |
@:build(SelectorBuilder.build_tag()) | |
class Tag{} | |
abstract IdJQ(String) to String from String { | |
inline public function new(id) this = id; | |
/** | |
* @return '#' + this | |
*/ | |
inline public function selector() return '#' + this; | |
/** | |
* @return jQuery('#' + this) | |
*/ | |
inline public function jq() return J(selector()); | |
/** | |
* @return jQuery('#' + this + ' > ' + child_selector) | |
*/ | |
inline public function children(child_selector:String) return J(selector() + ' > $child_selector'); | |
} | |
abstract ClsJQ(String) to String from String { | |
inline public function new(cls) this = cls; | |
/** | |
* @return '.' + this | |
*/ | |
inline public function selector() return '.' + this; | |
/** | |
* @return jQuery('.' + this) | |
*/ | |
inline public function jq() return J(selector()); | |
/** | |
* @return jQuery('.' + this + ' > ' + child_selector) | |
*/ | |
inline public function children(child_selector:String) return J(selector() + ' > $child_selector'); | |
/** | |
* @return jQuery(this + ' ' + extra_selector) | |
*/ | |
inline public function specify(extra_selector:String) return J(selector() + ' $extra_selector'); | |
} | |
abstract TagJQ(String) to String from String { | |
inline public function new(tag) this = tag; | |
/** | |
* @return this | |
*/ | |
inline public function selector() return this; | |
/** | |
* @return jQuery(this) | |
*/ | |
inline public function jq() return J(selector()); | |
/** | |
* @return jQuery(this + '[for="' + id + '"]') | |
*/ | |
inline public function for_id(id:String) return J(selector() + '[for="$id"]'); | |
/** | |
* @return jQuery(this + ' > ' + child_selector) | |
*/ | |
inline public function children(child_selector:String) return J(selector() + ' > $child_selector'); | |
/** | |
* @return jQuery(this + ' ' + extra_selector) | |
*/ | |
inline public function specify(extra_selector:String) return J(selector() + ' $extra_selector'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment