Skip to content

Commit

Permalink
README: finish fixups.
Browse files Browse the repository at this point in the history
  • Loading branch information
kivikakk committed Jul 10, 2024
1 parent edcbf5f commit dc80666
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 42 deletions.
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@ Rust port of [github's `cmark-gfm`](https://github.com/github/cmark-gfm).
Compliant with [CommonMark 0.31.2](https://spec.commonmark.org/0.31.2/) in default mode.
GFM support synced with release `0.29.0.gfm.13`.

- [Installation](#installation)
- [Usage](#usage)
- [Security](#security)
- [Extensions](#extensions)
- [Related projects](#related-projects)
- [Contributing](#contributing)
- [Legal](#legal)

## Installation

Specify it as a requirement in `Cargo.toml`:
Expand Down Expand Up @@ -187,7 +179,6 @@ assert_eq!(markdown_to_html("Hello, **世界**!", &Options::default()),
Or you can parse the input into an AST yourself, manipulate it, and then use your desired formatter:

``` rust
extern crate comrak;
use comrak::nodes::NodeValue;
use comrak::{format_html, parse_document, Arena, Options};

Expand Down Expand Up @@ -219,6 +210,13 @@ fn main() {
let html = replace_text(&doc, &orig, &repl);

println!("{}", html);
// Output:
//
// <p>This is your input.</p>
// <ol>
// <li>Also <a href="#">your</a> input.</li>
// <li>Certainly <em>your</em> input.</li>
// </ol>
}
```

Expand Down
65 changes: 33 additions & 32 deletions examples/sample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,47 @@ fn small() {
}

fn large() {
use comrak::nodes::{AstNode, NodeValue};
use comrak::nodes::NodeValue;
use comrak::{format_html, parse_document, Arena, Options};

// The returned nodes are created in the supplied Arena, and are bound by its lifetime.
let arena = Arena::new();
fn replace_text(document: &str, orig_string: &str, replacement: &str) -> String {
// The returned nodes are created in the supplied Arena, and are bound by its lifetime.
let arena = Arena::new();

let root = parse_document(
&arena,
"This is my input.\n\n1. Also my input.\n2. Certainly my input.\n",
&Options::default(),
);
// Parse the document into a root `AstNode`
let root = parse_document(&arena, document, &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);
// Iterate over all the descendants of root.
for node in root.descendants() {
if let NodeValue::Text(ref mut text) = node.data.borrow_mut().value {
// If the node is a text node, perform the string replacement.
*text = text.replace(orig_string, replacement)
}
}
}

iter_nodes(root, &|node| {
if let NodeValue::Text(ref mut text) = node.data.borrow_mut().value {
let orig = std::mem::take(text);
*text = orig.replace("my", "your");
}
});
let mut html = vec![];
format_html(root, &Options::default(), &mut html).unwrap();

String::from_utf8(html).unwrap()
}

let mut html = vec![];
format_html(root, &Options::default(), &mut html).unwrap();
fn main() {
let doc = "This is my input.\n\n1. Also [my](#) input.\n2. Certainly *my* input.\n";
let orig = "my";
let repl = "your";
let html = replace_text(&doc, &orig, &repl);

println!("{}", html);
// Output:
//
// <p>This is your input.</p>
// <ol>
// <li>Also <a href="#">your</a> input.</li>
// <li>Certainly <em>your</em> input.</li>
// </ol>
}

assert_eq!(
String::from_utf8(html).unwrap(),
"<p>This is your input.</p>\n\
<ol>\n\
<li>Also your input.</li>\n\
<li>Certainly your input.</li>\n\
</ol>\n"
);
main()
}

fn main() {
Expand Down
3 changes: 2 additions & 1 deletion examples/update-readme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use comrak::{format_commonmark, parse_document, Arena, Options};

const DEPENDENCIES: &str = "[dependencies]\ncomrak = ";
const HELP: &str = "$ comrak --help\n";
const HELP_START: &str = "A 100% CommonMark-compatible GitHub Flavored Markdown parser and formatter\n";
const HELP_START: &str =
"A 100% CommonMark-compatible GitHub Flavored Markdown parser and formatter\n";

fn main() -> Result<(), Box<dyn Error>> {
let arena = Arena::new();
Expand Down

0 comments on commit dc80666

Please sign in to comment.