X-Git-Url: https://git.r.bdr.sh/rbdr/page/blobdiff_plain/b03284133baa6339fe4adf48e4a6499ef3ac287b..8766e4412b95cfa0288683748cc20aba81a64d08:/src/html_renderer.rs?ds=sidebyside diff --git a/src/html_renderer.rs b/src/html_renderer.rs index 1b26bff..7724131 100644 --- a/src/html_renderer.rs +++ b/src/html_renderer.rs @@ -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) -> 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'); @@ -61,7 +61,7 @@ fn line_preamble( } } heading_stack.push(*level); - html.push_str(&format!("
\n", level)); + html.push_str(&format!("
\n")); } GeminiLine::ListItem(_) => match last_line { Some(GeminiLine::ListItem(_)) => {} @@ -79,15 +79,15 @@ fn line_preamble( fn line_content(line: &GeminiLine) -> String { match line { - GeminiLine::Text(content, false) => format!("

{}

", content), + GeminiLine::Text(content, false) => format!("

{content}

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

{}

", url, display) + format!("

{display}

") } - GeminiLine::Heading(level, content) => format!("{}", level, content, level), - GeminiLine::ListItem(content) => format!("
  • {}
  • ", content), + GeminiLine::Heading(level, content) => format!("{content}"), + GeminiLine::ListItem(content) => format!("
  • {content}
  • "), GeminiLine::PreformattedToggle(true, alt_text) => { - format!("
    ", alt_text)
    +            format!("
    ")
             }
             GeminiLine::PreformattedToggle(false, _) => "
    ".to_string(), GeminiLine::Text(content, true) | GeminiLine::Quote(content) => content.to_string(), @@ -117,7 +117,7 @@ mod tests { #[test] fn test_simple_text() { let input = vec![GeminiLine::Text("Hello world".to_string(), false)]; - assert_eq!(render_html(input), "

    Hello world

    \n"); + assert_eq!(render_html(&input), "

    Hello world

    \n"); } #[test] @@ -129,7 +129,7 @@ mod tests { GeminiLine::Heading(2, "Another Sub".to_string()), ]; assert_eq!( - render_html(input), + render_html(&input), "
    \n\

    Top

    \n\
    \n\ @@ -154,7 +154,7 @@ mod tests { GeminiLine::ListItem("New list".to_string()), ]; assert_eq!( - render_html(input), + render_html(&input), "
      \n\
    • First
    • \n\
    • Second
    • \n\ @@ -175,7 +175,7 @@ mod tests { GeminiLine::Quote("New quote".to_string()), ]; assert_eq!( - render_html(input), + render_html(&input), "
      \n\ First quote\n\ Still quoting\n\ @@ -195,7 +195,7 @@ mod tests { GeminiLine::PreformattedToggle(false, String::new()), ]; assert_eq!( - render_html(input), + render_html(&input), "
      \n\
                    let x = 42;\n\
                    
      \n" @@ -209,7 +209,7 @@ mod tests { GeminiLine::Link("https://rust-lang.org".to_string(), "".to_string()), ]; assert_eq!( - render_html(input), + render_html(&input), "

      Example

      \n\

      https://rust-lang.org

      \n" ); @@ -227,7 +227,7 @@ mod tests { GeminiLine::Heading(2, "Another Section".to_string()), ]; assert_eq!( - render_html(input), + render_html(&input), "
      \n\

      Title

      \n\

      Intro

      \n\ @@ -251,6 +251,6 @@ mod tests { #[test] fn test_empty_input() { let input = Vec::new(); - assert_eq!(render_html(input), ""); + assert_eq!(render_html(&input), ""); } }