origami_engine/lib.rs
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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
//! # Origami Engine
//! A Rust templating engine that allows for rendering HTML elements and building reusable components.
//!
//! ## Props Passing
//!
//! You can pass props to components to customize their behavior and appearance. Below is an example of a homepage and an about page, both utilizing a button component with various attributes.
//! ```rust
//! use origami_engine::comp;
//!
//! // Define a button component that takes props
//! comp! {
//! button_component =>
//! button class="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600" $attr {
//! $label
//! }
//! }
//!
//! // Define the homepage component
//! comp! {
//! home =>
//! div {
//! h1 { "Welcome to the Homepage!" }
//! // Use the button_component with props
//! @button_component! { attr { onclick="alert('clicked')" }, label { "Click Me" } };
//! }
//! }
//!
//! let html = home!();
//!
//! assert_eq!(
//! html.0,
//! r#"<div><h1>Welcome to the Homepage!</h1><button class="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600" onclick="alert('clicked')">Click Me</button></div>"#
//! );
//!
//! // Define the about page component
//! comp! {
//! about =>
//! div {
//! h1 { "About Us" }
//! p { "We are committed to delivering quality service." }
//! // Use the button_component with props
//! @button_component! { attr { onclick="alert('clicked learn more')" }, label { "Learn More" } };
//! }
//! }
//!
//! let html = about!();
//! assert_eq!(
//! html.0,
//! r#"<div><h1>About Us</h1><p>We are committed to delivering quality service.</p><button class="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600" onclick="alert('clicked learn more')">Learn More</button></div>"#
//! );
//! ```
//!
//! ## Layout
//!
//! You can create a layout structure that includes a navigation bar, a body for dynamic content, and a footer. Below is an example demonstrating this layout.
//!
//! ```rust
//! use origami_engine::comp;
//!
//! // Define a layout component with a navigation bar, body, and footer
//! comp! {
//! layout_component =>
//! // Navigation bar
//! nav {
//! ul {
//! li { a { "Home" } }
//! li { a { "About" } }
//! li { a { "Contact" } }
//! }
//! }
//! // Body placeholder for dynamic content
//! main {
//! $content
//! }
//! // Footer
//! footer {
//! p { "© 2024 Your Company" }
//! }
//! }
//!
//! // Define the homepage component using the layout
//! comp! {
//! home =>
//! @layout_component! {
//! content {
//! h1 { "Welcome to the Homepage!" }
//! p { "This is the main content of the homepage." }
//! }
//! };
//! }
//!
//! let html = home!();
//! assert_eq!(
//! html.0,
//! r#"<nav><ul><li><a>Home</a></li><li><a>About</a></li><li><a>Contact</a></li></ul></nav><main><h1>Welcome to the Homepage!</h1><p>This is the main content of the homepage.</p></main><footer><p>© 2024 Your Company</p></footer>"#
//! );
//!
//! // Define the about page component using the layout
//! comp! {
//! about =>
//! @layout_component! {
//! content {
//! h1 { "About Us" }
//! p { "We are committed to delivering quality service." }
//! }
//! };
//! }
//!
//! let html = about!();
//! assert_eq!(
//! html.0,
//! r#"<nav><ul><li><a>Home</a></li><li><a>About</a></li><li><a>Contact</a></li></ul></nav><main><h1>About Us</h1><p>We are committed to delivering quality service.</p></main><footer><p>© 2024 Your Company</p></footer>"#
//! );
//! ```
//! ## Escape and Noescape
//!
//! You can use `escape` and `noescape` to control HTML escaping behavior in the template (`html_escape` is feature is required):
//!
//! ```rust
//! #[cfg(feature = "html_escape")]
//! {
//! use origami_engine::comp;
//!
//! comp! {
//! foo =>
//! div noescape {
//! div { "<div>Unsafe HTML</div>" } // Inherited, this will not be escaped
//! div escape {
//! "<div>Safe HTML</div>" // This will be escaped
//! }
//! }
//! }
//!
//! let html = foo!();
//! assert_eq!(html.0, "<div><div><div>Unsafe HTML</div></div><div><div>Safe HTML</div></div></div>");
//! }
//! ```
//!
//! You can also use `noescape` or `escape` with conditional rendering:
//!
//! ```rust
//! #[cfg(feature = "html_escape")]
//! {
//! use origami_engine::comp;
//!
//! let text = "bar";
//!
//! comp! {
//! foo =>
//! div noescape {
//! if text == "foo"; noescape {
//! "<div>Unsafe HTML</div>"
//! } else if text == "bar"; escape {
//! "<div>Safe HTML</div>"
//! } else noescape {
//! "<div>Default HTML</div>"
//! }
//! }
//! }
//!
//! let html = foo!();
//! assert_eq!(html.0, "<div><div>Safe HTML</div></div>");
//! }
//! ```
//!
//! Or with expressions:
//!
//! ```rust
//! #[cfg(feature = "html_escape")]
//! {
//! use origami_engine::comp;
//!
//! let text = "<div>foo</div>";
//! comp! {
//! foo =>
//! div { *text;! }
//! }
//!
//! let html = foo!();
//! assert_eq!(html.0, "<div><div>foo</div></div>");
//! }
//! ```
//!
//! Or with literals:
//!
//! ```rust
//! #[cfg(feature = "html_escape")]
//! {
//! use origami_engine::comp;
//!
//! comp! {
//! foo =>
//! div {
//! "<div>foo</div>"!
//! }
//! }
//!
//! let html = foo!();
//! assert_eq!(html.0, "<div><div>foo</div></div>");
//! }
//! ```
//!
//! Or match expressions:
//! ```rust
//! #[cfg(feature = "html_escape")]
//! {
//! use origami_engine::comp;
//!
//! let text = "foo";
//! comp! {
//! foo =>
//! div {
//! match text; noescape {
//! "foo" => {
//! "<div>foo</div>" // Inherited, this will not be escaped
//! },
//! _ => escape {
//! "<div>foo</div>" // This will be escaped
//! }
//! }
//! }
//! }
//!
//! let html = foo!();
//! assert_eq!(html.0, "<div><div>foo</div></div>");
//! }
//! ```
//!
//! ## Moveable Scripts
//!
//! To create moveable scripts, assign a unique name to the script. You can then use this name below the component call.
//!
//! ## Example
//!
//! ```rust
//! use origami_engine::comp;
//!
//! // Define a component that includes a moveable script named "bar_script"
//! comp! {
//! bar =>
//! div {
//! "bar_component"
//! script script_name="bar_script" {
//! r#"function foo() {
//! return "hello world";
//! }"#
//! }
//! }
//! }
//!
//! // Define another component that uses the previously defined component
//! comp! {
//! foo =>
//! div {
//! "foo_component"
//! @bar!(); // Include the bar component
//! }
//! // Use the moveable script below the component call
//! script_use bar_script; // Include the script with the specified name
//! }
//!
//! let html = foo!();
//!
//! #[cfg(feature = "minify_html")] // `minify_html` is required to minify the script
//! assert_eq!(
//! html.0,
//! "<div>foo_component<div>bar_component</div></div><script>function foo() { return \"hello world\"; }</script>"
//! );
//! ```
/// # `anon!` Macro
///
/// The `anon!` macro is responsible for rendering HTML elements into a `String`. It supports conditionals, loops, and any expression that returns `&str`.
///
/// ### Basic Usage
/// ```rust
/// use origami_macros::anon;
///
/// let mut s = String::new();
/// anon!(s, div {
/// "Hello, World!"
/// });
/// assert_eq!(s, "<div>Hello, World!</div>");
/// ```
///
/// ### Expressions
/// You can include any expression that returns `&str` directly in the HTML:
/// ```rust
/// use origami_macros::anon;
///
/// let foo = "some_string";
/// let mut s = String::new();
/// anon!(s, div {
/// *foo;
/// });
/// assert_eq!(s, "<div>some_string</div>");
///
/// let mut s = String::new();
/// let bar = "dynamic_string";
/// anon!(s, div {
/// *format!("Hello, {}", bar).as_str(); // Using format! as an expression
/// });
/// assert_eq!(s, "<div>Hello, dynamic_string</div>");
/// ```
///
/// ### Match Expressions
/// `anon!` supports match expressions for dynamic content:
/// ```rust
/// use origami_macros::anon;
///
/// let value = "bar";
/// let mut s = String::new();
/// anon!(s, div {
/// match value; {
/// "bar" => { "Bar Component" },
/// _ => { "Default Component" },
/// }
/// });
/// assert_eq!(s, "<div>Bar Component</div>");
/// ```
///
/// ### Conditionals
/// `anon!` supports conditionals directly:
/// ```rust
/// use origami_macros::anon;
///
/// let condition = true;
/// let mut s = String::new();
/// anon!(s, div {
/// if condition; {
/// "Condition met!"
/// }
/// });
/// assert_eq!(s, "<div>Condition met!</div>");
/// ```
///
/// ### Loops
/// You can iterate over collections with `anon!`:
/// ```rust
/// use origami_macros::anon;
///
/// struct Points {
/// x: i32,
/// y: i32,
/// }
/// let points = [
/// Points { x: 1, y: 2 },
/// Points { x: 3, y: 4 },
/// ];
/// let mut s = String::new();
/// anon!(s, div {
/// for point in points.iter(); {
/// div {
/// *point.x.to_string().as_str();
/// ", "
/// *point.y.to_string().as_str();
/// }
/// }
/// });
/// assert_eq!(s, "<div><div>1, 2</div><div>3, 4</div></div>");
/// ```
pub use origami_macros::anon;
/// # `comp!` Macro
///
/// The `comp!` macro generates a reusable component by defining its structure and allowing the replacement of props. This facilitates the creation of dynamic components that can be rendered with different values.
///
/// ### Example
/// ```rust
/// use origami_macros::{comp};
///
/// comp! {
/// greeting_component =>
/// div {
/// "Hello, "
/// $name
/// }
///
/// }
///
/// let html = greeting_component!(name { "World" });
/// assert_eq!(html.0, "<div>Hello, World</div>");
/// ```
pub use origami_macros::comp;
pub struct Origami(pub String);
#[cfg(feature = "html_escape")]
#[doc(no_inline)]
pub use html_escape::encode_text_to_string;
#[cfg(feature = "minify_html")]
#[doc(no_inline)]
pub use minify_html::*;
#[cfg(feature = "axum")]
use ::axum::response::{Html, IntoResponse, Response};
#[cfg(feature = "axum")]
impl IntoResponse for Origami {
fn into_response(self) -> Response {
Html(self.0).into_response()
}
}