krz/orgo

Lightning fast org-mode static site generator. fast go org-mode static-site-generator

examples/skeleton.rs

46 lines · 1584 bytes

 1//! Render an org file the way orgo would and print either its HTML or its semantic
 2//! skeleton — the reference outputs the cross-language conformance corpus is built from.
 3//!
 4//! Usage:
 5//!   cargo run --example skeleton -- <file.org>            # print the skeleton
 6//!   cargo run --example skeleton -- --html <file.org>     # print the rendered HTML
 7//!
 8//! The skeleton is one line per element open, element close, or text run (see
 9//! [`orgo::skeleton`]). Another implementation conforms when its own output, reduced by
10//! its own port of the same reduction, equals this.
11
12use camino::Utf8PathBuf;
13
14use orgo::parser::parse;
15use orgo::render::{render, Html, SyntectHighlighter};
16use orgo::resolve::ResolvedDoc;
17use orgo::skeleton::skeleton;
18
19fn main() {
20    let mut args = std::env::args().skip(1);
21    let mut html_mode = false;
22    let mut path: Option<String> = None;
23    for arg in args.by_ref() {
24        match arg.as_str() {
25            "--html" => html_mode = true,
26            _ => path = Some(arg),
27        }
28    }
29    let Some(path) = path else {
30        eprintln!("usage: skeleton [--html] <file.org>");
31        std::process::exit(2);
32    };
33
34    let path = Utf8PathBuf::from(path);
35    let source = std::fs::read_to_string(&path).expect("read org file");
36    let document = parse(path.as_path(), &source).expect("parse org file");
37    let Html(html) = render(&ResolvedDoc { document }, &SyntectHighlighter::new());
38
39    if html_mode {
40        print!("{html}");
41    } else {
42        for line in skeleton(&html) {
43            println!("{line}");
44        }
45    }
46}