From 260e8ec69b8e08b9fd105bf688e7a3a9fafecd61 Mon Sep 17 00:00:00 2001 From: Ruben Beltran del Rio Date: Fri, 3 Jan 2025 20:59:02 +0100 Subject: Add first tests --- src/html_renderer.rs | 267 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 267 insertions(+) create mode 100644 src/html_renderer.rs (limited to 'src/html_renderer.rs') diff --git a/src/html_renderer.rs b/src/html_renderer.rs new file mode 100644 index 0000000..47d36bb --- /dev/null +++ b/src/html_renderer.rs @@ -0,0 +1,267 @@ +use crate::gemini_parser::GeminiLine; + +/// Renders HTML from a vector of GeminiLine elements. +/// +/// # Arguments +/// * `lines` - Vector of GeminiLine elements to render +/// +/// # Returns +/// A String containing the rendered HTML. +pub fn render_html(lines: Vec) -> String { + let mut heading_stack = Vec::new(); + let mut last_line: Option<&GeminiLine> = None; + let mut result = String::new(); + + for line in &lines { + result.push_str(&line_preamble(line, last_line, &mut heading_stack)); + result.push_str(&line_content(line)); + result.push('\n'); + last_line = Some(line); + } + + result.push_str(&finalize_html(&heading_stack, last_line)); + result +} + +fn line_preamble( + line: &GeminiLine, + last_line: Option<&GeminiLine>, + heading_stack: &mut Vec +) -> 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("\n"), + } + }, + GeminiLine::Quote(_) => { + match line { + GeminiLine::Quote(_) => {}, + _ => html.push_str("\n"), + } + }, + _ => {} + } + } + + match line { + GeminiLine::Heading(level, _) => { + while let Some(open_heading) = heading_stack.pop() { + // You just encountered a more important heading. + // Put it back. Desist. + if open_heading < *level { + heading_stack.push(open_heading); + break; + } + + html.push_str("\n"); + + if open_heading == *level { + break; + } + } + heading_stack.push(*level); + html.push_str(&format!("
\n", level)); + }, + GeminiLine::ListItem(_) => { + match last_line { + Some(GeminiLine::ListItem(_)) => {}, + _ => html.push_str("
    \n"), + } + }, + GeminiLine::Quote(_) => { + match last_line { + Some(GeminiLine::Quote(_)) => {}, + _ => html.push_str("
    \n"), + } + }, + _ => {} + } + + html +} + +fn line_content(line: &GeminiLine) -> String { + match line { + GeminiLine::Text(content, false) => format!("

    {}

    ", content), + GeminiLine::Link(url, text) => { + let display = if text.is_empty() { url } else { text }; + format!("

    {}

    ", url, display) + }, + GeminiLine::Heading(level, content) => format!("{}", level, content, level), + GeminiLine::ListItem(content) => format!("
  • {}
  • ", content), + GeminiLine::PreformattedToggle(true, alt_text) => { format!("
    ", alt_text) }
    +        GeminiLine::PreformattedToggle(false, _) => { "
    ".to_string() } + GeminiLine::Text(content, true) | GeminiLine::Quote(content) => format!("{}", content) + } +} + +fn finalize_html(heading_stack: &[u8], last_line: Option<&GeminiLine>) -> String { + let mut html = String::new(); + + match last_line { + Some(GeminiLine::ListItem(_)) => html.push_str("
\n"), + Some(GeminiLine::Quote(_)) => html.push_str("\n"), + _ => {} + } + + for _ in heading_stack { + html.push_str("
\n"); + } + + html +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_simple_text() { + let input = vec![ + GeminiLine::Text("Hello world".to_string(), false), + ]; + assert_eq!( + render_html(input), + "

Hello world

\n" + ); + } + + #[test] + fn test_heading_nesting() { + let input = vec![ + GeminiLine::Heading(1, "Top".to_string()), + GeminiLine::Heading(2, "Sub".to_string()), + GeminiLine::Heading(3, "SubSub".to_string()), + GeminiLine::Heading(2, "Another Sub".to_string()), + ]; + assert_eq!( + render_html(input), + "
\n\ +

Top

\n\ +
\n\ +

Sub

\n\ +
\n\ +

SubSub

\n\ +
\n\ +
\n\ +
\n\ +

Another Sub

\n\ +
\n\ +
\n" + ); + } + + #[test] + fn test_list_transitions() { + let input = vec![ + GeminiLine::ListItem("First".to_string()), + GeminiLine::ListItem("Second".to_string()), + GeminiLine::Text("Break".to_string(), false), + GeminiLine::ListItem("New list".to_string()), + ]; + assert_eq!( + render_html(input), + "
    \n\ +
  • First
  • \n\ +
  • Second
  • \n\ +
\n\ +

Break

\n\ +
    \n\ +
  • New list
  • \n\ +
\n" + ); + } + + #[test] + fn test_quote_transitions() { + let input = vec![ + GeminiLine::Quote("First quote".to_string()), + GeminiLine::Quote("Still quoting".to_string()), + GeminiLine::Text("Normal text".to_string(), false), + GeminiLine::Quote("New quote".to_string()), + ]; + assert_eq!( + render_html(input), + "
\n\ + First quote\n\ + Still quoting\n\ +
\n\ +

Normal text

\n\ +
\n\ + New quote\n\ +
\n" + ); + } + + #[test] + fn test_preformatted() { + let input = vec![ + GeminiLine::PreformattedToggle(true, "code".to_string()), + GeminiLine::Text("let x = 42;".to_string(), true), + GeminiLine::PreformattedToggle(false, String::new()), + ]; + assert_eq!( + render_html(input), + "
\n\
+             let x = 42;\n\
+             
\n" + ); + } + + #[test] + 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()), + ]; + assert_eq!( + render_html(input), + "

Example

\n\ +

https://rust-lang.org

\n" + ); + } + + #[test] + fn test_complex_nesting() { + let input = vec![ + GeminiLine::Heading(1, "Title".to_string()), + GeminiLine::Text("Intro".to_string(), false), + GeminiLine::Heading(2, "Section".to_string()), + GeminiLine::ListItem("Point 1".to_string()), + GeminiLine::ListItem("Point 2".to_string()), + GeminiLine::Quote("Important quote".to_string()), + GeminiLine::Heading(2, "Another Section".to_string()), + ]; + assert_eq!( + render_html(input), + "
\n\ +

Title

\n\ +

Intro

\n\ +
\n\ +

Section

\n\ +
    \n\ +
  • Point 1
  • \n\ +
  • Point 2
  • \n\ +
\n\ +
\n\ + Important quote\n\ +
\n\ +
\n\ +
\n\ +

Another Section

\n\ +
\n\ +
\n" + ); + } + + #[test] + fn test_empty_input() { + let input = Vec::new(); + assert_eq!(render_html(input), ""); + } +} -- cgit