]>
Commit | Line | Data |
---|---|---|
13cf67c4 | 1 | extern crate regex; |
9fa01778 | 2 | |
74b04a01 | 3 | use regex::{Captures, Regex}; |
13cf67c4 XL |
4 | use std::io; |
5 | use std::io::{Read, Write}; | |
13cf67c4 XL |
6 | |
7 | fn main() { | |
8 | write_md(remove_markup(read_md())); | |
9 | } | |
10 | ||
11 | fn read_md() -> String { | |
12 | let mut buffer = String::new(); | |
13 | match io::stdin().read_to_string(&mut buffer) { | |
14 | Ok(_) => buffer, | |
136023e0 | 15 | Err(error) => panic!("{}", error), |
13cf67c4 XL |
16 | } |
17 | } | |
18 | ||
19 | fn write_md(output: String) { | |
20 | write!(io::stdout(), "{}", output).unwrap(); | |
21 | } | |
22 | ||
23 | fn remove_markup(input: String) -> String { | |
74b04a01 XL |
24 | let filename_regex = |
25 | Regex::new(r#"\A<span class="filename">(.*)</span>\z"#).unwrap(); | |
9fa01778 | 26 | // Captions sometimes take up multiple lines. |
74b04a01 XL |
27 | let caption_start_regex = |
28 | Regex::new(r#"\A<span class="caption">(.*)\z"#).unwrap(); | |
13cf67c4 XL |
29 | let caption_end_regex = Regex::new(r#"(.*)</span>\z"#).unwrap(); |
30 | let regexen = vec![filename_regex, caption_start_regex, caption_end_regex]; | |
31 | ||
74b04a01 XL |
32 | let lines: Vec<_> = input |
33 | .lines() | |
34 | .flat_map(|line| { | |
35 | // Remove our syntax highlighting and rustdoc markers. | |
36 | if line.starts_with("```") { | |
37 | Some(String::from("```")) | |
38 | // Remove the span around filenames and captions. | |
39 | } else { | |
40 | let result = | |
41 | regexen.iter().fold(line.to_string(), |result, regex| { | |
136023e0 XL |
42 | regex |
43 | .replace_all(&result, |caps: &Captures<'_>| { | |
44 | caps.get(1).unwrap().as_str().to_string() | |
45 | }) | |
46 | .to_string() | |
74b04a01 XL |
47 | }); |
48 | Some(result) | |
49 | } | |
50 | }) | |
51 | .collect(); | |
13cf67c4 XL |
52 | lines.join("\n") |
53 | } |