rumtk_web/utils/
render.rs1use crate::utils::types::HTMLResult;
24use axum::response::Html;
25use rumtk_core::strings::rumtk_format;
26
27pub fn rumtk_web_render_html<T: askama::Template>(template: T) -> HTMLResult {
28 let result = template.render();
29 match result {
30 Ok(html) => Ok(Html(html)),
31 Err(e) => {
32 let tn = std::any::type_name::<T>();
33 Err(rumtk_format!("Template {tn} render failed: {e:?}"))
34 }
35 }
36}
37
38#[macro_export]
39macro_rules! rumtk_web_render_component {
40 ( $component_fxn:expr ) => {{
41 use rumtk_core::strings::RUMStringConversions;
42 match $component_fxn() {
43 Ok(x) => x.0.to_rumstring(),
44 _ => RUMString::default(),
45 }
46 }};
47 ( $component_fxn:expr, $app_state:expr ) => {{
48 use rumtk_core::strings::RUMStringConversions;
49 match $component_fxn($app_state.clone()) {
50 Ok(x) => x.0.to_rumstring(),
51 _ => RUMString::default(),
52 }
53 }};
54 ( $component:expr, $params:expr, $app_state:expr ) => {{
55 use rumtk_core::strings::RUMStringConversions;
56 use $crate::rumtk_web_get_component;
57 use $crate::rumtk_web_params_map;
58
59 use $crate::RUMString;
60 let component = rumtk_web_get_component!($component);
61
62 match component(&[""], &rumtk_web_params_map!($params), $app_state.clone()) {
63 Ok(x) => x.0.to_rumstring(),
64 _ => RUMString::default(),
65 }
66 }};
67}
68
69#[macro_export]
70macro_rules! rumtk_web_render_html {
71 ( $component:expr ) => {{
72 use $crate::utils::{rumtk_web_render_html, types::HTMLResult};
73
74 let closure = || -> HTMLResult { rumtk_web_render_html($component) };
75
76 closure()
77 }};
78}
79
80#[macro_export]
85macro_rules! rumtk_web_render_markdown {
86 ( $md:expr ) => {{
87 use pulldown_cmark::{Options, Parser};
88 use rumtk_core::strings::RUMStringConversions;
89
90 let mut options = Options::empty();
91 options.insert(Options::ENABLE_STRIKETHROUGH);
92 options.insert(Options::ENABLE_TASKLISTS);
93 options.insert(Options::ENABLE_MATH);
94 options.insert(Options::ENABLE_TABLES);
95 options.insert(Options::ENABLE_WIKILINKS);
96
97 let input = String::from($md);
98 let parser = Parser::new_ext(&input, options);
99 let mut html_output = String::new();
100 pulldown_cmark::html::push_html(&mut html_output, parser);
101 println!("{}", &html_output);
102
103 html_output.to_rumstring()
104 }};
105}