Skip to content

Commit 2a5a693

Browse files
authored
Merge pull request #195 from jugglerchris/rich_no_decorate
Add config::rich_no_decorate()
2 parents b145707 + 64af8c9 commit 2a5a693

File tree

7 files changed

+75
-11
lines changed

7 files changed

+75
-11
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ jobs:
2525
override: true
2626

2727
- name: Check semver
28-
uses: obi1kenobi/cargo-semver-checks-action@v1
28+
uses: obi1kenobi/cargo-semver-checks-action@v2
2929
with:
3030
version-tag-prefix: ''

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ Possible log types:
1111

1212
### Latest
1313

14+
### 0.14.0
15+
16+
- [changed] Various small refactors (thanks sftse)
17+
- [added] New `config::rich_no_decorate`, to use annotations without '\*' markers around
18+
bold text etc.
19+
1420
### 0.13.6
1521

1622
- [fixed] Fixed issue parsing CSS rules with known rules but unknown values,

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "html2text"
3-
version = "0.13.6"
3+
version = "0.14.0"
44
authors = ["Chris Emerson <[email protected]>"]
55
description = "Render HTML as plain text."
66
repository = "https://github.com/jugglerchris/rust-html2text/"

examples/html2text.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,11 @@ where
109109
#[cfg(unix)]
110110
{
111111
if flags.use_colour {
112-
let conf = config::rich();
112+
let conf = if flags.no_decorate {
113+
config::rich_no_decorate()
114+
} else {
115+
config::rich()
116+
};
113117
let conf = update_config(conf, &flags);
114118
#[cfg(feature = "css")]
115119
let use_css_colours = !flags.ignore_css_colours;
@@ -163,6 +167,8 @@ struct Flags {
163167
wrap_width: Option<usize>,
164168
#[allow(unused)]
165169
use_colour: bool,
170+
#[allow(unused)]
171+
no_decorate: bool,
166172
#[cfg(feature = "css")]
167173
use_css: bool,
168174
#[cfg(feature = "css")]
@@ -185,6 +191,7 @@ fn main() {
185191
width: 80,
186192
wrap_width: None,
187193
use_colour: false,
194+
no_decorate: false,
188195
#[cfg(feature = "css")]
189196
use_css: false,
190197
#[cfg(feature = "css")]
@@ -231,6 +238,12 @@ fn main() {
231238
StoreTrue,
232239
"Use ANSI terminal colours",
233240
);
241+
#[cfg(unix)]
242+
ap.refer(&mut flags.no_decorate).add_option(
243+
&["--no-decorate"],
244+
StoreTrue,
245+
"Skip decorations (with --colour)",
246+
);
234247
#[cfg(feature = "css")]
235248
ap.refer(&mut flags.use_css)
236249
.add_option(&["--css"], StoreTrue, "Enable CSS");

src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -2590,6 +2590,11 @@ pub mod config {
25902590
with_decorator(RichDecorator::new())
25912591
}
25922592

2593+
/// Return a Config initialized with a `RichDecorator` and decoration disabled.
2594+
pub fn rich_no_decorate() -> Config<RichDecorator> {
2595+
with_decorator(RichDecorator::new_undecorated())
2596+
}
2597+
25932598
/// Return a Config initialized with a `PlainDecorator`.
25942599
pub fn plain() -> Config<PlainDecorator> {
25952600
with_decorator(PlainDecorator::new())

src/render/text_renderer.rs

+37-8
Original file line numberDiff line numberDiff line change
@@ -1865,7 +1865,10 @@ impl TextDecorator for TrivialDecorator {
18651865
/// A decorator to generate rich text (styled) rather than
18661866
/// pure text output.
18671867
#[derive(Clone, Debug)]
1868-
pub struct RichDecorator {}
1868+
pub struct RichDecorator {
1869+
// Don't output decorations around '*bold*' text.
1870+
skip_decorations: bool,
1871+
}
18691872

18701873
/// Annotation type for "rich" text. Text is associated with a set of
18711874
/// these.
@@ -1896,10 +1899,20 @@ pub enum RichAnnotation {
18961899
}
18971900

18981901
impl RichDecorator {
1899-
/// Create a new `RichDecorator`.
1902+
/// Create a new `RichDecorator` with the default settings.
19001903
#[allow(clippy::new_without_default)]
19011904
pub fn new() -> RichDecorator {
1902-
RichDecorator {}
1905+
RichDecorator {
1906+
skip_decorations: false,
1907+
}
1908+
}
1909+
1910+
/// Create a new `RichDecorator` which doesn't add decorations
1911+
/// when terminal formatting can be used.
1912+
pub fn new_undecorated() -> RichDecorator {
1913+
RichDecorator {
1914+
skip_decorations: true,
1915+
}
19031916
}
19041917
}
19051918

@@ -1923,11 +1936,19 @@ impl TextDecorator for RichDecorator {
19231936
}
19241937

19251938
fn decorate_strong_start(&self) -> (String, Self::Annotation) {
1926-
("*".to_string(), RichAnnotation::Strong)
1939+
if self.skip_decorations {
1940+
("".to_string(), RichAnnotation::Strong)
1941+
} else {
1942+
("*".to_string(), RichAnnotation::Strong)
1943+
}
19271944
}
19281945

19291946
fn decorate_strong_end(&self) -> String {
1930-
"*".to_string()
1947+
if self.skip_decorations {
1948+
"".to_string()
1949+
} else {
1950+
"*".to_string()
1951+
}
19311952
}
19321953

19331954
fn decorate_strikeout_start(&self) -> (String, Self::Annotation) {
@@ -1939,11 +1960,19 @@ impl TextDecorator for RichDecorator {
19391960
}
19401961

19411962
fn decorate_code_start(&self) -> (String, Self::Annotation) {
1942-
("`".to_string(), RichAnnotation::Code)
1963+
if self.skip_decorations {
1964+
("".to_string(), RichAnnotation::Code)
1965+
} else {
1966+
("`".to_string(), RichAnnotation::Code)
1967+
}
19431968
}
19441969

19451970
fn decorate_code_end(&self) -> String {
1946-
"`".to_string()
1971+
if self.skip_decorations {
1972+
"".to_string()
1973+
} else {
1974+
"`".to_string()
1975+
}
19471976
}
19481977

19491978
fn decorate_preformat_first(&self) -> Self::Annotation {
@@ -1979,7 +2008,7 @@ impl TextDecorator for RichDecorator {
19792008
}
19802009

19812010
fn make_subblock_decorator(&self) -> Self {
1982-
RichDecorator::new()
2011+
self.clone()
19832012
}
19842013

19852014
fn push_colour(&mut self, colour: Colour) -> Option<Self::Annotation> {

src/tests.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1393,6 +1393,17 @@ fn test_read_rich() {
13931393
assert_eq!(vec![line], lines);
13941394
}
13951395

1396+
#[test]
1397+
fn test_read_rich_nodecorate() {
1398+
let html: &[u8] = b"<strong>bold</strong>";
1399+
let lines = config::rich_no_decorate()
1400+
.render_to_lines(parse(html).unwrap(), 80)
1401+
.unwrap();
1402+
let tag = vec![RichAnnotation::Strong];
1403+
let line = TaggedLine::from_string("bold".to_owned(), &tag);
1404+
assert_eq!(vec![line], lines);
1405+
}
1406+
13961407
#[test]
13971408
fn test_read_custom() {
13981409
let html: &[u8] = b"<strong>bold</strong>";

0 commit comments

Comments
 (0)