From 2e4173eead901e2904c7a59d41fee2f8baafb473 Mon Sep 17 00:00:00 2001 From: Rasmus Kaj Date: Fri, 1 Nov 2019 22:37:35 +0100 Subject: [PATCH] Allow svg and xml templates as well as html. 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. --- examples/simple/src/main.rs | 53 ++++++++++--------- .../templates/hello_use_templates.rs.html | 8 +-- examples/simple/templates/page/page.rs.html | 4 +- .../simple/templates/page/page_two.rs.html | 4 +- examples/simple/templates/uselist.rs.html | 2 +- src/Template_syntax.rs | 8 +-- src/lib.rs | 49 ++++++++++++----- 7 files changed, 77 insertions(+), 51 deletions(-) diff --git a/examples/simple/src/main.rs b/examples/simple/src/main.rs index 4182311..46384a0 100644 --- a/examples/simple/src/main.rs +++ b/examples/simple/src/main.rs @@ -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) -> String @@ -21,7 +21,7 @@ where #[test] fn test_hello() { assert_eq!( - r2s(|o| hello(o)), + r2s(|o| hello_html(o)), "

Hello World!

\ \n

Note: Brackets and @ signs needs to be escaped: { ... }

\n" ); @@ -29,13 +29,16 @@ fn test_hello() { #[test] fn test_hello_args() { - assert_eq!(r2s(|o| hello_args(o, "World")), "

Hello World!

\n"); + assert_eq!( + r2s(|o| hello_args_html(o, "World")), + "

Hello World!

\n", + ); } #[test] fn test_hello_encodes_args() { assert_eq!( - r2s(|o| hello_args(o, "encoded < & >")), + r2s(|o| hello_args_html(o, "encoded < & >")), "

Hello encoded < & >!

\n" ); } @@ -43,7 +46,7 @@ fn test_hello_encodes_args() { #[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)), "

Is 56 a prime? false!

\n" ); } @@ -51,7 +54,7 @@ fn test_hello_args_two() { #[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)), "

56 56 56

\n" ); } @@ -59,19 +62,19 @@ fn test_hello_args_three() { #[test] fn test_if_let_some() { assert_eq!( - r2s(|o| if_let(o, Some("thing"))), + r2s(|o| if_let_html(o, Some("thing"))), "

The item is thing

\n" ) } #[test] fn test_if_let_none() { - assert_eq!(r2s(|o| if_let(o, None)), "

Got nothing

\n") + assert_eq!(r2s(|o| if_let_html(o, None)), "

Got nothing

\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)))), "

We have 47 and 11

\n" ) } @@ -79,20 +82,20 @@ fn test_if_let_destructure() { #[test] fn test_list() { assert_eq!( - r2s(|o| list(o, &["foo", "bar"])), + r2s(|o| list_html(o, &["foo", "bar"])), "\n\n\n" ); } #[test] fn test_list_empty() { - assert_eq!(r2s(|o| list(o, &[])), "\n

No items

\n\n"); + assert_eq!(r2s(|o| list_html(o, &[])), "\n

No items

\n\n"); } #[test] fn test_list_destructure() { assert_eq!( - r2s(|o| list_destructure(o, &["foo", "bar"])), + r2s(|o| list_destructure_html(o, &["foo", "bar"])), "\n" ); @@ -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

Rasmus is 44 years old.

\n\n \

Mike is 36 years old.

\n" ); @@ -110,7 +113,7 @@ fn test_list_destructure_2() { #[test] fn test_uselist() { assert_eq!( - r2s(|o| uselist(o)), + r2s(|o| uselist_html(o)), "

Two items

