Skip to content

Instantly share code, notes, and snippets.

@BenoitZugmeyer
Last active May 29, 2016 21:31
Show Gist options
  • Save BenoitZugmeyer/14100c1c9c453c1ff062e85aedacee02 to your computer and use it in GitHub Desktop.
Save BenoitZugmeyer/14100c1c9c453c1ff062e85aedacee02 to your computer and use it in GitHub Desktop.
hyperscript.rs
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