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::rumtk_web_get_component;
55 use $crate::rumtk_web_params_map;
56
57 use $crate::RUMString;
58 let component = rumtk_web_get_component!($component);
59
60 match component(&[""], &rumtk_web_params_map!($params), $app_state.clone()) {
61 Ok(x) => x.0.to_rumstring(),
62 _ => RUMString::default(),
63 }
64 }};
65}
66
67#[macro_export]
68macro_rules! rumtk_web_render_html {
69 ( $component:expr ) => {{
70 use $crate::utils::{rumtk_web_render_html, types::HTMLResult};
71
72 let closure = || -> HTMLResult { rumtk_web_render_html($component) };
73
74 closure()
75 }};
76}
77
78#[macro_export]
83macro_rules! rumtk_web_render_markdown {
84 ( $md:expr ) => {{
85 use pulldown_cmark::{Options, Parser};
86 use rumtk_core::strings::RUMStringConversions;
87
88 let mut options = Options::empty();
89 options.insert(Options::ENABLE_STRIKETHROUGH);
90 options.insert(Options::ENABLE_TASKLISTS);
91 options.insert(Options::ENABLE_MATH);
92 options.insert(Options::ENABLE_TABLES);
93 options.insert(Options::ENABLE_WIKILINKS);
94
95 let input = String::from($md);
96 let parser = Parser::new_ext(&input, options);
97 let mut html_output = String::new();
98 pulldown_cmark::html::push_html(&mut html_output, parser);
99 println!("{}", &html_output);
100
101 html_output.to_rumstring()
102 }};
103}