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) => {
103 if !url.starts_with("gemini:") && url.ends_with(".gmi") {
104 processed_url = url.replace(".gmi", ".html");
106 processed_url = url.to_string();
108 let display = if text.is_empty() {
113 format!("<p class=\"a\"><a href=\"{processed_url}\">{display}</a></p>")
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}\">")
120 GeminiLine::PreformattedToggle(false, _) => "</pre>".to_string(),
121 GeminiLine::Text(content, true) | GeminiLine::Quote(content) => content.to_string(),
125 fn finalize_html(heading_stack: &[u8], last_line: Option<&GeminiLine>) -> String {
126 let mut html = String::new();
129 Some(GeminiLine::ListItem(_)) => html.push_str("</ul>\n"),
130 Some(GeminiLine::Quote(_)) => html.push_str("</blockquote>\n"),
134 for _ in heading_stack {
135 html.push_str("</section>\n");
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");
152 fn test_heading_nesting() {
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()),
161 "<section class=\"h1\">\n\
163 <section class=\"h2\">\n\
165 <section class=\"h3\">\n\
169 <section class=\"h2\">\n\
170 <h2>Another Sub</h2>\n\
177 fn test_list_transitions() {
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()),
198 fn test_quote_transitions() {
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()),
211 <p>Normal text</p>\n\
219 fn test_preformatted() {
221 GeminiLine::PreformattedToggle(true, "code".to_string()),
222 GeminiLine::Text("let x = 42;".to_string(), true),
223 GeminiLine::PreformattedToggle(false, String::new()),
227 "<pre aria-label=\"code\">\n\
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()),
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"
253 fn test_complex_nesting() {
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()),
265 "<section class=\"h1\">\n\
268 <section class=\"h2\">\n\
278 <section class=\"h2\">\n\
279 <h2>Another Section</h2>\n\
286 fn test_empty_input() {
287 let input = Vec::new();
288 assert_eq!(render_html(&input), "");