Skip to content

Commit

Permalink
Merge pull request #144 from jugglerchris/add_only_css
Browse files Browse the repository at this point in the history
Add html2text --only-css option.
  • Loading branch information
jugglerchris authored Apr 7, 2024
2 parents 77d8e73 + 7d9b051 commit 89f1a17
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ Possible log types:
- `[fixed]` for any bug fixes.
- `[security]` to invite users to upgrade in case of vulnerabilities.

### Unreleased

- [changed] Updated some dependencies
- [added] The `html2text` example now has `--ignore-css-colour`, which ignores CSS
colour information but still uses `display: none`, for example.
- [added] The `html2text` example now has `--only-css` option, to not use
default colours when CSS colours are being used.

### 0.12.4

- [changed] Update the previous `max-height: 0` to also look at `height: 0` and require
Expand Down
25 changes: 22 additions & 3 deletions examples/html2text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ use std::io::Write;
#[cfg(unix)]
use html2text::render::text_renderer::RichAnnotation;
#[cfg(unix)]
fn default_colour_map(annotations: &[RichAnnotation], s: &str, use_css_colours: bool) -> String {
fn default_colour_map(
annotations: &[RichAnnotation],
s: &str,
use_css_colours: bool,
no_default_colours: bool,
) -> String {
use termion::color::*;
use RichAnnotation::*;
// Explicit CSS colours override any other colours
let mut have_explicit_colour = false;
let mut have_explicit_colour = no_default_colours;
let mut start = Vec::new();
let mut finish = Vec::new();
trace!("default_colour_map: str={s}, annotations={annotations:?}");
Expand Down Expand Up @@ -110,9 +115,13 @@ where
let use_css_colours = !flags.ignore_css_colours;
#[cfg(not(feature = "css"))]
let use_css_colours = false;
#[cfg(feature = "css")]
let use_only_css = flags.use_only_css;
#[cfg(not(feature = "css"))]
let use_only_css = false;
return conf
.coloured(input, flags.width, move |anns, s| {
default_colour_map(anns, s, use_css_colours)
default_colour_map(anns, s, use_css_colours, use_only_css)
})
.unwrap();
}
Expand All @@ -137,6 +146,8 @@ struct Flags {
use_css: bool,
#[cfg(feature = "css")]
ignore_css_colours: bool,
#[cfg(feature = "css")]
use_only_css: bool,
}

fn main() {
Expand All @@ -153,6 +164,8 @@ fn main() {
use_css: false,
#[cfg(feature = "css")]
ignore_css_colours: false,
#[cfg(feature = "css")]
use_only_css: false,
};
let mut literal: bool = false;

Expand Down Expand Up @@ -195,6 +208,12 @@ fn main() {
#[cfg(feature = "css")]
ap.refer(&mut flags.ignore_css_colours)
.add_option(&["--ignore-css-colour"], StoreTrue, "With --css, ignore CSS colour information (still hides elements with e.g. display: none)");
#[cfg(feature = "css")]
ap.refer(&mut flags.use_only_css).add_option(
&["--only-css"],
StoreTrue,
"Don't use default non-CSS colours",
);
ap.parse_args_or_exit();
}

Expand Down

0 comments on commit 89f1a17

Please sign in to comment.