Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config::rich_no_decorate() #195

Merged
merged 3 commits into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ jobs:
override: true

- name: Check semver
uses: obi1kenobi/cargo-semver-checks-action@v1
uses: obi1kenobi/cargo-semver-checks-action@v2
with:
version-tag-prefix: ''
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ Possible log types:

### Latest

### 0.14.0

- [changed] Various small refactors (thanks sftse)
- [added] New `config::rich_no_decorate`, to use annotations without '\*' markers around
bold text etc.

### 0.13.6

- [fixed] Fixed issue parsing CSS rules with known rules but unknown values,
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "html2text"
version = "0.13.6"
version = "0.14.0"
authors = ["Chris Emerson <[email protected]>"]
description = "Render HTML as plain text."
repository = "https://github.com/jugglerchris/rust-html2text/"
Expand Down
15 changes: 14 additions & 1 deletion examples/html2text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ where
#[cfg(unix)]
{
if flags.use_colour {
let conf = config::rich();
let conf = if flags.no_decorate {
config::rich_no_decorate()
} else {
config::rich()
};
let conf = update_config(conf, &flags);
#[cfg(feature = "css")]
let use_css_colours = !flags.ignore_css_colours;
Expand Down Expand Up @@ -163,6 +167,8 @@ struct Flags {
wrap_width: Option<usize>,
#[allow(unused)]
use_colour: bool,
#[allow(unused)]
no_decorate: bool,
#[cfg(feature = "css")]
use_css: bool,
#[cfg(feature = "css")]
Expand All @@ -185,6 +191,7 @@ fn main() {
width: 80,
wrap_width: None,
use_colour: false,
no_decorate: false,
#[cfg(feature = "css")]
use_css: false,
#[cfg(feature = "css")]
Expand Down Expand Up @@ -231,6 +238,12 @@ fn main() {
StoreTrue,
"Use ANSI terminal colours",
);
#[cfg(unix)]
ap.refer(&mut flags.no_decorate).add_option(
&["--no-decorate"],
StoreTrue,
"Skip decorations (with --colour)",
);
#[cfg(feature = "css")]
ap.refer(&mut flags.use_css)
.add_option(&["--css"], StoreTrue, "Enable CSS");
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2590,6 +2590,11 @@ pub mod config {
with_decorator(RichDecorator::new())
}

/// Return a Config initialized with a `RichDecorator` and decoration disabled.
pub fn rich_no_decorate() -> Config<RichDecorator> {
with_decorator(RichDecorator::new_undecorated())
}

/// Return a Config initialized with a `PlainDecorator`.
pub fn plain() -> Config<PlainDecorator> {
with_decorator(PlainDecorator::new())
Expand Down
45 changes: 37 additions & 8 deletions src/render/text_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1865,7 +1865,10 @@ impl TextDecorator for TrivialDecorator {
/// A decorator to generate rich text (styled) rather than
/// pure text output.
#[derive(Clone, Debug)]
pub struct RichDecorator {}
pub struct RichDecorator {
// Don't output decorations around '*bold*' text.
skip_decorations: bool,
}

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

impl RichDecorator {
/// Create a new `RichDecorator`.
/// Create a new `RichDecorator` with the default settings.
#[allow(clippy::new_without_default)]
pub fn new() -> RichDecorator {
RichDecorator {}
RichDecorator {
skip_decorations: false,
}
}

/// Create a new `RichDecorator` which doesn't add decorations
/// when terminal formatting can be used.
pub fn new_undecorated() -> RichDecorator {
RichDecorator {
skip_decorations: true,
}
}
}

Expand All @@ -1923,11 +1936,19 @@ impl TextDecorator for RichDecorator {
}

fn decorate_strong_start(&self) -> (String, Self::Annotation) {
("*".to_string(), RichAnnotation::Strong)
if self.skip_decorations {
("".to_string(), RichAnnotation::Strong)
} else {
("*".to_string(), RichAnnotation::Strong)
}
}

fn decorate_strong_end(&self) -> String {
"*".to_string()
if self.skip_decorations {
"".to_string()
} else {
"*".to_string()
}
}

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

fn decorate_code_start(&self) -> (String, Self::Annotation) {
("`".to_string(), RichAnnotation::Code)
if self.skip_decorations {
("".to_string(), RichAnnotation::Code)
} else {
("`".to_string(), RichAnnotation::Code)
}
}

fn decorate_code_end(&self) -> String {
"`".to_string()
if self.skip_decorations {
"".to_string()
} else {
"`".to_string()
}
}

fn decorate_preformat_first(&self) -> Self::Annotation {
Expand Down Expand Up @@ -1979,7 +2008,7 @@ impl TextDecorator for RichDecorator {
}

fn make_subblock_decorator(&self) -> Self {
RichDecorator::new()
self.clone()
}

fn push_colour(&mut self, colour: Colour) -> Option<Self::Annotation> {
Expand Down
11 changes: 11 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1393,6 +1393,17 @@ fn test_read_rich() {
assert_eq!(vec![line], lines);
}

#[test]
fn test_read_rich_nodecorate() {
let html: &[u8] = b"<strong>bold</strong>";
let lines = config::rich_no_decorate()
.render_to_lines(parse(html).unwrap(), 80)
.unwrap();
let tag = vec![RichAnnotation::Strong];
let line = TaggedLine::from_string("bold".to_owned(), &tag);
assert_eq!(vec![line], lines);
}

#[test]
fn test_read_custom() {
let html: &[u8] = b"<strong>bold</strong>";
Expand Down
Loading