-
-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
README: update with MSRV, and update mechanism for doing so.
Thanks to @kaesluder and @bioinformatist for various improvements that are working together here!
- Loading branch information
Showing
4 changed files
with
138 additions
and
46 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,77 @@ | ||
// Update the "comrak --help" text in Comrak's own README. | ||
|
||
use in_place::InPlace; | ||
use std::error::Error; | ||
use std::fmt::Write; | ||
use std::str; | ||
use toml::Table; | ||
|
||
use comrak::nodes::{AstNode, NodeValue}; | ||
use comrak::nodes::NodeValue; | ||
use comrak::{format_commonmark, parse_document, Arena, Options}; | ||
|
||
const DEPENDENCIES: &str = "[dependencies]\ncomrak = "; | ||
const HELP: &str = "$ comrak --help\n"; | ||
|
||
fn main() -> Result<(), Box<dyn std::error::Error + 'static>> { | ||
fn main() -> Result<(), Box<dyn Error>> { | ||
let arena = Arena::new(); | ||
|
||
let readme = std::fs::read_to_string("README.md")?; | ||
let inp = InPlace::new("README.md").open()?; | ||
let readme = std::io::read_to_string(inp.reader())?; | ||
let doc = parse_document(&arena, &readme, &Options::default()); | ||
|
||
fn iter_nodes<'a, F>(node: &'a AstNode<'a>, f: &F) | ||
where | ||
F: Fn(&'a AstNode<'a>), | ||
{ | ||
f(node); | ||
for c in node.children() { | ||
iter_nodes(c, f); | ||
} | ||
} | ||
let cargo_toml = std::fs::read_to_string("Cargo.toml")?.parse::<Table>()?; | ||
let msrv = cargo_toml["package"].as_table().unwrap()["rust-version"] | ||
.as_str() | ||
.unwrap(); | ||
|
||
iter_nodes(doc, &|node| { | ||
if let NodeValue::CodeBlock(ref mut ncb) = node.data.borrow_mut().value { | ||
// Look for the Cargo.toml example block. | ||
if ncb.info == "toml" && ncb.literal.starts_with(DEPENDENCIES) { | ||
let mut content = DEPENDENCIES.to_string(); | ||
let mut version_parts = comrak::version().split('.').collect::<Vec<&str>>(); | ||
version_parts.pop(); | ||
write!(content, "\"{}\"", version_parts.join(".")).unwrap(); | ||
ncb.literal = content; | ||
} | ||
let mut in_msrv = false; | ||
for node in doc.descendants() { | ||
match node.data.borrow_mut().value { | ||
NodeValue::CodeBlock(ref mut ncb) => { | ||
// Look for the Cargo.toml example block. | ||
if ncb.info == "toml" && ncb.literal.starts_with(DEPENDENCIES) { | ||
let mut content = DEPENDENCIES.to_string(); | ||
let mut version_parts = comrak::version().split('.').collect::<Vec<&str>>(); | ||
version_parts.pop(); | ||
write!(content, "\"{}\"", version_parts.join(".")).unwrap(); | ||
ncb.literal = content; | ||
} | ||
|
||
// Look for a console code block whose contents starts with the HELP string. | ||
// Replace its contents with the same string and the actual command output. | ||
if ncb.info == "console" && ncb.literal.starts_with(HELP) { | ||
let mut content = HELP.to_string(); | ||
let mut cmd = std::process::Command::new("cargo"); | ||
content.push_str( | ||
str::from_utf8( | ||
&cmd.args(["run", "--all-features", "--", "--help"]) | ||
.output() | ||
.unwrap() | ||
.stdout, | ||
) | ||
.unwrap(), | ||
); | ||
ncb.literal = content; | ||
// Look for a console code block whose contents starts with the HELP string. | ||
// Replace its contents with the same string and the actual command output. | ||
if ncb.info == "console" && ncb.literal.starts_with(HELP) { | ||
let mut content = HELP.to_string(); | ||
let mut cmd = std::process::Command::new("cargo"); | ||
content.push_str( | ||
str::from_utf8( | ||
&cmd.args(["run", "--all-features", "--", "--help"]) | ||
.output() | ||
.unwrap() | ||
.stdout, | ||
) | ||
.unwrap(), | ||
); | ||
ncb.literal = content; | ||
} | ||
} | ||
NodeValue::HtmlInline(ref mut s) => { | ||
if s == "<span class=\"msrv\">" { | ||
in_msrv = true; | ||
} else if in_msrv && s == "</span>" { | ||
in_msrv = false; | ||
} | ||
} | ||
NodeValue::Text(ref mut t) => { | ||
if in_msrv { | ||
std::mem::swap(t, &mut msrv.to_string()); | ||
} | ||
} | ||
_ => {} | ||
} | ||
}); | ||
|
||
let mut out = vec![]; | ||
format_commonmark(doc, &Options::default(), &mut out).unwrap(); | ||
} | ||
|
||
std::fs::write("README.md", &out)?; | ||
format_commonmark(doc, &Options::default(), &mut inp.writer())?; | ||
inp.save()?; | ||
|
||
Ok(()) | ||
} |