Skip to content

Commit

Permalink
Merge pull request #202 from kivikakk/update-readme-version
Browse files Browse the repository at this point in the history
update-readme: update the dependency version too
  • Loading branch information
kivikakk authored Dec 6, 2021
2 parents c8edf23 + 3802518 commit 27aa8c8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Specify it as a requirement in `Cargo.toml`:

``` toml
[dependencies]
comrak = "0.10"
comrak = "0.12"
```

Comrak supports Rust stable.
Expand Down
18 changes: 15 additions & 3 deletions examples/update-readme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ extern crate comrak;
use comrak::nodes::{AstNode, NodeValue};
use comrak::{format_commonmark, parse_document, Arena, ComrakOptions};

const DEPENDENCIES: &str = "[dependencies]\ncomrak = ";
const HELP: &str = "$ comrak --help\n";

fn main() -> Result<(), Box<dyn std::error::Error + 'static>> {
Expand All @@ -23,10 +24,21 @@ fn main() -> Result<(), Box<dyn std::error::Error + 'static>> {
}

iter_nodes(doc, &|node| {
// Look for a code block whose contents starts with the HELP string.
// Replace its contents with the same string and the actual command output.
if let NodeValue::CodeBlock(ref mut ncb) = node.data.borrow_mut().value {
if ncb.literal.starts_with(&HELP.as_bytes()) {
// Look for the Cargo.toml example block.
if ncb.info == "toml".as_bytes() && ncb.literal.starts_with(&DEPENDENCIES.as_bytes()) {
let mut content = DEPENDENCIES.as_bytes().to_vec();
let mut version_parts = comrak::version().split('.').collect::<Vec<&str>>();
version_parts.pop();
content.extend("\"".bytes());
content.extend(version_parts.join(".").bytes());
content.extend("\"".bytes());
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".as_bytes() && ncb.literal.starts_with(&HELP.as_bytes()) {
let mut content = HELP.as_bytes().to_vec();
let mut cmd = std::process::Command::new("cargo");
content.extend(cmd.args(&["run", "--", "--help"]).output().unwrap().stdout);
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,8 @@ pub fn markdown_to_html_with_plugins(
format_html_with_plugins(root, options, &mut s, plugins).unwrap();
String::from_utf8(s).unwrap()
}

/// Return the version of the crate.
pub fn version() -> &'static str {
env!("CARGO_PKG_VERSION")
}

0 comments on commit 27aa8c8

Please sign in to comment.