Skip to content

Commit

Permalink
do not reexport writer wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
stepantubanov committed Apr 16, 2024
1 parent 00c9c48 commit cefabf3
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 35 deletions.
23 changes: 11 additions & 12 deletions pulldown-cmark/examples/event-filter.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
use std::io::Write as _;

use pulldown_cmark::{html, Event, IoWriter, Options, Parser, Tag, TagEnd};
use pulldown_cmark::{html, Event, Options, Parser, Tag, TagEnd};

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(TagEnd::Image) => false,
_ => true,
});
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(TagEnd::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(IoWriter(&mut handle), parser).unwrap();
html::write_html_io(&mut handle, parser).unwrap();
}
8 changes: 4 additions & 4 deletions pulldown-cmark/examples/footnote-rewrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;
use std::fmt::Write as _;
use std::io::Write as _;

use pulldown_cmark::{html, CowStr, Event, IoWriter, Options, Parser, Tag, TagEnd};
use pulldown_cmark::{html, CowStr, Event, Options, Parser, Tag, TagEnd};

/// This example shows how to do footnotes as bottom-notes, in the style of GitHub.
fn main() {
Expand Down Expand Up @@ -53,7 +53,7 @@ fn main() {
let stdout = std::io::stdout();
let mut handle = stdout.lock();
handle.write_all(b"\nHTML output:\n").unwrap();
html::write_html(IoWriter(&mut handle), parser).unwrap();
html::write_html_io(&mut handle, parser).unwrap();

// To make the footnotes look right, we need to sort them by their appearance order, not by
// the in-tree order of their actual definitions. Unused items are omitted entirely.
Expand Down Expand Up @@ -88,8 +88,8 @@ fn main() {
handle
.write_all(b"<hr><ol class=\"footnotes-list\">\n")
.unwrap();
html::write_html(
IoWriter(&mut handle),
html::write_html_io(
&mut handle,
footnotes.into_iter().flat_map(|fl| {
// To write backrefs, the name needs kept until the end of the footnote definition.
let mut name = CowStr::from("");
Expand Down
53 changes: 45 additions & 8 deletions pulldown-cmark/src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ use std::collections::HashMap;
use crate::strings::CowStr;
use crate::Event::*;
use crate::{Alignment, BlockQuoteKind, CodeBlockKind, Event, LinkType, Tag, TagEnd};
use pulldown_cmark_escape::{escape_href, escape_html, escape_html_body_text, StrWrite};
use pulldown_cmark_escape::{
escape_href, escape_html, escape_html_body_text, FmtWriter, IoWriter, StrWrite,
};

enum TableState {
Head,
Expand Down Expand Up @@ -512,11 +514,11 @@ pub fn push_html<'a, I>(s: &mut String, iter: I)
where
I: Iterator<Item = Event<'a>>,
{
HtmlWriter::new(iter, s).run().unwrap();
write_html_fmt(s, iter).unwrap()
}

/// Iterate over an `Iterator` of `Event`s, generate HTML for each `Event`, and
/// write it out to a writable stream.
/// write it out to an I/O stream.
///
/// **Note**: using this function with an unbuffered writer like a file or socket
/// will result in poor performance. Wrap these in a
Expand All @@ -526,7 +528,7 @@ where
/// # Examples
///
/// ```
/// use pulldown_cmark::{html, Parser, IoWriter};
/// use pulldown_cmark::{html, Parser};
/// use std::io::Cursor;
///
/// let markdown_str = r#"
Expand All @@ -539,7 +541,7 @@ where
/// let mut bytes = Vec::new();
/// let parser = Parser::new(markdown_str);
///
/// html::write_html(IoWriter(Cursor::new(&mut bytes)), parser);
/// html::write_html_io(Cursor::new(&mut bytes), parser);
///
/// assert_eq!(&String::from_utf8_lossy(&bytes)[..], r#"<h1>hello</h1>
/// <ul>
Expand All @@ -548,10 +550,45 @@ where
/// </ul>
/// "#);
/// ```
pub fn write_html<'a, I, W>(writer: W, iter: I) -> Result<(), W::Error>
pub fn write_html_io<'a, I, W>(writer: W, iter: I) -> std::io::Result<()>
where
I: Iterator<Item = Event<'a>>,
W: StrWrite,
W: std::io::Write,
{
HtmlWriter::new(iter, IoWriter(writer)).run()
}

/// Iterate over an `Iterator` of `Event`s, generate HTML for each `Event`, and
/// write it into Unicode-accepting buffer or stream.
///
/// # Examples
///
/// ```
/// use pulldown_cmark::{html, Parser};
///
/// let markdown_str = r#"
/// hello
/// =====
///
/// * alpha
/// * beta
/// "#;
/// let mut buf = String::new();
/// let parser = Parser::new(markdown_str);
///
/// html::write_html_fmt(&mut buf, parser);
///
/// assert_eq!(buf, r#"<h1>hello</h1>
/// <ul>
/// <li>alpha</li>
/// <li>beta</li>
/// </ul>
/// "#);
/// ```
pub fn write_html_fmt<'a, I, W>(writer: W, iter: I) -> std::fmt::Result
where
I: Iterator<Item = Event<'a>>,
W: std::fmt::Write,
{
HtmlWriter::new(iter, writer).run()
HtmlWriter::new(iter, FmtWriter(writer)).run()
}
1 change: 0 additions & 1 deletion pulldown-cmark/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ pub use crate::parse::{
};
pub use crate::strings::{CowStr, InlineStr};
pub use crate::utils::*;
pub use pulldown_cmark_escape::{FmtWriter, IoWriter, StrWrite};

/// Codeblock kind.
#[derive(Clone, Debug, PartialEq)]
Expand Down
19 changes: 9 additions & 10 deletions pulldown-cmark/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

#![forbid(unsafe_code)]

use pulldown_cmark::{html, BrokenLink, IoWriter, Options, Parser};
use pulldown_cmark::{html, BrokenLink, Options, Parser};

use std::env;
use std::fs::File;
Expand Down Expand Up @@ -94,14 +94,13 @@ pub fn main() -> std::io::Result<()> {
"fail if input file has broken links",
);

let matches =
match opts.parse(&args[1..]) {
Ok(m) => m,
Err(f) => {
eprintln!("{}\n{}", f, opts.usage(&brief(&args[0])));
std::process::exit(1);
}
};
let matches = match opts.parse(&args[1..]) {
Ok(m) => m,
Err(f) => {
eprintln!("{}\n{}", f, opts.usage(&brief(&args[0])));
std::process::exit(1);
}
};
if matches.opt_present("help") {
println!("{}", opts.usage(&brief(&args[0])));
return Ok(());
Expand Down Expand Up @@ -188,5 +187,5 @@ pub fn pulldown_cmark(input: &str, opts: Options, broken_links: &mut Vec<BrokenL
);
let stdio = io::stdout();
let buffer = std::io::BufWriter::with_capacity(1024 * 1024, stdio.lock());
let _ = html::write_html(IoWriter(buffer), &mut p);
let _ = html::write_html_io(buffer, &mut p);
}

0 comments on commit cefabf3

Please sign in to comment.