Skip to content

Commit 2c3a1c2

Browse files
committed
Remove fn parse() from public API
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)?;
1 parent 450e8bb commit 2c3a1c2

File tree

2 files changed

+16
-23
lines changed

2 files changed

+16
-23
lines changed

src/lib.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -2370,7 +2370,8 @@ pub mod config {
23702370
wrap_links: self.wrap_links,
23712371
}
23722372
}
2373-
/// Parse with context.
2373+
2374+
/// Reads and parses HTML from `input` and prepares a render tree.
23742375
pub fn do_parse<R: io::Read>(&self, input: R) -> Result<RenderTree> {
23752376
let doc = plain().parse_html(input)?;
23762377
self.dom_to_render_tree(&doc)
@@ -2660,11 +2661,6 @@ impl<D: TextDecorator> RenderedText<D> {
26602661
}
26612662
}
26622663

2663-
/// Reads and parses HTML from `input` and prepares a render tree.
2664-
pub fn parse(input: impl io::Read) -> Result<RenderTree> {
2665-
config::plain().do_parse(input)
2666-
}
2667-
26682664
/// Reads HTML from `input`, decorates it using `decorator`, and
26692665
/// returns a `String` with text wrapped to `width` columns.
26702666
pub fn from_read_with_decorator<R, D>(input: R, width: usize, decorator: D) -> Result<String>

src/tests.rs

+14-17
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{config, Error};
55
#[cfg(feature = "css")]
66
use super::render::text_renderer::RichDecorator;
77
use super::render::text_renderer::{RichAnnotation, TaggedLine, TrivialDecorator};
8-
use super::{from_read, from_read_with_decorator, parse, TextDecorator};
8+
use super::{from_read, from_read_with_decorator, TextDecorator};
99

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

13621362
#[test]
13631363
fn test_multi_parse() {
1364+
let cfg = config::plain();
13641365
let html: &[u8] = b"one two three four five six seven eight nine ten eleven twelve thirteen \
13651366
fourteen fifteen sixteen seventeen";
1366-
let tree = parse(html).unwrap();
1367+
let tree = cfg.do_parse(html).unwrap();
13671368
assert_eq!(
13681369
"one two three four five six seven eight nine ten eleven twelve thirteen fourteen\n\
13691370
fifteen sixteen seventeen\n",
1370-
config::plain().render_to_string(tree.clone(), 80).unwrap()
1371+
cfg.render_to_string(tree.clone(), 80).unwrap()
13711372
);
13721373
assert_eq!(
13731374
"one two three four five six seven eight nine ten eleven twelve\n\
13741375
thirteen fourteen fifteen sixteen seventeen\n",
1375-
config::plain().render_to_string(tree.clone(), 70).unwrap()
1376+
cfg.render_to_string(tree.clone(), 70).unwrap()
13761377
);
13771378
assert_eq!(
13781379
"one two three four five six seven eight nine ten\n\
13791380
eleven twelve thirteen fourteen fifteen sixteen\n\
13801381
seventeen\n",
1381-
config::plain().render_to_string(tree.clone(), 50).unwrap()
1382+
cfg.render_to_string(tree.clone(), 50).unwrap()
13821383
);
13831384
}
13841385

13851386
#[test]
13861387
fn test_read_rich() {
13871388
let html: &[u8] = b"<strong>bold</strong>";
1388-
let lines = config::rich()
1389-
.render_to_lines(parse(html).unwrap(), 80)
1390-
.unwrap();
1389+
let lines = config::rich().lines_from_read(html, 80).unwrap();
13911390
let tag = vec![RichAnnotation::Strong];
13921391
let line = TaggedLine::from_string("*bold*".to_owned(), &tag);
13931392
assert_eq!(vec![line], lines);
@@ -1397,7 +1396,7 @@ fn test_read_rich() {
13971396
fn test_read_custom() {
13981397
let html: &[u8] = b"<strong>bold</strong>";
13991398
let lines = config::with_decorator(TrivialDecorator::new())
1400-
.render_to_lines(parse(html).unwrap(), 80)
1399+
.lines_from_read(html, 80)
14011400
.unwrap();
14021401
let tag = vec![()];
14031402
let line = TaggedLine::from_string("bold".to_owned(), &tag);
@@ -1409,7 +1408,7 @@ fn test_pre_rich() {
14091408
use RichAnnotation::*;
14101409
assert_eq!(
14111410
config::rich()
1412-
.render_to_lines(parse(&b"<pre>test</pre>"[..]).unwrap(), 100)
1411+
.lines_from_read(&b"<pre>test</pre>"[..], 100)
14131412
.unwrap(),
14141413
[TaggedLine::from_string(
14151414
"test".into(),
@@ -1419,7 +1418,7 @@ fn test_pre_rich() {
14191418

14201419
assert_eq!(
14211420
config::rich()
1422-
.render_to_lines(crate::parse("<pre>testlong</pre>".as_bytes()).unwrap(), 4)
1421+
.lines_from_read("<pre>testlong</pre>".as_bytes(), 4)
14231422
.unwrap(),
14241423
[
14251424
TaggedLine::from_string("test".into(), &vec![Preformat(false)]),
@@ -1431,10 +1430,7 @@ fn test_pre_rich() {
14311430
// tags.
14321431
assert_eq!(
14331432
config::rich()
1434-
.render_to_lines(
1435-
crate::parse(r#"<p style="white-space: pre">testlong</p>"#.as_bytes()).unwrap(),
1436-
4
1437-
)
1433+
.lines_from_read(r#"<p style="white-space: pre">testlong</p>"#.as_bytes(), 4)
14381434
.unwrap(),
14391435
[
14401436
TaggedLine::from_string("test".into(), &vec![]),
@@ -1931,8 +1927,9 @@ fn test_issue_93_x() {
19311927
114, 104, 60, 47, 101, 109, 62, 60, 99, 99, 172, 97, 97, 58, 60, 119, 99, 64, 126, 118,
19321928
104, 100, 100, 107, 105, 60, 120, 98, 255, 255, 255, 0, 60, 255, 127, 46, 60, 113, 127,
19331929
];
1934-
let _local0 = crate::parse(&data[..]).unwrap();
1935-
let _local1 = config::with_decorator(TrivialDecorator::new()).render_to_string(_local0, 1);
1930+
let cfg = config::with_decorator(TrivialDecorator::new());
1931+
let _local0 = cfg.do_parse(&data[..]).unwrap();
1932+
let _local1 = cfg.render_to_string(_local0, 1);
19361933
}
19371934

19381935
#[test]

0 commit comments

Comments
 (0)