build.rs

changeset 6
bcb508479948
equal deleted inserted replaced
5:df971c81282e 6:bcb508479948
1 use std::env;
2 use regex::{Regex, Captures};
3
4 fn proc<A : AsRef<str>>(re : &str, str : A) -> String {
5 let need_to_escape = Regex::new(r"([_*\\])").unwrap();
6 Regex::new(re).unwrap().replace_all(str.as_ref(), |caps : &Captures| {
7 format!("{}{}{}",
8 caps.get(1).unwrap().as_str(),
9 need_to_escape.replace_all(caps.get(2).unwrap().as_str(), "\\$1"),
10 caps.get(3).unwrap().as_str()
11 )
12 }).to_string()
13 }
14
15 fn main() {
16 let out_dir = env::var("OUT_DIR").unwrap();
17
18 // Since rust is stuck in 80's 7-bit gringo ASCII world, so that rustdoc does not support
19 // markdown KaTeX mathematics, we have to process the README to include horrible horrible
20 // horrible escapes for the math, and then use an vomit-inducingly ugly javasccript
21 // workaround to process the math on the fly.
22
23 println!("cargo:rerun-if-changed=README.md");
24
25 let readme = std::fs::read_to_string("README.md")
26 .expect("Error reading README");
27
28 // Escape _, *, and \ in equations.
29 let readme_uglified = proc(r"(?m)([^$]\$)([^$]+)(\$[^$])",
30 proc(r"([^$]\$\$)([^$]+)(\$\$[^$])", readme));
31 // Remove the instructions for building the documentation
32 let readme_cut = Regex::new("## Internals(.*|\n)*")
33 .unwrap()
34 .replace_all(&readme_uglified, "");
35
36 std::fs::write(out_dir + "/README_uglified.md", readme_cut.as_bytes())
37 .expect("Error saving uglified README");
38 }

mercurial