spaces_printer/
markdown.rs

1use crate::Printer;
2use std::sync::Arc;
3
4pub struct Markdown<'a> {
5    pub printer: &'a mut Printer,
6}
7
8pub fn heading(level: u8, content: &str) -> String {
9    format!("{} {}\n\n", "#".repeat(level as usize), content)
10}
11
12pub fn hline() -> &'static str {
13    "\n---\n\n"
14}
15
16pub fn list(items: Vec<Arc<str>>) -> String {
17    let mut result = String::new();
18    for item in items {
19        result.push_str(format!("- {item}\n").as_str());
20    }
21    result.push('\n');
22    result
23}
24
25pub fn list_item(level: u8, item: &str) -> String {
26    let level = if level == 0 { 1_usize } else { level as usize };
27    format!("{}- {}\n", " ".repeat(((level) - 1) * 2), item)
28}
29
30pub fn bold(content: &str) -> String {
31    format!("**{content}**")
32}
33
34pub fn hyperlink(show: &str, link: &str) -> String {
35    format!("[{show}]({link})")
36}
37
38pub fn italic(content: &str) -> String {
39    format!("*{content}*")
40}
41
42pub fn strikethrough(content: &str) -> String {
43    format!("~~{content}~~")
44}
45
46pub fn code(content: &str) -> String {
47    format!("`{content}`")
48}
49
50pub fn code_block(code_type: &str, content: &str) -> String {
51    format!("```{code_type}\n{content}\n```\n")
52}
53
54pub fn paragraph(content: &str) -> String {
55    format!("{content}\n\n")
56}
57
58impl<'a> Markdown<'a> {
59    pub fn new(printer: &'a mut Printer) -> Self {
60        Markdown { printer }
61    }
62
63    pub fn heading(&mut self, level: u8, content: &str) -> anyhow::Result<()> {
64        self.printer.write(&heading(level, content))?;
65        Ok(())
66    }
67
68    pub fn write(&mut self, content: &str) -> anyhow::Result<()> {
69        self.printer.write(content)
70    }
71
72    pub fn hline(&mut self) -> anyhow::Result<()> {
73        self.printer.write(hline())
74    }
75
76    pub fn list(&mut self, items: Vec<Arc<str>>) -> anyhow::Result<()> {
77        self.printer.write(&list(items))
78    }
79
80    pub fn list_item(&mut self, level: u8, item: &str) -> anyhow::Result<()> {
81        self.printer.write(&list_item(level, item))
82    }
83
84    pub fn bold(&mut self, content: &str) -> anyhow::Result<()> {
85        self.printer.write(&bold(content))?;
86        Ok(())
87    }
88
89    pub fn hyperlink(&mut self, show: &str, link: &str) -> anyhow::Result<()> {
90        self.printer.write(&hyperlink(show, link))?;
91        Ok(())
92    }
93
94    pub fn italic(&mut self, content: &str) -> anyhow::Result<()> {
95        self.printer.write(&italic(content))?;
96        Ok(())
97    }
98
99    pub fn strikethrough(&mut self, content: &str) -> anyhow::Result<()> {
100        self.printer.write(&strikethrough(content))?;
101        Ok(())
102    }
103
104    pub fn code(&mut self, content: &str) -> anyhow::Result<()> {
105        self.printer.write(&code(content))?;
106        Ok(())
107    }
108
109    pub fn code_block(&mut self, code_type: &str, content: &str) -> anyhow::Result<()> {
110        self.printer.write(&code_block(code_type, content))?;
111        Ok(())
112    }
113
114    pub fn paragraph(&mut self, content: &str) -> anyhow::Result<()> {
115        self.printer.write(&paragraph(content))?;
116        Ok(())
117    }
118}