]> git.proxmox.com Git - rustc.git/blame - src/doc/book/tools/src/bin/remove_markup.rs
New upstream version 1.43.0+dfsg1
[rustc.git] / src / doc / book / tools / src / bin / remove_markup.rs
CommitLineData
13cf67c4 1extern crate regex;
9fa01778 2
74b04a01 3use regex::{Captures, Regex};
13cf67c4
XL
4use std::io;
5use std::io::{Read, Write};
13cf67c4
XL
6
7fn main() {
8 write_md(remove_markup(read_md()));
9}
10
11fn read_md() -> String {
12 let mut buffer = String::new();
13 match io::stdin().read_to_string(&mut buffer) {
14 Ok(_) => buffer,
15 Err(error) => panic!(error),
16 }
17}
18
19fn write_md(output: String) {
20 write!(io::stdout(), "{}", output).unwrap();
21}
22
23fn 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| {
42 regex.replace_all(&result, |caps: &Captures<'_>| {
43 caps.get(1).unwrap().as_str().to_string()
44 }).to_string()
45 });
46 Some(result)
47 }
48 })
49 .collect();
13cf67c4
XL
50 lines.join("\n")
51}