diff options
| author | Fuwn <[email protected]> | 2025-05-27 16:16:35 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2025-05-27 16:16:35 +0000 |
| commit | 884aa53553e4a811dc269d60fd6fc59e837390d3 (patch) | |
| tree | 53718aa956dd7623465a9dd2fa142d024d4f9726 /src/convert/html.rs | |
| parent | ci(ga): bump rustc (diff) | |
| download | germ-884aa53553e4a811dc269d60fd6fc59e837390d3.tar.xz germ-884aa53553e4a811dc269d60fd6fc59e837390d3.zip | |
refactor: Use latest best practices and formatting
Diffstat (limited to 'src/convert/html.rs')
| -rw-r--r-- | src/convert/html.rs | 42 |
1 files changed, 25 insertions, 17 deletions
diff --git a/src/convert/html.rs b/src/convert/html.rs index a6b0426..4608cd5 100644 --- a/src/convert/html.rs +++ b/src/convert/html.rs @@ -16,7 +16,7 @@ // Copyright (C) 2022-2022 Fuwn <[email protected]> // SPDX-License-Identifier: GPL-3.0-only -use crate::ast::Node; +use {crate::ast::Node, std::fmt::Write}; pub fn convert(source: &[Node]) -> String { let mut html = String::new(); @@ -25,16 +25,20 @@ pub fn convert(source: &[Node]) -> String { // this AST tree to an alternative markup format. for node in source { match node { - Node::Text(text) => html.push_str(&format!("<p>{text}</p>")), + Node::Text(text) => { + let _ = write!(&mut html, "<p>{text}</p>"); + } Node::Link { to, text } => { - html.push_str(&format!( + let _ = write!( + &mut html, "<a href=\"{}\">{}</a><br>", to, text.clone().unwrap_or_else(|| to.clone()) - )); + ); } Node::Heading { level, text } => { - html.push_str(&format!( + let _ = write!( + &mut html, "<{}>{}</{0}>", match level { 1 => "h1", @@ -43,20 +47,24 @@ pub fn convert(source: &[Node]) -> String { _ => "p", }, text - )); + ); + } + Node::List(items) => { + let _ = write!( + &mut html, + "<ul>{}</ul>", + items + .iter() + .map(|i| format!("<li>{i}</li>")) + .collect::<Vec<String>>() + .join("\n") + ); + } + Node::Blockquote(text) => { + let _ = write!(&mut html, "<blockquote>{text}</blockquote>"); } - Node::List(items) => html.push_str(&format!( - "<ul>{}</ul>", - items - .iter() - .map(|i| format!("<li>{i}</li>")) - .collect::<Vec<String>>() - .join("\n") - )), - Node::Blockquote(text) => - html.push_str(&format!("<blockquote>{text}</blockquote>")), Node::PreformattedText { text, .. } => { - html.push_str(&format!("<pre>{text}</pre>")); + let _ = write!(&mut html, "<pre>{text}</pre>"); } Node::Whitespace => {} } |