summaryrefslogtreecommitdiff
path: root/fable-cat-poc/src/main.rs
blob: fb82b8a5de1634942e9fc50308dcd8464590ee3a (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use std::io::{self, Read};
use std::ops::{Range, RangeBounds};

use pulldown_cmark::{Event, Options, Parser, Tag};

#[derive(Default)]
struct Span {
    first_line: usize,
    first_column: usize,
    last_line: usize,
    last_column: usize,
}

fn location_to_span(input: &str, loc: &Range<usize>) -> Span {
    let mut ret = Span::default();

    let mut line = 1;
    let mut col = 1;
    for (pos, ch) in input.chars().enumerate() {
        if ch == '\n' {
            line += 1;
            col = 1;
        } else {
            col += 1;
        }
        if pos == loc.start {
            ret.first_line = line;
            ret.first_column = col;
        }
        if pos == loc.end {
            ret.last_line = line;
            ret.last_column = col;
        }
    }

    ret
}

fn print_thing(input: &str, loc: &Range<usize>) {
    let content = &input[loc.clone()];
    let span = location_to_span(input, loc);
    println!(
        "> From line {} column {} to line {} column {}",
        span.first_line, span.first_column, span.last_line, span.last_column
    );
    for l in content.lines() {
        println!("> `{}`", l);
    }
    if span.first_line == span.last_line {
        print!("> `");
        for _ in 0..span.first_column {
            print!(" ");
        }
        for _ in span.first_column..=span.last_column {
            print!("^");
        }
        println!("`");
    }
}

fn main() -> io::Result<()> {
    let mut input = String::new();
    let inputlen = io::stdin().read_to_string(&mut input)?;
    eprintln!("Read {} bytes of input", inputlen);
    let options = Options::all();
    let parser = Parser::new_ext(&input, options);

    let mut depth = 0;
    let mut in_scenario = false;
    let mut in_scenario_block = false;
    let mut in_header = false;
    let mut last_header_depth = 1000;
    let mut last_header_content: Option<String> = None;
    for (event, span) in parser.into_offset_iter() {
        eprintln!("{:?}", event);
        match event {
            Event::Start(tag) => {
                depth += 1;
                eprintln!("Depth now: {}", depth);
                if depth == 1 {
                    match tag {
                        Tag::Header(level) => {
                            // Two ways to handle a header,
                            // If we're in a scenario, we care only if this header
                            // is at the same or a higher level, otherwise we
                            // always read the header in.
                            if in_scenario {
                                if level <= last_header_depth {
                                    println!("```");
                                    in_scenario = false;
                                    in_header = true;
                                    last_header_depth = level;
                                }
                            } else {
                                in_header = true;
                                last_header_depth = level;
                            }
                        }
                        Tag::CodeBlock(name) => {
                            if name.as_ref() == "fable" {
                                // Fable code block, only valid if we were
                                // after a header
                                if last_header_content.is_some() {
                                    in_scenario_block = true;
                                }
                            }
                        }
                        _ => {}
                    }
                }
            }
            Event::End(tag) => {
                depth -= 1;
                eprintln!("Depth now: {}", depth);
                if depth == 0 {
                    match tag {
                        Tag::Header(_) => {
                            // We're ending a header now
                            in_header = false;
                        }
                        Tag::CodeBlock(_) => {
                            in_scenario_block = false;
                        }
                        _ => {}
                    }
                }
            }
            Event::Text(s) => {
                if in_header {
                    // We're parsing a header
                    last_header_content = Some(s.to_string());
                    in_header = false;
                    if cfg!(feature = "print_headers") {
                        print_thing(&input, &span);
                    }
                } else if in_scenario_block {
                    if !in_scenario {
                        for _ in 0..last_header_depth {
                            print!("#");
                        }
                        println!(" {}", last_header_content.as_ref().unwrap());
                        in_scenario = true;
                        println!("```fable");
                    }
                    print!("{}", s);
                }
            }
            _ => {}
        }
    }
    Ok(())
}