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