Skip to content

Commit

Permalink
Remove fn parse() from public API
Browse files Browse the repository at this point in the history
The parse function is an odd one in the current API, other functions
can be separated cleanly into functions that are convenient and functions
that need to be configured. The parse function does not advertise what
configuration it uses, and will silently drop css information, as it parses
with config::plain() as default configuration. This can lead to the following
misuse of the public API

let render_tree = parse(html)?;
customcfg.render_to_string(render_tree, 80)?;
  • Loading branch information
sftse committed Dec 12, 2024
1 parent 450e8bb commit 2c3a1c2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 23 deletions.
8 changes: 2 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2370,7 +2370,8 @@ pub mod config {
wrap_links: self.wrap_links,
}
}
/// Parse with context.

/// Reads and parses HTML from `input` and prepares a render tree.
pub fn do_parse<R: io::Read>(&self, input: R) -> Result<RenderTree> {
let doc = plain().parse_html(input)?;
self.dom_to_render_tree(&doc)
Expand Down Expand Up @@ -2660,11 +2661,6 @@ impl<D: TextDecorator> RenderedText<D> {
}
}

/// Reads and parses HTML from `input` and prepares a render tree.
pub fn parse(input: impl io::Read) -> Result<RenderTree> {
config::plain().do_parse(input)
}

/// Reads HTML from `input`, decorates it using `decorator`, and
/// returns a `String` with text wrapped to `width` columns.
pub fn from_read_with_decorator<R, D>(input: R, width: usize, decorator: D) -> Result<String>
Expand Down
31 changes: 14 additions & 17 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{config, Error};
#[cfg(feature = "css")]
use super::render::text_renderer::RichDecorator;
use super::render::text_renderer::{RichAnnotation, TaggedLine, TrivialDecorator};
use super::{from_read, from_read_with_decorator, parse, TextDecorator};
use super::{from_read, from_read_with_decorator, TextDecorator};

/// Like assert_eq!(), but prints out the results normally as well
macro_rules! assert_eq_str {
Expand Down Expand Up @@ -1361,33 +1361,32 @@ fn test_s() {

#[test]
fn test_multi_parse() {
let cfg = config::plain();
let html: &[u8] = b"one two three four five six seven eight nine ten eleven twelve thirteen \
fourteen fifteen sixteen seventeen";
let tree = parse(html).unwrap();
let tree = cfg.do_parse(html).unwrap();
assert_eq!(
"one two three four five six seven eight nine ten eleven twelve thirteen fourteen\n\
fifteen sixteen seventeen\n",
config::plain().render_to_string(tree.clone(), 80).unwrap()
cfg.render_to_string(tree.clone(), 80).unwrap()
);
assert_eq!(
"one two three four five six seven eight nine ten eleven twelve\n\
thirteen fourteen fifteen sixteen seventeen\n",
config::plain().render_to_string(tree.clone(), 70).unwrap()
cfg.render_to_string(tree.clone(), 70).unwrap()
);
assert_eq!(
"one two three four five six seven eight nine ten\n\
eleven twelve thirteen fourteen fifteen sixteen\n\
seventeen\n",
config::plain().render_to_string(tree.clone(), 50).unwrap()
cfg.render_to_string(tree.clone(), 50).unwrap()
);
}

#[test]
fn test_read_rich() {
let html: &[u8] = b"<strong>bold</strong>";
let lines = config::rich()
.render_to_lines(parse(html).unwrap(), 80)
.unwrap();
let lines = config::rich().lines_from_read(html, 80).unwrap();
let tag = vec![RichAnnotation::Strong];
let line = TaggedLine::from_string("*bold*".to_owned(), &tag);
assert_eq!(vec![line], lines);
Expand All @@ -1397,7 +1396,7 @@ fn test_read_rich() {
fn test_read_custom() {
let html: &[u8] = b"<strong>bold</strong>";
let lines = config::with_decorator(TrivialDecorator::new())
.render_to_lines(parse(html).unwrap(), 80)
.lines_from_read(html, 80)
.unwrap();
let tag = vec![()];
let line = TaggedLine::from_string("bold".to_owned(), &tag);
Expand All @@ -1409,7 +1408,7 @@ fn test_pre_rich() {
use RichAnnotation::*;
assert_eq!(
config::rich()
.render_to_lines(parse(&b"<pre>test</pre>"[..]).unwrap(), 100)
.lines_from_read(&b"<pre>test</pre>"[..], 100)
.unwrap(),
[TaggedLine::from_string(
"test".into(),
Expand All @@ -1419,7 +1418,7 @@ fn test_pre_rich() {

assert_eq!(
config::rich()
.render_to_lines(crate::parse("<pre>testlong</pre>".as_bytes()).unwrap(), 4)
.lines_from_read("<pre>testlong</pre>".as_bytes(), 4)
.unwrap(),
[
TaggedLine::from_string("test".into(), &vec![Preformat(false)]),
Expand All @@ -1431,10 +1430,7 @@ fn test_pre_rich() {
// tags.
assert_eq!(
config::rich()
.render_to_lines(
crate::parse(r#"<p style="white-space: pre">testlong</p>"#.as_bytes()).unwrap(),
4
)
.lines_from_read(r#"<p style="white-space: pre">testlong</p>"#.as_bytes(), 4)
.unwrap(),
[
TaggedLine::from_string("test".into(), &vec![]),
Expand Down Expand Up @@ -1931,8 +1927,9 @@ fn test_issue_93_x() {
114, 104, 60, 47, 101, 109, 62, 60, 99, 99, 172, 97, 97, 58, 60, 119, 99, 64, 126, 118,
104, 100, 100, 107, 105, 60, 120, 98, 255, 255, 255, 0, 60, 255, 127, 46, 60, 113, 127,
];
let _local0 = crate::parse(&data[..]).unwrap();
let _local1 = config::with_decorator(TrivialDecorator::new()).render_to_string(_local0, 1);
let cfg = config::with_decorator(TrivialDecorator::new());
let _local0 = cfg.do_parse(&data[..]).unwrap();
let _local1 = cfg.render_to_string(_local0, 1);
}

#[test]
Expand Down

0 comments on commit 2c3a1c2

Please sign in to comment.