馃 The Definitive Gemini Protocol Toolkit
gemini gemini-protocol gemtext parser zero-dependency toolkit ast converter html markdown cli networking
at main 54 lines 1.3 kB view raw
1use {crate::ast::Node, std::fmt::Write}; 2 3pub fn convert(source: &[Node]) -> String { 4 let mut html = String::new(); 5 6 // Since we have an AST tree of the Gemtext, it is very easy to convert from 7 // this AST tree to an alternative markup format. 8 for node in source { 9 match node { 10 Node::Text(text) => { 11 let _ = write!(&mut html, "<p>{text}</p>"); 12 } 13 Node::Link { to, text } => { 14 let _ = write!( 15 &mut html, 16 "<a href=\"{}\">{}</a><br>", 17 to, 18 text.clone().unwrap_or_else(|| to.clone()) 19 ); 20 } 21 Node::Heading { level, text } => { 22 let _ = write!( 23 &mut html, 24 "<{}>{}</{0}>", 25 match level { 26 1 => "h1", 27 2 => "h2", 28 3 => "h3", 29 _ => "p", 30 }, 31 text 32 ); 33 } 34 Node::List(items) => { 35 let _ = write!(&mut html, "<ul>"); 36 37 for item in items { 38 let _ = write!(&mut html, "<li>{item}</li>"); 39 } 40 41 let _ = write!(&mut html, "</ul>"); 42 } 43 Node::Blockquote(text) => { 44 let _ = write!(&mut html, "<blockquote>{text}</blockquote>"); 45 } 46 Node::PreformattedText { text, .. } => { 47 let _ = write!(&mut html, "<pre>{text}</pre>"); 48 } 49 Node::Whitespace => {} 50 } 51 } 52 53 html 54}