summaryrefslogtreecommitdiff
path: root/src/bin/pandoc-filter-diagram.rs
blob: c2f808a74442947a22bed5ce03f5e839dfb74619 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//! A program that can be used with the pandoc --filter option.

use pandoc_filter_diagram::DiagramFilter;
use std::io::{Read, Write};

fn main() {
    if let Err(err) = real_main() {
        eprintln!("ERROR: {}", err);
        std::process::exit(1);
    }
}

fn real_main() -> anyhow::Result<()> {
    let fail_on_error = if let Ok(v) = std::env::var("PANDOC_FILTER_FAIL") {
        v == "1"
    } else {
        false
    };

    let mut df = DiagramFilter::new();
    let mut json = String::new();
    std::io::stdin().read_to_string(&mut json)?;
    let json = pandoc_ast::filter(json, |doc| df.filter(doc));
    if !df.errors().is_empty() {
        for e in df.errors().iter() {
            eprintln!("ERROR: {}", e);
        }
        if fail_on_error {
            eprintln!("Failing as requested");
            std::process::exit(1);
        }
    }
    std::io::stdout().write_all(json.as_bytes())?;
    Ok(())
}