mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-24 07:44:10 +00:00
An attempt at a macro to support HTML literals
This commit is contained in:
parent
96fdad2fb7
commit
d704fc9196
68
src/test/run-pass/html-literals.rs
Normal file
68
src/test/run-pass/html-literals.rs
Normal file
@ -0,0 +1,68 @@
|
||||
// A test of the macro system. Can we do HTML literals?
|
||||
|
||||
// xfail-pretty
|
||||
// xfail-test
|
||||
|
||||
macro_rules! html {
|
||||
{ $($body:tt)* } => {
|
||||
let builder = HTMLBuilder();
|
||||
build_html!{builder := $($body)*};
|
||||
builder.getDoc()
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! build_html {
|
||||
{ $builder:expr := </$tag:ident> $($rest:tt)* } => {
|
||||
$builder.endTag(stringify!($tag));
|
||||
build_html!{ $builder := $($rest)* };
|
||||
};
|
||||
|
||||
{ $builder:expr := <$tag:ident> $($rest:tt)* } => {
|
||||
$builder.beginTag(stringify!($tag));
|
||||
build_html!{ $builder := $($rest)* };
|
||||
};
|
||||
|
||||
{ $builder:expr := . $($rest:tt)* } => {
|
||||
$builder.addText(~".");
|
||||
build_html!{ $builder := $($rest)* };
|
||||
};
|
||||
|
||||
{ $builder:expr := $word:ident $($rest:tt)* } => {
|
||||
$builder.addText(stringify!($word));
|
||||
build_html!{ $builder := $($rest)* };
|
||||
};
|
||||
|
||||
{ $builder:expr := } => { }
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
let page = html! {
|
||||
<html>
|
||||
<head><title>This is the title.</title></head>
|
||||
<body>
|
||||
<p>This is some text</p>
|
||||
</body>
|
||||
</html>
|
||||
};
|
||||
|
||||
// When we can do this, we are successful:
|
||||
//
|
||||
//let page = tag(~"html", ~[tag(~"head", ~[...])])
|
||||
|
||||
}
|
||||
|
||||
enum HTMLFragment {
|
||||
}
|
||||
|
||||
struct HTMLBuilder {
|
||||
bar: ();
|
||||
fn getDoc() -> HTMLFragment { fail }
|
||||
fn beginTag(tag: ~str) { }
|
||||
fn endTag(tag: ~str) { }
|
||||
fn addText(test: ~str) { }
|
||||
}
|
||||
|
||||
fn HTMLBuilder() -> HTMLBuilder {
|
||||
HTMLBuilder { bar: () }
|
||||
}
|
Loading…
Reference in New Issue
Block a user