| 1 |
// Update the "comrak --help" text in Comrak's own README. |
| 2 |
|
| 3 |
use std::error::Error; |
| 4 |
use std::fmt::Write; |
| 5 |
use std::str; |
| 6 |
use toml::Table; |
| 7 |
|
| 8 |
use comrak::nodes::NodeValue; |
| 9 |
use comrak::{Arena, Options, format_commonmark, parse_document}; |
| 10 |
|
| 11 |
const DEPENDENCIES: &str = "[dependencies]\ncomrak = "; |
| 12 |
const HELP: &str = "$ comrak --help\n"; |
| 13 |
const HELP_START: &str = |
| 14 |
"A 100% CommonMark-compatible GitHub Flavored Markdown parser and formatter\n"; |
| 15 |
|
| 16 |
fn main() -> Result<(), Box<dyn Error>> { |
| 17 |
let arena = Arena::new(); |
| 18 |
|
| 19 |
let readme = std::fs::read_to_string("README.md")?; |
| 20 |
let doc = parse_document(&arena, &readme, &Options::default()); |
| 21 |
|
| 22 |
let cargo_toml = std::fs::read_to_string("Cargo.toml")?.parse::<Table>()?; |
| 23 |
let msrv = cargo_toml["package"].as_table().unwrap()["rust-version"] |
| 24 |
.as_str() |
| 25 |
.unwrap(); |
| 26 |
|
| 27 |
let mut in_msrv = false; |
| 28 |
let mut next_block_is_help_body = false; |
| 29 |
|
| 30 |
for node in doc.descendants() { |
| 31 |
match node.data_mut().value { |
| 32 |
NodeValue::CodeBlock(ref mut ncb) => { |
| 33 |
// Look for the Cargo.toml example block. |
| 34 |
if ncb.info == "toml" && ncb.literal.starts_with(DEPENDENCIES) { |
| 35 |
let mut content = DEPENDENCIES.to_string(); |
| 36 |
let mut version_parts = comrak::version().split('.').collect::<Vec<&str>>(); |
| 37 |
version_parts.pop(); |
| 38 |
write!(content, "\"{}\"", version_parts.join(".")).unwrap(); |
| 39 |
ncb.literal = content; |
| 40 |
continue; |
| 41 |
} |
| 42 |
|
| 43 |
// Look for a console code block whose contents starts with the HELP string. |
| 44 |
// The *next* code block contains our help, minus the starting string. |
| 45 |
if ncb.info == "console" && ncb.literal.starts_with(HELP) { |
| 46 |
next_block_is_help_body = true; |
| 47 |
continue; |
| 48 |
} |
| 49 |
|
| 50 |
if next_block_is_help_body { |
| 51 |
next_block_is_help_body = false; |
| 52 |
assert!(ncb.info.is_empty() && ncb.literal.starts_with(HELP_START)); |
| 53 |
let mut content = String::new(); |
| 54 |
let mut cmd = std::process::Command::new("cargo"); |
| 55 |
content.push_str( |
| 56 |
str::from_utf8( |
| 57 |
&cmd.args(["run", "--all-features", "--", "--help"]) |
| 58 |
.output() |
| 59 |
.unwrap() |
| 60 |
.stdout, |
| 61 |
) |
| 62 |
.unwrap(), |
| 63 |
); |
| 64 |
ncb.literal = content; |
| 65 |
continue; |
| 66 |
} |
| 67 |
} |
| 68 |
NodeValue::HtmlInline(ref mut s) => { |
| 69 |
if s == "<span class=\"msrv\">" { |
| 70 |
in_msrv = true; |
| 71 |
} else if in_msrv && s == "</span>" { |
| 72 |
in_msrv = false; |
| 73 |
} |
| 74 |
} |
| 75 |
NodeValue::Text(ref mut t) => { |
| 76 |
if in_msrv { |
| 77 |
*t = msrv.to_string().into(); |
| 78 |
} |
| 79 |
} |
| 80 |
_ => {} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
let mut options = Options::default(); |
| 85 |
options.render.prefer_fenced = true; |
| 86 |
options.render.experimental_minimize_commonmark = true; |
| 87 |
|
| 88 |
let mut out = String::new(); |
| 89 |
format_commonmark(doc, &options, &mut out)?; |
| 90 |
std::fs::write("README.md", &out)?; |
| 91 |
|
| 92 |
// Strip everything except the "Contributing" section. |
| 93 |
|
| 94 |
let mut in_contributing = false; |
| 95 |
|
| 96 |
for node in doc.children() { |
| 97 |
if let NodeValue::Heading(ref nh) = node.data().value { |
| 98 |
if !in_contributing { |
| 99 |
in_contributing = nh.level == 2 && node.collect_text() == "Contributing"; |
| 100 |
node.detach(); |
| 101 |
continue; |
| 102 |
} else if nh.level <= 2 { |
| 103 |
in_contributing = false; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
if !in_contributing { |
| 108 |
node.detach(); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
out.clear(); |
| 113 |
writeln!( |
| 114 |
out, |
| 115 |
"<!-- This file is generated by examples/update-readme.rs. -->" |
| 116 |
)?; |
| 117 |
writeln!(out)?; |
| 118 |
writeln!( |
| 119 |
out, |
| 120 |
"The following is included verbatim from [README.md](README.md)." |
| 121 |
)?; |
| 122 |
writeln!(out)?; |
| 123 |
writeln!(out, "---")?; |
| 124 |
writeln!(out)?; |
| 125 |
format_commonmark(doc, &options, &mut out)?; |
| 126 |
|
| 127 |
std::fs::write("CONTRIBUTING.md", &out)?; |
| 128 |
|
| 129 |
Ok(()) |
| 130 |
} |