rumtk_web/utils/
render.rs

1/*
2 * rumtk attempts to implement HL7 and medical protocols for interoperability in medicine.
3 * This toolkit aims to be reliable, simple, performant, and standards compliant.
4 * Copyright (C) 2025  Luis M. Santos, M.D.
5 * Copyright (C) 2025  Nick Stephenson
6 * Copyright (C) 2025  Ethan Dixon
7 * Copyright (C) 2025  MedicalMasses L.L.C.
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 */
23use 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///
81///
82/// If using raw strings, do not leave an extra line. The first input must have characters or you will get <pre><code> blocks regardless of what you do.
83///
84#[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}