1 use crate::gemini_parser::GeminiLine;
3 /// Renders HTML from a vector of `GeminiLine` elements.
6 /// * `lines` - Vector of `GeminiLine` elements to render
9 /// A String containing the rendered HTML.
13 /// use std::fs::read_to_string;
14 /// use gema_texto::gemini_parser::parse;
15 /// use gema_texto::html_renderer::render_html;
17 /// let gemini_source = "\
19 /// This is some gemini text!
20 /// => https://test Go to the test page.
23 /// let gemini_lines = parse(&gemini_source);
24 /// let html_source = render_html(&gemini_lines);
25 /// println!("{html_source}");
28 pub 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();
34 result.push_str(&line_preamble(line, last_line, &mut heading_stack));
35 result.push_str(&line_content(line));
37 last_line = Some(line);
40 result.push_str(&finalize_html(&heading_stack, last_line));
46 last_line: Option<&GeminiLine>,
47 heading_stack: &mut Vec<u8>,
49 let mut html = String::new();
51 if let Some(last_line) = last_line {
53 GeminiLine::ListItem(_) => match line {
54 GeminiLine::ListItem(_) => {}
55 _ => html.push_str("</ul>\n"),
57 GeminiLine::Quote(_) => match line {
58 GeminiLine::Quote(_) => {}
59 _ => html.push_str("</blockquote>\n"),
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);
75 html.push_str("</section>\n");
77 if open_heading == *level {
81 heading_stack.push(*level);
82 html.push_str(&format!("<section class=\"h{level}\">\n"));
84 GeminiLine::ListItem(_) => match last_line {
85 Some(GeminiLine::ListItem(_)) => {}
86 _ => html.push_str("<ul>\n"),
88 GeminiLine::Quote(_) => match last_line {
89 Some(GeminiLine::Quote(_)) => {}
90 _ => html.push_str("<blockquote>\n"),
98 fn line_content(line: &GeminiLine) -> String {
100 GeminiLine::Text(content, false) => format!("<p>{content}</p>"),
101 GeminiLine::Link(url, text) => {
102 let display = if text.is_empty() { url } else { text };
103 format!("<p class=\"a\"><a href=\"{url}\">{display}</a></p>")
105 GeminiLine::Heading(level, content) => format!("<h{level}>{content}</h{level}>"),
106 GeminiLine::ListItem(content) => format!("<li>{content}</li>"),
107 GeminiLine::PreformattedToggle(true, alt_text) => {
108 format!("<pre aria-label=\"{alt_text}\">")
110 GeminiLine::PreformattedToggle(false, _) => "</pre>".to_string(),
111 GeminiLine::Text(content, true) | GeminiLine::Quote(content) => content.to_string(),
115 fn finalize_html(heading_stack: &[u8], last_line: Option<&GeminiLine>) -> String {
116 let mut html = String::new();
119 Some(GeminiLine::ListItem(_)) => html.push_str("</ul>\n"),
120 Some(GeminiLine::Quote(_)) => html.push_str("</blockquote>\n"),
124 for _ in heading_stack {
125 html.push_str("</section>\n");
136 fn test_simple_text() {
137 let input = vec![GeminiLine::Text("Hello world".to_string(), false)];
138 assert_eq!(render_html(&input), "<p>Hello world</p>\n");
142 fn test_heading_nesting() {
144 GeminiLine::Heading(1, "Top".to_string()),
145 GeminiLine::Heading(2, "Sub".to_string()),
146 GeminiLine::Heading(3, "SubSub".to_string()),
147 GeminiLine::Heading(2, "Another Sub".to_string()),
151 "<section class=\"h1\">\n\
153 <section class=\"h2\">\n\
155 <section class=\"h3\">\n\
159 <section class=\"h2\">\n\
160 <h2>Another Sub</h2>\n\
167 fn test_list_transitions() {
169 GeminiLine::ListItem("First".to_string()),
170 GeminiLine::ListItem("Second".to_string()),
171 GeminiLine::Text("Break".to_string(), false),
172 GeminiLine::ListItem("New list".to_string()),
188 fn test_quote_transitions() {
190 GeminiLine::Quote("First quote".to_string()),
191 GeminiLine::Quote("Still quoting".to_string()),
192 GeminiLine::Text("Normal text".to_string(), false),
193 GeminiLine::Quote("New quote".to_string()),
201 <p>Normal text</p>\n\
209 fn test_preformatted() {
211 GeminiLine::PreformattedToggle(true, "code".to_string()),
212 GeminiLine::Text("let x = 42;".to_string(), true),
213 GeminiLine::PreformattedToggle(false, String::new()),
217 "<pre aria-label=\"code\">\n\
226 GeminiLine::Link("https://example.com".to_string(), "Example".to_string()),
227 GeminiLine::Link("https://rust-lang.org".to_string(), String::new()),
231 "<p class=\"a\"><a href=\"https://example.com\">Example</a></p>\n\
232 <p class=\"a\"><a href=\"https://rust-lang.org\">https://rust-lang.org</a></p>\n"
237 fn test_complex_nesting() {
239 GeminiLine::Heading(1, "Title".to_string()),
240 GeminiLine::Text("Intro".to_string(), false),
241 GeminiLine::Heading(2, "Section".to_string()),
242 GeminiLine::ListItem("Point 1".to_string()),
243 GeminiLine::ListItem("Point 2".to_string()),
244 GeminiLine::Quote("Important quote".to_string()),
245 GeminiLine::Heading(2, "Another Section".to_string()),
249 "<section class=\"h1\">\n\
252 <section class=\"h2\">\n\
262 <section class=\"h2\">\n\
263 <h2>Another Section</h2>\n\
270 fn test_empty_input() {
271 let input = Vec::new();
272 assert_eq!(render_html(&input), "");