diff --git a/src/gemini.rs b/src/gemini.rs index d2d9f23..fee4916 100644 --- a/src/gemini.rs +++ b/src/gemini.rs @@ -1,6 +1,6 @@ /// This module implements a simple text/gemini parser based on the description /// here: https://gemini.circumlunar.space/docs/specification.html -use std::io::Write; +use std::io::{self, Write}; /// Build a gemini document up from a series of nodes. #[derive(Default)] @@ -54,6 +54,27 @@ impl Builder { } } +/// Render a set of nodes as a document to a writer. +pub fn render(nodes: Vec, out: &mut impl Write) -> io::Result<()> { + use Node::*; + + for node in nodes { + match node { + Text(body) => write!(out, "{}\n", body)?, + Link{to, name} => match name { + Some(name) => write!(out, "{} {}\n", to, name)?, + None => write!(out, "{}\n", to)?, + }, + Preformatted(body) => write!(out, "```\n{}\n```\n", body)?, + Heading { level, body} => write!(out, "{} {}\n", "#".repeat(level as usize), body)?, + ListItem(body) => write!(out, "* {}\n", body)?, + Quote(body) => write!(out, "> {}\n", body)?, + }; + } + + Ok(()) +} + /// Individual nodes of the document. Each node correlates to a line in the file. #[derive(Debug, PartialEq, Eq)] pub enum Node {