etch 0.4.2

Not just a text formatter, don't mark it down, etch it.
Documentation
pub mod nodes;
pub mod parsers;
pub mod plugins;
pub mod state;

use crate::nodes::{Node, Nodes, RenderableNode};
use crate::state::*;
use crate::plugins::SharedPlugin;
use bork::Source;
use glue::prelude::*;
use std::fs::read_to_string;
use std::path::{Path, PathBuf};
use std::fmt::{Display, Formatter, Result as FormatResult};

pub type Errors = Vec<Error>;

#[derive(Debug, Clone, PartialEq)]
pub enum Error {
    DocumentNotFound(PathBuf),
    DocumentSyntax(PathBuf, String),
}

impl Display for Error {
    fn fmt(&self, f: &mut Formatter<'_>) -> FormatResult {
        match self {
            Error::DocumentNotFound(path) => write!(f, "Document not found: {}", path.display()),
            Error::DocumentSyntax(_, error) => write!(f, "{}", error),
        }
    }
}

#[derive(Clone, Default, Debug)]
pub struct Etch {
    errors: Errors,
    state: SharedState,
    nodes: Nodes,
}

impl Etch {
    pub fn with_document<P: AsRef<Path>>(mut self, filename: P) -> Self {
        let filename = filename.as_ref().to_path_buf();
        let source = read_to_string(filename.clone()).unwrap();
        let parent = filename.parent();

        if let Some(parent) = parent {
            self.state.borrow_mut().paths.push(parent.to_path_buf());
        }

        match parsers::block::document(self.state.clone()).parse(&source) {
            Ok((_, nodes)) => {
                self.nodes.append(&mut nodes
                    .into_iter()
                    .map(|node| node.finish(self.state.clone()))
                    .collect());
            }
            Err((_, error)) => {
                let mut source = Source::new(filename.clone(), &source);

                source.add_error(error);

                self.errors.push(Error::DocumentSyntax(
                    filename.clone(),
                    format!("{}", source),
                ));
            }
        }

        if let Some(_) = parent {
            self.state.borrow_mut().paths.pop();
        }

        self
    }

    pub fn with_plugin(self, plugin: SharedPlugin) -> Self {
        self.state.borrow_mut().plugins.push(plugin);
        self
    }

    pub fn document(self) -> Result<Option<Node>, Errors> {
        if self.errors.is_empty() {
            Ok(Some(Node::Fragment {
                children: Some(self.nodes),
            }))
        } else {
            Err(self.errors)
        }
    }

    pub fn render(self) -> Result<String, Errors> {
        if self.errors.is_empty() {
            Ok(self.nodes.html())
        } else {
            Err(self.errors)
        }
    }
}

#[test]
fn example() {
    use crate::plugins::*;

    let metadata = MetadataPlugin::new();
    let word_count = WordCountPlugin::new();
    let etch = Etch::default()
        .with_plugin(metadata.clone())
        .with_plugin(word_count.clone())
        .with_plugin(SyntectPlugin::new())
        .with_plugin(WidowedWordsPlugin::new())
        .with_document("samples/example.etch");

    println!("{:#?}", etch.render());
    println!("{:#?}", metadata);
    println!("{:#?}", word_count);
}