]> git.r.bdr.sh - rbdr/page/blobdiff - src/html_renderer.rs
Use gema_texto for gemini parsing
[rbdr/page] / src / html_renderer.rs
index 47d36bb68a699743aadf4b466bb91d5dec87f9bb..6b9ce38471dbc41718f62c2799e917ced482c18a 100644 (file)
@@ -1,18 +1,18 @@
 use crate::gemini_parser::GeminiLine;
 
-/// Renders HTML from a vector of GeminiLine elements.
-/// 
+/// Renders HTML from a vector of `GeminiLine` elements.
+///
 /// # Arguments
-/// * `lines` - Vector of GeminiLine elements to render
-/// 
+/// * `lines` - Vector of `GeminiLine` elements to render
+///
 /// # Returns
 /// A String containing the rendered HTML.
-pub fn render_html(lines: Vec<GeminiLine>) -> String {
+pub fn render_html(lines: &[GeminiLine]) -> String {
     let mut heading_stack = Vec::new();
     let mut last_line: Option<&GeminiLine> = None;
     let mut result = String::new();
 
-    for line in &lines {
+    for line in lines {
         result.push_str(&line_preamble(line, last_line, &mut heading_stack));
         result.push_str(&line_content(line));
         result.push('\n');
@@ -26,23 +26,19 @@ pub fn render_html(lines: Vec<GeminiLine>) -> String {
 fn line_preamble(
     line: &GeminiLine,
     last_line: Option<&GeminiLine>,
-    heading_stack: &mut Vec<u8>
+    heading_stack: &mut Vec<u8>,
 ) -> String {
     let mut html = String::new();
 
     if let Some(last_line) = last_line {
         match last_line {
-            GeminiLine::ListItem(_) => {
-                match line {
-                    GeminiLine::ListItem(_) => {},
-                    _ => html.push_str("</ul>\n"),
-                }
+            GeminiLine::ListItem(_) => match line {
+                GeminiLine::ListItem(_) => {}
+                _ => html.push_str("</ul>\n"),
             },
-            GeminiLine::Quote(_) => {
-                match line {
-                    GeminiLine::Quote(_) => {},
-                    _ => html.push_str("</blockquote>\n"),
-                }
+            GeminiLine::Quote(_) => match line {
+                GeminiLine::Quote(_) => {}
+                _ => html.push_str("</blockquote>\n"),
             },
             _ => {}
         }
@@ -65,19 +61,15 @@ fn line_preamble(
                 }
             }
             heading_stack.push(*level);
-            html.push_str(&format!("<section class=\"h{}\">\n", level));
-        },
-        GeminiLine::ListItem(_) => {
-            match last_line {
-                Some(GeminiLine::ListItem(_)) => {},
-                _ => html.push_str("<ul>\n"),
-            }
+            html.push_str(&format!("<section class=\"h{level}\">\n"));
+        }
+        GeminiLine::ListItem(_) => match last_line {
+            Some(GeminiLine::ListItem(_)) => {}
+            _ => html.push_str("<ul>\n"),
         },
-        GeminiLine::Quote(_) => {
-            match last_line {
-                Some(GeminiLine::Quote(_)) => {},
-                _ => html.push_str("<blockquote>\n"),
-            }
+        GeminiLine::Quote(_) => match last_line {
+            Some(GeminiLine::Quote(_)) => {}
+            _ => html.push_str("<blockquote>\n"),
         },
         _ => {}
     }
@@ -87,16 +79,18 @@ fn line_preamble(
 
 fn line_content(line: &GeminiLine) -> String {
     match line {
-        GeminiLine::Text(content, false) => format!("<p>{}</p>", content),
+        GeminiLine::Text(content, false) => format!("<p>{content}</p>"),
         GeminiLine::Link(url, text) => {
             let display = if text.is_empty() { url } else { text };
-            format!("<p class=\"a\"><a href=\"{}\">{}</a></p>", url, display)
-        },
-        GeminiLine::Heading(level, content) => format!("<h{}>{}</h{}>", level, content, level),
-        GeminiLine::ListItem(content) => format!("<li>{}</li>", content),
-        GeminiLine::PreformattedToggle(true, alt_text) => { format!("<pre aria-label=\"{}\">", alt_text) }
-        GeminiLine::PreformattedToggle(false, _) => { "</pre>".to_string() }
-        GeminiLine::Text(content, true) | GeminiLine::Quote(content) => format!("{}", content)
+            format!("<p class=\"a\"><a href=\"{url}\">{display}</a></p>")
+        }
+        GeminiLine::Heading(level, content) => format!("<h{level}>{content}</h{level}>"),
+        GeminiLine::ListItem(content) => format!("<li>{content}</li>"),
+        GeminiLine::PreformattedToggle(true, alt_text) => {
+            format!("<pre aria-label=\"{alt_text}\">")
+        }
+        GeminiLine::PreformattedToggle(false, _) => "</pre>".to_string(),
+        GeminiLine::Text(content, true) | GeminiLine::Quote(content) => content.to_string(),
     }
 }
 
@@ -122,13 +116,8 @@ mod tests {
 
     #[test]
     fn test_simple_text() {
-        let input = vec![
-            GeminiLine::Text("Hello world".to_string(), false),
-        ];
-        assert_eq!(
-            render_html(input),
-            "<p>Hello world</p>\n"
-        );
+        let input = vec![GeminiLine::Text("Hello world".to_string(), false)];
+        assert_eq!(render_html(&input), "<p>Hello world</p>\n");
     }
 
     #[test]
@@ -140,7 +129,7 @@ mod tests {
             GeminiLine::Heading(2, "Another Sub".to_string()),
         ];
         assert_eq!(
-            render_html(input),
+            render_html(&input),
             "<section class=\"h1\">\n\
              <h1>Top</h1>\n\
              <section class=\"h2\">\n\
@@ -165,7 +154,7 @@ mod tests {
             GeminiLine::ListItem("New list".to_string()),
         ];
         assert_eq!(
-            render_html(input),
+            render_html(&input),
             "<ul>\n\
              <li>First</li>\n\
              <li>Second</li>\n\
@@ -186,7 +175,7 @@ mod tests {
             GeminiLine::Quote("New quote".to_string()),
         ];
         assert_eq!(
-            render_html(input),
+            render_html(&input),
             "<blockquote>\n\
              First quote\n\
              Still quoting\n\
@@ -206,7 +195,7 @@ mod tests {
             GeminiLine::PreformattedToggle(false, String::new()),
         ];
         assert_eq!(
-            render_html(input),
+            render_html(&input),
             "<pre aria-label=\"code\">\n\
              let x = 42;\n\
              </pre>\n"
@@ -217,10 +206,10 @@ mod tests {
     fn test_links() {
         let input = vec![
             GeminiLine::Link("https://example.com".to_string(), "Example".to_string()),
-            GeminiLine::Link("https://rust-lang.org".to_string(), "".to_string()),
+            GeminiLine::Link("https://rust-lang.org".to_string(), String::new()),
         ];
         assert_eq!(
-            render_html(input),
+            render_html(&input),
             "<p class=\"a\"><a href=\"https://example.com\">Example</a></p>\n\
              <p class=\"a\"><a href=\"https://rust-lang.org\">https://rust-lang.org</a></p>\n"
         );
@@ -238,7 +227,7 @@ mod tests {
             GeminiLine::Heading(2, "Another Section".to_string()),
         ];
         assert_eq!(
-            render_html(input),
+            render_html(&input),
             "<section class=\"h1\">\n\
              <h1>Title</h1>\n\
              <p>Intro</p>\n\
@@ -262,6 +251,6 @@ mod tests {
     #[test]
     fn test_empty_input() {
         let input = Vec::new();
-        assert_eq!(render_html(input), "");
+        assert_eq!(render_html(&input), "");
     }
 }