diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/ast.rs | 58 | ||||
| -rw-r--r-- | src/convert/markdown.rs | 2 |
2 files changed, 59 insertions, 1 deletions
@@ -215,6 +215,64 @@ impl Ast { } } + #[must_use] + pub fn to_gemtext(&self) -> String { + let mut gemtext = "".to_string(); + + for node in &self.inner { + match node { + Node::Text(text) => gemtext.push_str(&format!("{}\n", text)), + Node::Link { + to, + text, + } => + gemtext.push_str(&format!( + "=> {}{}\n", + to, + text + .clone() + .map_or_else(|| "".to_string(), |text| format!(" {}", text)), + )), + Node::Heading { + level, + text, + } => + gemtext.push_str(&format!( + "{} {}\n", + match level { + 1 => "#", + 2 => "##", + 3 => "###", + _ => "", + }, + text + )), + Node::List(items) => + gemtext.push_str(&format!( + "{}\n", + items + .iter() + .map(|i| format!("* {}", i)) + .collect::<Vec<String>>() + .join("\n"), + )), + Node::Blockquote(text) => gemtext.push_str(&format!("> {}\n", text)), + Node::PreformattedText { + alt_text, + text, + } => + gemtext.push_str(&format!( + "```{}\n{}```\n", + alt_text.clone().unwrap_or_else(|| "".to_string()), + text + )), + Node::Whitespace => gemtext.push('\n'), + } + } + + gemtext + } + /// The actual AST of `Ast` /// /// # Example diff --git a/src/convert/markdown.rs b/src/convert/markdown.rs index c6899b7..a38da9f 100644 --- a/src/convert/markdown.rs +++ b/src/convert/markdown.rs @@ -69,7 +69,7 @@ pub fn convert(source: &[Node]) -> String { text )); } - Node::Whitespace => markdown.push_str("\n"), + Node::Whitespace => markdown.push('\n'), } } |