Skip to content

Commit

Permalink
README: update with MSRV, and update mechanism for doing so.
Browse files Browse the repository at this point in the history
Thanks to @kaesluder and @bioinformatist for various improvements that
are working together here!
  • Loading branch information
kivikakk committed Apr 18, 2024
1 parent 15609f7 commit 56581d7
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 46 deletions.
81 changes: 78 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ in-place = { version = "0.2.0", optional = true }

[dev-dependencies]
ntest = "0.9"
toml = "0.8"

[target.'cfg(not(target_arch="wasm32"))'.dev-dependencies]
propfuzz = "0.0.1"
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Specify it as a requirement in `Cargo.toml`:
comrak = "0.22"
```

Comrak supports Rust stable.
Comrak's library supports Rust <span class="msrv">1.62.1</span>+.

### Mac & Linux Binaries

Expand Down Expand Up @@ -56,6 +56,9 @@ Options:
[default: /Users/kivikakk/.config/comrak/config]

-i, --inplace
To perform an in-place formatting

--hardbreaks
Treat newlines as hard line breaks

Expand Down
97 changes: 55 additions & 42 deletions examples/update-readme.rs
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(())
}

0 comments on commit 56581d7

Please sign in to comment.