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