]> git.r.bdr.sh - rbdr/gema_texto/blob - src/html_renderer.rs
Replace HTML tags in HTML
[rbdr/gema_texto] / src / html_renderer.rs
1 use crate::gemini_parser::GeminiLine;
2 use std::path::Path;
3
4 /// Renders HTML from a vector of `GeminiLine` elements.
5 ///
6 /// # Arguments
7 /// * `lines` - Vector of `GeminiLine` elements to render
8 ///
9 /// # Returns
10 /// A String containing the rendered HTML.
11 ///
12 /// # Examples
13 /// ```
14 /// use std::fs::read_to_string;
15 /// use gema_texto::gemini_parser::parse;
16 /// use gema_texto::html_renderer::render_html;
17 ///
18 /// let gemini_source = "\
19 /// # Hello
20 /// This is some gemini text!
21 /// => https://test Go to the test page.
22 /// ";
23 ///
24 /// let gemini_lines = parse(&gemini_source);
25 /// let html_source = render_html(&gemini_lines);
26 /// println!("{html_source}");
27 /// ```
28 #[must_use]
29 pub fn render_html(lines: &[GeminiLine]) -> String {
30 let mut heading_stack = Vec::new();
31 let mut last_line: Option<&GeminiLine> = None;
32 let mut result = String::new();
33
34 for line in lines {
35 result.push_str(&line_preamble(line, last_line, &mut heading_stack));
36 result.push_str(&line_content(line));
37 result.push('\n');
38 last_line = Some(line);
39 }
40
41 result.push_str(&finalize_html(&heading_stack, last_line));
42 result
43 }
44
45 fn line_preamble(
46 line: &GeminiLine,
47 last_line: Option<&GeminiLine>,
48 heading_stack: &mut Vec<u8>,
49 ) -> String {
50 let mut html = String::new();
51
52 if let Some(last_line) = last_line {
53 match last_line {
54 GeminiLine::ListItem(_) => match line {
55 GeminiLine::ListItem(_) => {}
56 _ => html.push_str("</ul>\n"),
57 },
58 GeminiLine::Quote(_) => match line {
59 GeminiLine::Quote(_) => {}
60 _ => html.push_str("</blockquote>\n"),
61 },
62 _ => {}
63 }
64 }
65
66 match line {
67 GeminiLine::Heading(level, _) => {
68 while let Some(open_heading) = heading_stack.pop() {
69 // You just encountered a more important heading.
70 // Put it back. Desist.
71 if open_heading < *level {
72 heading_stack.push(open_heading);
73 break;
74 }
75
76 html.push_str("</section>\n");
77
78 if open_heading == *level {
79 break;
80 }
81 }
82 heading_stack.push(*level);
83 html.push_str(&format!("<section class=\"h{level}\">\n"));
84 }
85 GeminiLine::ListItem(_) => match last_line {
86 Some(GeminiLine::ListItem(_)) => {}
87 _ => html.push_str("<ul>\n"),
88 },
89 GeminiLine::Quote(_) => match last_line {
90 Some(GeminiLine::Quote(_)) => {}
91 _ => html.push_str("<blockquote>\n"),
92 },
93 _ => {}
94 }
95
96 html
97 }
98
99 fn line_content(line: &GeminiLine) -> String {
100 match line {
101 GeminiLine::Text(content, false) => format!("<p>{content}</p>"),
102 GeminiLine::Link(url, text) => {
103 let path = Path::new(url);
104 let processed_url = if !url.starts_with("gemini:")
105 && path
106 .extension()
107 .map_or(false, |ext| ext.eq_ignore_ascii_case("gmi"))
108 {
109 url.replace(".gmi", ".html")
110 } else {
111 url.to_string()
112 };
113
114 let display = if text.is_empty() {
115 &processed_url
116 } else {
117 text
118 };
119 format!("<p class=\"a\"><a href=\"{processed_url}\">{display}</a></p>")
120 }
121 GeminiLine::Heading(level, content) => format!("<h{level}>{content}</h{level}>"),
122 GeminiLine::ListItem(content) => format!("<li>{content}</li>"),
123 GeminiLine::PreformattedToggle(true, alt_text) => {
124 format!("<pre aria-label=\"{alt_text}\">")
125 }
126 GeminiLine::PreformattedToggle(false, _) => "</pre>".to_string(),
127 GeminiLine::Text(content, true) | GeminiLine::Quote(content) => content
128 .replace('<', "&lt;")
129 .replace('>', "&gt;")
130 .to_string(),
131 }
132 }
133
134 fn finalize_html(heading_stack: &[u8], last_line: Option<&GeminiLine>) -> String {
135 let mut html = String::new();
136
137 match last_line {
138 Some(GeminiLine::ListItem(_)) => html.push_str("</ul>\n"),
139 Some(GeminiLine::Quote(_)) => html.push_str("</blockquote>\n"),
140 _ => {}
141 }
142
143 for _ in heading_stack {
144 html.push_str("</section>\n");
145 }
146
147 html
148 }
149
150 #[cfg(test)]
151 mod tests {
152 use super::*;
153
154 #[test]
155 fn test_simple_text() {
156 let input = vec![GeminiLine::Text("Hello world".to_string(), false)];
157 assert_eq!(render_html(&input), "<p>Hello world</p>\n");
158 }
159
160 #[test]
161 fn test_heading_nesting() {
162 let input = vec![
163 GeminiLine::Heading(1, "Top".to_string()),
164 GeminiLine::Heading(2, "Sub".to_string()),
165 GeminiLine::Heading(3, "SubSub".to_string()),
166 GeminiLine::Heading(2, "Another Sub".to_string()),
167 ];
168 assert_eq!(
169 render_html(&input),
170 "<section class=\"h1\">\n\
171 <h1>Top</h1>\n\
172 <section class=\"h2\">\n\
173 <h2>Sub</h2>\n\
174 <section class=\"h3\">\n\
175 <h3>SubSub</h3>\n\
176 </section>\n\
177 </section>\n\
178 <section class=\"h2\">\n\
179 <h2>Another Sub</h2>\n\
180 </section>\n\
181 </section>\n"
182 );
183 }
184
185 #[test]
186 fn test_list_transitions() {
187 let input = vec![
188 GeminiLine::ListItem("First".to_string()),
189 GeminiLine::ListItem("Second".to_string()),
190 GeminiLine::Text("Break".to_string(), false),
191 GeminiLine::ListItem("New list".to_string()),
192 ];
193 assert_eq!(
194 render_html(&input),
195 "<ul>\n\
196 <li>First</li>\n\
197 <li>Second</li>\n\
198 </ul>\n\
199 <p>Break</p>\n\
200 <ul>\n\
201 <li>New list</li>\n\
202 </ul>\n"
203 );
204 }
205
206 #[test]
207 fn test_quote_transitions() {
208 let input = vec![
209 GeminiLine::Quote("First quote".to_string()),
210 GeminiLine::Quote("Still quoting".to_string()),
211 GeminiLine::Text("Normal text".to_string(), false),
212 GeminiLine::Quote("New quote".to_string()),
213 ];
214 assert_eq!(
215 render_html(&input),
216 "<blockquote>\n\
217 First quote\n\
218 Still quoting\n\
219 </blockquote>\n\
220 <p>Normal text</p>\n\
221 <blockquote>\n\
222 New quote\n\
223 </blockquote>\n"
224 );
225 }
226
227 #[test]
228 fn test_preformatted() {
229 let input = vec![
230 GeminiLine::PreformattedToggle(true, "code".to_string()),
231 GeminiLine::Text("let x = 42;".to_string(), true),
232 GeminiLine::PreformattedToggle(false, String::new()),
233 ];
234 assert_eq!(
235 render_html(&input),
236 "<pre aria-label=\"code\">\n\
237 let x = 42;\n\
238 </pre>\n"
239 );
240 }
241
242 #[test]
243 fn test_links() {
244 let input = vec![
245 GeminiLine::Link("gemini://hi.gmi".to_string(), "Example".to_string()),
246 GeminiLine::Link("/hi.gmi/kidding".to_string(), "Example".to_string()),
247 GeminiLine::Link("/hi.gmi".to_string(), "Example".to_string()),
248 GeminiLine::Link("https://example.com".to_string(), "Example".to_string()),
249 GeminiLine::Link("https://rust-lang.org".to_string(), String::new()),
250 ];
251 assert_eq!(
252 render_html(&input),
253 "<p class=\"a\"><a href=\"gemini://hi.gmi\">Example</a></p>\n\
254 <p class=\"a\"><a href=\"/hi.gmi/kidding\">Example</a></p>\n\
255 <p class=\"a\"><a href=\"/hi.html\">Example</a></p>\n\
256 <p class=\"a\"><a href=\"https://example.com\">Example</a></p>\n\
257 <p class=\"a\"><a href=\"https://rust-lang.org\">https://rust-lang.org</a></p>\n"
258 );
259 }
260
261 #[test]
262 fn test_complex_nesting() {
263 let input = vec![
264 GeminiLine::Heading(1, "Title".to_string()),
265 GeminiLine::Text("Intro".to_string(), false),
266 GeminiLine::Heading(2, "Section".to_string()),
267 GeminiLine::ListItem("Point 1".to_string()),
268 GeminiLine::ListItem("Point 2".to_string()),
269 GeminiLine::Quote("Important quote".to_string()),
270 GeminiLine::Heading(2, "Another Section".to_string()),
271 ];
272 assert_eq!(
273 render_html(&input),
274 "<section class=\"h1\">\n\
275 <h1>Title</h1>\n\
276 <p>Intro</p>\n\
277 <section class=\"h2\">\n\
278 <h2>Section</h2>\n\
279 <ul>\n\
280 <li>Point 1</li>\n\
281 <li>Point 2</li>\n\
282 </ul>\n\
283 <blockquote>\n\
284 Important quote\n\
285 </blockquote>\n\
286 </section>\n\
287 <section class=\"h2\">\n\
288 <h2>Another Section</h2>\n\
289 </section>\n\
290 </section>\n"
291 );
292 }
293
294 #[test]
295 fn test_empty_input() {
296 let input = Vec::new();
297 assert_eq!(render_html(&input), "");
298 }
299 }