1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
//! This crate allows you to separate your HTML from your Rust code when using [Yew](https://yew.rs).
//!
//! # Usage
//!
//! ## Hello World
//!
//! ```html
//! <div>
//! <p>Hello [name]!</p>
//! </div>
//! ```
//!
//! ```rust
//! # use yew_template::*;
//! # use yew::prelude::*;
//! # fn main() {
//! let html = template_html!("templates/hello.html", name="World");
//! # }
//! ```
//!
//! The code above will actually compile to the following code:
//!
//! ```rust
//! # use yew::prelude::*;
//! # fn main() {
//! let html = html! {
//! <div>
//! <p>{"Hello World!"}</p>
//! </div>
//! };
//! # }
//! ```
//!
//! ## Using variables
//!
//! ```rust
//! # use yew_template::*;
//! # use yew::prelude::*;
//! # fn main() {
//! let name = "World";
//! let html = template_html!("templates/hello.html", name);
//! # }
//! ```
//!
//! Would compile to:
//!
//! ```rust
//! # use yew::prelude::*;
//! # fn main() {
//! let name = "World";
//! let html = html! {
//! <div>
//! <p>{"Hello "}{name}{"!"}</p>
//! </div>
//! };
//! # }
//! ```
//!
//! ## Variables with different identifiers
//!
//! ```rust
//! # use yew_template::*;
//! # use yew::prelude::*;
//! # fn main() {
//! let last_name = "World";
//! let html = template_html!("templates/hello.html", name=last_name);
//! # }
//! ```
//!
//! ## Using expressions
//!
//! ```rust
//! # use yew_template::*;
//! # use yew::prelude::*;
//! # fn main() {
//! let name_reversed = String::from("dlroW");
//! let html = template_html!(
//! "templates/hello.html",
//! name = {
//! let mut name = name_reversed.into_bytes();
//! name.reverse();
//! let name = String::from_utf8(name).unwrap();
//! name
//! }
//! );
//! # }
//! ```
//!
//! Which will also display `Hello World!` as the output is as follows:
//!
//! ```rust
//! # use yew::prelude::*;
//! # fn main() {
//! let name_reversed = String::from("dlroW");
//! let html = html! {
//! <div>
//! <p>
//! {"Hello "}{{
//! let mut name = name_reversed.into_bytes();
//! name.reverse();
//! let name = String::from_utf8(name).unwrap();
//! name
//! }}{"!"}
//! </p>
//! </div>
//! };
//! # }
//! ```
//!
//! Note that the brackets around expressions are optional.
//!
//! ## In attributes
//!
//! ```html
//! <div style=[style]>
//! <p>Hello [name]!</p>
//! </div>
//! ```
//!
//! ```rust
//! # use yew_template::*;
//! # use yew::prelude::*;
//! # fn main() {
//! let html = template_html!("templates/hello.html", name="World", style="color: red;");
//! # }
//! ```
//!
//! ## Applied to Yew callbacks
//!
//! ```html
//! <div onclick=[onclick]>
//! <p>Hello [name]!</p>
//! </div>
//! ```
//!
//! ```ignore
//! # use yew_template::*;
//! # use yew::prelude::*;
//! # fn main() {
//! let link = ctx.link();
//! let html = template_html!("templates/hello.html", name="World", onclick={link.callback(|_| Msg::AddOne)});
//! # }
//! ```
//!
//! # Notes
//!
//! - Litteral values are NOT escaped because they come from your code. Using a litteral value of `value closed by quotes" trailing stuff` will cause problems. This will be fixed in a future version.
//!
//! - You can use multiple top-level elements in your html template file.
extern crate proc_macro;
use proc_macro::TokenStream;
mod args;
mod codegen;
mod sink;
pub(crate) use {
crate::args::*,
crate::codegen::*,
crate::sink::*,
};
/// Reads a file and replaces the variables it contains with supplie values. Produces a Yew html! macro invocation.
///
/// ```ignore
/// let html = template_html!("path", arg="value", arg2="value2", arg3={expression});
/// ```
///
/// See top-level documentation for more information.
#[proc_macro]
pub fn template_html(args: TokenStream) -> TokenStream {
let args = parse_args(args);
//println!("{args:?}");
let code = generate_code(args);
code.parse().unwrap()
}