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<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"),
},
_ => {}
}
}
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"),
},
- 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"),
},
_ => {}
}
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)
+ 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(),
}
}
#[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]