summaryrefslogtreecommitdiff
path: root/isabelle2unicode/src/main.rs
blob: 7dcbf69323dd3ec50eb82d83a83f3a1e1ac54a64 (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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use std::ffi::OsString;
use std::io::{self, BufReader, BufRead};

use isabelle_unicode::PrettyUnicode;


const HELP: &str =
"isabelle2unicode: convert isabelle markup to unicode text.

USAGE: isabelle2unicode [input file]

Read in [input file] and convert any markup found to its equivalent symbols
listed in Isabelle's etc/symbols file.

If not given, read from stdin.

OPTIONS:
  -h --help: display this help
";


fn main() {

    let args: Vec<OsString> = std::env::args_os().collect();

    let ifile = match &args[1..] {
        [] => None,
        [h] if h.to_str() == Some("-h") || h.to_str() == Some("--help") => {
            eprintln!("{}", HELP);
            std::process::exit(0)
        }
        [ifile] => Some(ifile),
        _ => {
            eprintln!("{}", HELP);
            std::process::exit(1)
        }
    };

    let instream: Box<dyn BufRead> = match ifile {
        None => Box::new(BufReader::new(io::stdin())),
        Some(ref path) => {
            let file = std::fs::File::open(path)
                .expect(&format!("could not open file {}", path.to_string_lossy()));
            Box::new(BufReader::new(file))
        }
    };


    instream
         .lines()
         .filter_map(|line| match line {
             Ok(line) if line.trim().is_empty()
                 => Some("\n".to_string()),
             Ok(line)
                 => line.to_pretty_unicode(),
             Err(_)
                 => None
         })
        .for_each(|line| print!("{}", line));

}