gemini: add document rendering function

This commit is contained in:
Cadey Ratio 2020-07-27 20:31:05 -04:00
parent 851d7925c5
commit 4216f6b709
1 changed files with 22 additions and 1 deletions

View File

@ -1,6 +1,6 @@
/// This module implements a simple text/gemini parser based on the description /// This module implements a simple text/gemini parser based on the description
/// here: https://gemini.circumlunar.space/docs/specification.html /// 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. /// Build a gemini document up from a series of nodes.
#[derive(Default)] #[derive(Default)]
@ -54,6 +54,27 @@ impl Builder {
} }
} }
/// Render a set of nodes as a document to a writer.
pub fn render(nodes: Vec<Node>, 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. /// Individual nodes of the document. Each node correlates to a line in the file.
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
pub enum Node { pub enum Node {