Skip to content

Commit

Permalink
Allow svg and xml templates as well as html.
Browse files Browse the repository at this point in the history
Allow template source files to be named *.rs.svg or *.rs.xml along
with *.rs.html.  The generated template functions will simlilarly be
suffixed _svg, _xml or _html (any template_html will get a template
alias, for backwards compatibility.

Helps #20.
  • Loading branch information
kaj committed Nov 1, 2019
1 parent 2d9f13f commit 2e4173e
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 51 deletions.
53 changes: 28 additions & 25 deletions examples/simple/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ include!(concat!(env!("OUT_DIR"), "/templates.rs"));
use templates::*;

fn main() {
page::page(&mut io::stdout(), "sample page").unwrap();
page::page_html(&mut io::stdout(), "sample page").unwrap();
}

fn r2s<Call>(call: Call) -> String
Expand All @@ -21,78 +21,81 @@ where
#[test]
fn test_hello() {
assert_eq!(
r2s(|o| hello(o)),
r2s(|o| hello_html(o)),
"<h1>Hello World!</h1>\
\n<p>Note: Brackets and @ signs needs to be escaped: { ... }</p>\n"
);
}

#[test]
fn test_hello_args() {
assert_eq!(r2s(|o| hello_args(o, "World")), "<h1>Hello World!</h1>\n");
assert_eq!(
r2s(|o| hello_args_html(o, "World")),
"<h1>Hello World!</h1>\n",
);
}

#[test]
fn test_hello_encodes_args() {
assert_eq!(
r2s(|o| hello_args(o, "encoded < & >")),
r2s(|o| hello_args_html(o, "encoded < & >")),
"<h1>Hello encoded &lt; &amp; &gt;!</h1>\n"
);
}

#[test]
fn test_hello_args_two() {
assert_eq!(
r2s(|o| hello_args_two(o, 56, "prime", false)),
r2s(|o| hello_args_two_html(o, 56, "prime", false)),
"<p class=\"foo\" data-n=\"56\">Is 56 a prime? false!</p>\n"
);
}

#[test]
fn test_hello_args_three() {
assert_eq!(
r2s(|o| hello_args_three(o, 56, &56, &56)),
r2s(|o| hello_args_three_html(o, 56, &56, &56)),
"<p>56 56 56</p>\n"
);
}

#[test]
fn test_if_let_some() {
assert_eq!(
r2s(|o| if_let(o, Some("thing"))),
r2s(|o| if_let_html(o, Some("thing"))),
"<p> The item is thing </p>\n"
)
}
#[test]
fn test_if_let_none() {
assert_eq!(r2s(|o| if_let(o, None)), "<p> Got nothing </p>\n")
assert_eq!(r2s(|o| if_let_html(o, None)), "<p> Got nothing </p>\n")
}

#[test]
fn test_if_let_destructure() {
assert_eq!(
r2s(|o| if_let_destructure(o, &Some((47, 11)))),
r2s(|o| if_let_destructure_html(o, &Some((47, 11)))),
"<p> We have 47 and 11 </p>\n"
)
}

#[test]
fn test_list() {
assert_eq!(
r2s(|o| list(o, &["foo", "bar"])),
r2s(|o| list_html(o, &["foo", "bar"])),
"\n<ul>\n \n <li>foo</li>\n \n <li>bar</li>\n </ul>\n\n"
);
}

#[test]
fn test_list_empty() {
assert_eq!(r2s(|o| list(o, &[])), "\n<p>No items</p>\n\n");
assert_eq!(r2s(|o| list_html(o, &[])), "\n<p>No items</p>\n\n");
}

#[test]
fn test_list_destructure() {
assert_eq!(
r2s(|o| list_destructure(o, &["foo", "bar"])),
r2s(|o| list_destructure_html(o, &["foo", "bar"])),
"<ul>\n \n <li>0: foo</li>\n \n \
<li>1: bar</li>\n </ul>\n"
);
Expand All @@ -101,7 +104,7 @@ fn test_list_destructure() {
#[test]
fn test_list_destructure_2() {
assert_eq!(
r2s(|o| list_destructure_2(o)),
r2s(|o| list_destructure_2_html(o)),
"\n <p>Rasmus is 44 years old.</p>\n\n \
<p>Mike is 36 years old.</p>\n"
);
Expand All @@ -110,7 +113,7 @@ fn test_list_destructure_2() {
#[test]
fn test_uselist() {
assert_eq!(
r2s(|o| uselist(o)),
r2s(|o| uselist_html(o)),
"<h1>Two items</h1>\n\n\
<ul>\n \n <li>foo</li>\n \
\n <li>bar</li>\n </ul>\n\n\n\
Expand All @@ -122,7 +125,7 @@ fn test_uselist() {
#[test]
fn test_hello_utf8() {
assert_eq!(
r2s(|o| hello_utf8(o, "δ", "ε", "δ < ε", "δ &lt; ε")),
r2s(|o| hello_utf8_html(o, "δ", "ε", "δ < ε", "δ &lt; ε")),
"<p>δ &lt; ε</p>\n\
<p>δ &lt; ε</p>\n\
<p>δ &lt; ε</p>\n\
Expand All @@ -133,7 +136,7 @@ fn test_hello_utf8() {
#[test]
fn test_comments() {
assert_eq!(
r2s(|o| comments(o)),
r2s(|o| comments_html(o)),
"<!-- this is a real HTML comment, which gets send to the client -->\n\
<p>This is visible</p>\n\n"
);
Expand Down Expand Up @@ -166,7 +169,7 @@ fn test_hello_fields() {
email: "tom@example.nl",
};
assert_eq!(
r2s(|o| hello_fields(o, &user)),
r2s(|o| hello_fields_html(o, &user)),
"<h1>Hello Tom Puss!</h1>\n<p>Your email is \
tom@example.nl</p>\n"
);
Expand All @@ -179,7 +182,7 @@ fn test_hello_method() {
email: "tom@example.nl",
};
assert_eq!(
r2s(|o| hello_method(o, &user)),
r2s(|o| hello_method_html(o, &user)),
"<h1>Hello Tom Puss!</h1>\n<p>Your email is \
<a href=\"mailto:tom@example.nl\">tom@example.nl</a></p>\n"
);
Expand All @@ -189,15 +192,15 @@ fn test_hello_method() {
fn test_hello_code() {
use templates::Html;
assert_eq!(
r2s(|o| hello_code(o, &"Paragraph:", &Html("<p>Hello.</p>"))),
r2s(|o| hello_code_html(o, &"Paragraph:", &Html("<p>Hello.</p>"))),
"<h2>Paragraph:</h2>\n<p>Hello.</p>\n"
);
}

#[test]
fn test_for_loop() {
assert_eq!(
r2s(|o| for_loop(o, &vec!["Hello", "World"])),
r2s(|o| for_loop_html(o, &vec!["Hello", "World"])),
"<h1>Looped paragraphs</h1>\n\n \
<p>Hello</p>\n\n <p>World</p>\n"
);
Expand All @@ -216,15 +219,15 @@ fn test_for_destructure() {
},
];
assert_eq!(
r2s(|o| for_destructure(o, &users)),
r2s(|o| for_destructure_html(o, &users)),
"<ul><li>Tom Puss</li><li>Heloise Walker</li></ul>\n",
)
}

#[test]
fn test_explicit_formatting() {
assert_eq!(
r2s(|o| explicit_formatting(o, 5.212432234, "one\ntwo")),
r2s(|o| explicit_formatting_html(o, 5.212432234, "one\ntwo")),
"<p>Value 1 is 5.2 (or really 5.212432234),\n\
while value 2 is &quot;one\\ntwo&quot;.</p>\n"
);
Expand All @@ -233,7 +236,7 @@ fn test_explicit_formatting() {
#[test]
fn test_hello_use_templates() {
assert_eq!(
r2s(|o| hello_use_templates(o, &Html("<p>this is foo</p>"))),
r2s(|o| hello_use_templates_html(o, &Html("<p>this is foo</p>"))),
"<h1>Hello World!</h1>\
\n<p>Note: Brackets and @ signs needs to be escaped: { ... }</p>\n\
\n<h2>foo</h2>\n<p>this is foo</p>\n\n"
Expand All @@ -243,7 +246,7 @@ fn test_hello_use_templates() {
#[test]
fn test_page_with_base() {
assert_eq!(
r2s(|o| page::page(o, "World")),
r2s(|o| page::page_html(o, "World")),
"<!doctype html>\
\n<html>\
\n <head><title>Hello World!</title>\
Expand All @@ -262,7 +265,7 @@ fn test_page_with_base() {
#[test]
fn test_page_two() {
assert_eq!(
r2s(|o| page::page_two(o, 2019)),
r2s(|o| page::page_two_html(o, 2019)),
"<!doctype html>\
\n<html lang=\"sv\">\
\n <head>\
Expand Down
8 changes: 4 additions & 4 deletions examples/simple/templates/hello_use_templates.rs.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@use super::hello;
@use super::hello_code;
@use super::hello_html;
@use super::hello_code_html;

@(body: &ToHtml)

@:hello()
@:hello_code(&"foo", body)
@:hello_html()
@:hello_code_html(&"foo", body)
4 changes: 2 additions & 2 deletions examples/simple/templates/page/page.rs.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
@use super::base;
@use super::base_html;

@(who: &str)

@:base(&format!("Hello {}!", who), {
@:base_html(&format!("Hello {}!", who), {
<p>This is page content for @who</p>
}, {
<meta property="og:description" content="A simple example"/>
Expand Down
4 changes: 2 additions & 2 deletions examples/simple/templates/page/page_two.rs.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@use super::base_two;
@use super::base_two_html;

@(year: u16)
@:base_two(&format!("Year {}", year), {
@:base_two_html(&format!("Year {}", year), {
<p>Welcome to this page about the year @year.</p>
},
{
Expand Down
2 changes: 1 addition & 1 deletion examples/simple/templates/uselist.rs.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@use super::list;
@use super::list_html as list;

@()

Expand Down
8 changes: 4 additions & 4 deletions src/Template_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,12 @@ pub mod d_Calling_other_templates {
//! It can be used like this:
//!
//! ```text
//! @use super::header;
//! @use super::header_html;
//!
//! @()
//!
//! <html>
//! @:header("Example")
//! @:header_html("Example")
//! <body>
//! <h1>Example</h1>
//! <p>page content ...</p>
Expand Down Expand Up @@ -242,11 +242,11 @@ pub mod d_Calling_other_templates {
//! And use it like this:
//!
//! ```text
//! @use super::base_page;
//! @use super::base_page_html;
//!
//! @()
//!
//! @:base_page("Example", {
//! @:base_page_html("Example", {
//! <p>page content ...</p>
//! })
//! ```
Expand Down
49 changes: 36 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
//! #[test]
//! fn test_hello() {
//! let mut buf = Vec::new();
//! templates::hello(&mut buf, "World").unwrap();
//! templates::hello_html(&mut buf, "World").unwrap();
//! assert_eq!(buf, b"<h1>Hello World!</h1>\n");
//! }
//! ```
Expand Down Expand Up @@ -103,7 +103,7 @@
//! build = "src/build.rs"
//!
//! [build-dependencies]
//! ructe = { version = "0.6.0", features = ["sass", "mime03"]
//! ructe = { version = "0.6.0", features = ["sass", "mime03"] }
//!
//! [dependencies]
//! mime = "0.3.13"
Expand Down Expand Up @@ -237,6 +237,15 @@ impl Ructe {
/// If indir is a relative path, it should be relative to the main
/// directory of your crate, i.e. the directory containing your
/// `Cargo.toml` file.
///
/// Files with suffix `.rs.html`, `.rs.svg`, or `.rs.xml` are
/// considered templates.
/// A templete file called `template.rs.html`, `template.rs.svg`,
/// etc, will result in a callable function named `template_html`,
/// `template_svg`, etc.
/// The `template_html` function will get a `template` alias for
/// backwards compatibility, but that will be removed in a future
/// release.
pub fn compile_templates<P>(&mut self, indir: P) -> Result<()>
where
P: AsRef<Path>,
Expand Down Expand Up @@ -305,7 +314,6 @@ fn handle_entries(
outdir: &Path,
) -> Result<()> {
println!("cargo:rerun-if-changed={}", indir.display());
let suffix = ".rs.html";
for entry in read_dir(indir)? {
let entry = entry?;
let path = entry.path();
Expand All @@ -325,16 +333,31 @@ fn handle_entries(
writeln!(f, "pub mod {name};\n", name = filename)?;
}
} else if let Some(filename) = entry.file_name().to_str() {
if filename.ends_with(suffix) {
println!("cargo:rerun-if-changed={}", path.display());
let name = &filename[..filename.len() - suffix.len()];
if handle_template(name, &path, outdir)? {
writeln!(
f,
"mod template_{name};\n\
pub use self::template_{name}::{name};\n",
name = name,
)?;
for suffix in &[".rs.html", ".rs.svg", ".rs.xml"] {
if filename.ends_with(suffix) {
println!("cargo:rerun-if-changed={}", path.display());
let prename = &filename[..filename.len() - suffix.len()];
let name =
format!("{}_{}", prename, &suffix[".rs.".len()..]);
if handle_template(&name, &path, outdir)? {
writeln!(
f,
"mod template_{name};\n\
pub use self::template_{name}::{name};\n",
name = name,
)?;
// Backwards compatibility to 0.7.2 and earlier.
if suffix == &".rs.html" {
writeln!(
f,
"#[deprecated(since=\"0.7.4\", \
note=\"please use `{name}` instead\")]\n\
pub use self::{name} as {alias};\n",
alias = prename,
name = name,
)?;
}
}
}
}
}
Expand Down

0 comments on commit 2e4173e

Please sign in to comment.