Last active
May 29, 2016 21:31
-
-
Save BenoitZugmeyer/14100c1c9c453c1ff062e85aedacee02 to your computer and use it in GitHub Desktop.
hyperscript.rs
This file contains 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
extern crate kuchiki; | |
#[macro_use] | |
extern crate string_cache; | |
macro_rules! h{ | |
($name:tt) => { | |
h!($name { } =>) | |
}; | |
($name:tt => $($other:expr)*) => { | |
h!($name { } => $($other)*) | |
}; | |
($name:tt { $( $attr_name:tt = $attr_value:expr )* }) => { | |
h!($name { $( $attr_name=$attr_value )* } =>) | |
}; | |
($name:tt { $( $attr_name:tt = $attr_value:expr )* } => $($other:expr)*) => {{ | |
let el = kuchiki::NodeRef::new_element( | |
qualname!("", $name), | |
vec![$( (qualname!("", $attr_name), $attr_value.to_string()) )*] | |
); | |
$(el.append($other);)* | |
el | |
}} | |
} | |
pub fn main() { | |
let node = h!("span"); | |
let node2 = h!("div" { "class"="foo" } => | |
h!("div" => h!("span")) | |
h!("span" { "class"="bonjour" }) | |
node | |
); | |
println!("{}", node2.to_string()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment