summaryrefslogtreecommitdiffstats
path: root/vendor/pulldown-cmark/examples
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /vendor/pulldown-cmark/examples
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/pulldown-cmark/examples')
-rw-r--r--vendor/pulldown-cmark/examples/broken-link-callbacks.rs35
-rw-r--r--vendor/pulldown-cmark/examples/event-filter.rs27
-rw-r--r--vendor/pulldown-cmark/examples/string-to-string.rs24
3 files changed, 86 insertions, 0 deletions
diff --git a/vendor/pulldown-cmark/examples/broken-link-callbacks.rs b/vendor/pulldown-cmark/examples/broken-link-callbacks.rs
new file mode 100644
index 000000000..268715dc3
--- /dev/null
+++ b/vendor/pulldown-cmark/examples/broken-link-callbacks.rs
@@ -0,0 +1,35 @@
+use pulldown_cmark::{html, BrokenLink, Options, Parser};
+
+fn main() {
+ let input: &str = "Hello world, check out [my website][].";
+ println!("Parsing the following markdown string:\n{}", input);
+
+ // Setup callback that sets the URL and title when it encounters
+ // a reference to our home page.
+ let callback = &mut |broken_link: BrokenLink| {
+ if broken_link.reference.as_ref() == "my website" {
+ println!(
+ "Replacing the markdown `{}` of type {:?} with a working link",
+ &input[broken_link.span], broken_link.link_type,
+ );
+ Some(("http://example.com".into(), "my example website".into()))
+ } else {
+ None
+ }
+ };
+
+ // Create a parser with our callback function for broken links.
+ let parser = Parser::new_with_broken_link_callback(input, Options::empty(), Some(callback));
+
+ // Write to String buffer.
+ let mut html_output: String = String::with_capacity(input.len() * 3 / 2);
+ html::push_html(&mut html_output, parser);
+
+ // Check that the output is what we expected.
+ let expected_html: &str =
+ "<p>Hello world, check out <a href=\"http://example.com\" title=\"my example website\">my website</a>.</p>\n";
+ assert_eq!(expected_html, &html_output);
+
+ // Write result to stdout.
+ println!("\nHTML output:\n{}", &html_output);
+}
diff --git a/vendor/pulldown-cmark/examples/event-filter.rs b/vendor/pulldown-cmark/examples/event-filter.rs
new file mode 100644
index 000000000..eb33e56cf
--- /dev/null
+++ b/vendor/pulldown-cmark/examples/event-filter.rs
@@ -0,0 +1,27 @@
+use std::io::Write as _;
+
+use pulldown_cmark::{html, Event, Options, Parser, Tag};
+
+fn main() {
+ let markdown_input: &str = "This is Peter on ![holiday in Greece](pearl_beach.jpg).";
+ println!("Parsing the following markdown string:\n{}", markdown_input);
+
+ // Set up parser. We can treat is as any other iterator. We replace Peter by John
+ // and image by its alt text.
+ let parser = Parser::new_ext(markdown_input, Options::empty())
+ .map(|event| match event {
+ Event::Text(text) => Event::Text(text.replace("Peter", "John").into()),
+ _ => event,
+ })
+ .filter(|event| match event {
+ Event::Start(Tag::Image(..)) | Event::End(Tag::Image(..)) => false,
+ _ => true,
+ });
+
+ // Write to anything implementing the `Write` trait. This could also be a file
+ // or network socket.
+ let stdout = std::io::stdout();
+ let mut handle = stdout.lock();
+ handle.write_all(b"\nHTML output:\n").unwrap();
+ html::write_html(&mut handle, parser).unwrap();
+}
diff --git a/vendor/pulldown-cmark/examples/string-to-string.rs b/vendor/pulldown-cmark/examples/string-to-string.rs
new file mode 100644
index 000000000..b71bea786
--- /dev/null
+++ b/vendor/pulldown-cmark/examples/string-to-string.rs
@@ -0,0 +1,24 @@
+use pulldown_cmark::{html, Options, Parser};
+
+fn main() {
+ let markdown_input: &str = "Hello world, this is a ~~complicated~~ *very simple* example.";
+ println!("Parsing the following markdown string:\n{}", markdown_input);
+
+ // Set up options and parser. Strikethroughs are not part of the CommonMark standard
+ // and we therefore must enable it explicitly.
+ let mut options = Options::empty();
+ options.insert(Options::ENABLE_STRIKETHROUGH);
+ let parser = Parser::new_ext(markdown_input, options);
+
+ // Write to String buffer.
+ let mut html_output: String = String::with_capacity(markdown_input.len() * 3 / 2);
+ html::push_html(&mut html_output, parser);
+
+ // Check that the output is what we expected.
+ let expected_html: &str =
+ "<p>Hello world, this is a <del>complicated</del> <em>very simple</em> example.</p>\n";
+ assert_eq!(expected_html, &html_output);
+
+ // Write result to stdout.
+ println!("\nHTML output:\n{}", &html_output);
+}