Skip to main content

termimad/
macros.rs

1/// print a markdown template, with other arguments taking `$0` to `$9` places in the template.
2///
3/// Example:
4///
5/// ```
6/// use termimad::*;
7///
8/// let skin = MadSkin::default();
9/// mad_print_inline!(
10///     &skin,
11///     "**$0 formula:** *$1*", // the markdown template, interpreted once
12///     "Disk",  // fills $0
13///     "2*π*r", // fills $1. Note that the stars don't mess the markdown
14/// );
15/// ```
16#[macro_export]
17macro_rules! mad_print_inline {
18    ($skin: expr, $md: literal $(, $value: expr )* $(,)? ) => {{
19        let vals: Vec<String> = vec![$($value.to_string(),)*];
20        #[allow(unused_variables)]
21        #[allow(unused_mut)]
22        let mut i: usize = 0;
23        use $crate::minimad::{once_cell::sync::Lazy, InlineTemplate};
24        static TEMPLATE: Lazy<InlineTemplate<'static>> = Lazy::new(|| {
25            InlineTemplate::from($md)
26        });
27        let mut composite = TEMPLATE.raw_composite();
28        for (arg_idx, val) in vals.iter().enumerate() {
29            TEMPLATE.apply(&mut composite, arg_idx, val);
30        }
31        $skin.print_composite(composite)
32    }};
33}
34
35/// write a markdown template, with other arguments taking `$0` to `$9` places in the template.
36///
37/// Example:
38///
39/// ```
40/// use termimad::*;
41///
42/// let skin = MadSkin::default();
43/// mad_write_inline!(
44///     &mut std::io::stdout(),
45///     &skin,
46///     "**$0 formula:** *$1*", // the markdown template, interpreted once
47///     "Disk",  // fills $0
48///     "2*π*r", // fills $1. Note that the stars don't mess the markdown
49/// ).unwrap();
50/// ```
51#[macro_export]
52macro_rules! mad_write_inline {
53    ($w: expr, $skin: expr, $md: literal $(, $value: expr )* $(,)? ) => {{
54        use std::io::Write;
55        let vals: Vec<String> = vec![$($value.to_string(),)*];
56        let mut i: usize = 0;
57        use $crate::minimad::{once_cell::sync::Lazy, InlineTemplate};
58        static TEMPLATE: Lazy<InlineTemplate<'static>> = Lazy::new(|| {
59            InlineTemplate::from($md)
60        });
61        let mut composite = TEMPLATE.raw_composite();
62        for (arg_idx, val) in vals.iter().enumerate() {
63            TEMPLATE.apply(&mut composite, arg_idx, val);
64        }
65        $skin.write_composite($w, composite)
66    }};
67}