\n\n\ \n\n\n\ @@ -122,7 +125,7 @@ fn test_uselist() { #[test] fn test_hello_utf8() { assert_eq!( - r2s(|o| hello_utf8(o, "δ", "ε", "δ < ε", "δ < ε")), + r2s(|o| hello_utf8_html(o, "δ", "ε", "δ < ε", "δ < ε")), "

δ < ε

\n\

δ < ε

\n\

δ < ε

\n\ @@ -133,7 +136,7 @@ fn test_hello_utf8() { #[test] fn test_comments() { assert_eq!( - r2s(|o| comments(o)), + r2s(|o| comments_html(o)), "\n\

This is visible

\n\n" ); @@ -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)), "

Hello Tom Puss!

\n

Your email is \ tom@example.nl

\n" ); @@ -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)), "

Hello Tom Puss!

\n

Your email is \ tom@example.nl

\n" ); @@ -189,7 +192,7 @@ fn test_hello_method() { fn test_hello_code() { use templates::Html; assert_eq!( - r2s(|o| hello_code(o, &"Paragraph:", &Html("

Hello.

"))), + r2s(|o| hello_code_html(o, &"Paragraph:", &Html("

Hello.

"))), "

Paragraph:

\n

Hello.

\n" ); } @@ -197,7 +200,7 @@ fn test_hello_code() { #[test] fn test_for_loop() { assert_eq!( - r2s(|o| for_loop(o, &vec!["Hello", "World"])), + r2s(|o| for_loop_html(o, &vec!["Hello", "World"])), "

Looped paragraphs

\n\n \

Hello

\n\n

World

\n" ); @@ -216,7 +219,7 @@ fn test_for_destructure() { }, ]; assert_eq!( - r2s(|o| for_destructure(o, &users)), + r2s(|o| for_destructure_html(o, &users)), "\n", ) } @@ -224,7 +227,7 @@ fn test_for_destructure() { #[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")), "

Value 1 is 5.2 (or really 5.212432234),\n\ while value 2 is "one\\ntwo".

\n" ); @@ -233,7 +236,7 @@ fn test_explicit_formatting() { #[test] fn test_hello_use_templates() { assert_eq!( - r2s(|o| hello_use_templates(o, &Html("

this is foo

"))), + r2s(|o| hello_use_templates_html(o, &Html("

this is foo

"))), "

Hello World!

\ \n

Note: Brackets and @ signs needs to be escaped: { ... }

\n\ \n

foo

\n

this is foo

\n\n" @@ -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")), "\ \n\ \n Hello World!\ @@ -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)), "\ \n\ \n \ diff --git a/examples/simple/templates/hello_use_templates.rs.html b/examples/simple/templates/hello_use_templates.rs.html index 9be771b..316500b 100644 --- a/examples/simple/templates/hello_use_templates.rs.html +++ b/examples/simple/templates/hello_use_templates.rs.html @@ -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) diff --git a/examples/simple/templates/page/page.rs.html b/examples/simple/templates/page/page.rs.html index ff57727..601a274 100644 --- a/examples/simple/templates/page/page.rs.html +++ b/examples/simple/templates/page/page.rs.html @@ -1,8 +1,8 @@ -@use super::base; +@use super::base_html; @(who: &str) -@:base(&format!("Hello {}!", who), { +@:base_html(&format!("Hello {}!", who), {

This is page content for @who

}, { diff --git a/examples/simple/templates/page/page_two.rs.html b/examples/simple/templates/page/page_two.rs.html index 1f83408..f379e0c 100644 --- a/examples/simple/templates/page/page_two.rs.html +++ b/examples/simple/templates/page/page_two.rs.html @@ -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), {

Welcome to this page about the year @year.

}, { diff --git a/examples/simple/templates/uselist.rs.html b/examples/simple/templates/uselist.rs.html index 2881519..e2192e4 100644 --- a/examples/simple/templates/uselist.rs.html +++ b/examples/simple/templates/uselist.rs.html @@ -1,4 +1,4 @@ -@use super::list; +@use super::list_html as list; @() diff --git a/src/Template_syntax.rs b/src/Template_syntax.rs index 3df11b5..679beac 100644 --- a/src/Template_syntax.rs +++ b/src/Template_syntax.rs @@ -207,12 +207,12 @@ pub mod d_Calling_other_templates { //! It can be used like this: //! //! ```text - //! @use super::header; + //! @use super::header_html; //! //! @() //! //! - //! @:header("Example") + //! @:header_html("Example") //! //!

Example

//!

page content ...

@@ -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", { //!

page content ...

//! }) //! ``` diff --git a/src/lib.rs b/src/lib.rs index d2f6ebb..41494ec 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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"

Hello World!

\n"); //! } //! ``` @@ -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" @@ -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

(&mut self, indir: P) -> Result<()> where P: AsRef, @@ -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(); @@ -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, + )?; + } + } } } }