aboutsummaryrefslogtreecommitdiff
path: root/src/convert
diff options
context:
space:
mode:
authorFuwn <[email protected]>2025-05-27 16:16:35 +0000
committerFuwn <[email protected]>2025-05-27 16:16:35 +0000
commit884aa53553e4a811dc269d60fd6fc59e837390d3 (patch)
tree53718aa956dd7623465a9dd2fa142d024d4f9726 /src/convert
parentci(ga): bump rustc (diff)
downloadgerm-884aa53553e4a811dc269d60fd6fc59e837390d3.tar.xz
germ-884aa53553e4a811dc269d60fd6fc59e837390d3.zip
refactor: Use latest best practices and formatting
Diffstat (limited to 'src/convert')
-rw-r--r--src/convert/html.rs42
-rw-r--r--src/convert/markdown.rs43
2 files changed, 51 insertions, 34 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 => {}
}
diff --git a/src/convert/markdown.rs b/src/convert/markdown.rs
index 71e54f2..d90790f 100644
--- a/src/convert/markdown.rs
+++ b/src/convert/markdown.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 markdown = String::new();
@@ -25,14 +25,17 @@ pub fn convert(source: &[Node]) -> String {
// this AST tree to an alternative markup format.
for node in source {
match node {
- Node::Text(text) => markdown.push_str(&format!("{text}\n")),
+ Node::Text(text) => {
+ let _ = writeln!(&mut markdown, "{text}");
+ }
Node::Link { to, text } => markdown.push_str(&text.clone().map_or_else(
|| format!("<{to}>\n"),
|text| format!("[{text}]({to})\n"),
)),
Node::Heading { level, text } => {
- markdown.push_str(&format!(
- "{} {}\n",
+ let _ = writeln!(
+ &mut markdown,
+ "{} {}",
match level {
1 => "#",
2 => "##",
@@ -40,23 +43,29 @@ pub fn convert(source: &[Node]) -> String {
_ => "",
},
text
- ));
+ );
+ }
+ Node::List(items) => {
+ let _ = writeln!(
+ &mut markdown,
+ "{}",
+ items
+ .iter()
+ .map(|i| format!("- {i}"))
+ .collect::<Vec<String>>()
+ .join("\n"),
+ );
+ }
+ Node::Blockquote(text) => {
+ let _ = writeln!(&mut markdown, "> {text}");
}
- Node::List(items) => markdown.push_str(&format!(
- "{}\n",
- items
- .iter()
- .map(|i| format!("- {i}"))
- .collect::<Vec<String>>()
- .join("\n"),
- )),
- Node::Blockquote(text) => markdown.push_str(&format!("> {text}\n")),
Node::PreformattedText { alt_text, text } => {
- markdown.push_str(&format!(
- "```{}\n{}```\n",
+ let _ = writeln!(
+ &mut markdown,
+ "```{}\n{}```",
alt_text.clone().unwrap_or_default(),
text
- ));
+ );
}
Node::Whitespace => markdown.push('\n'),
}