Skip to content

Commit

Permalink
Merge pull request #141 from jugglerchris/add_ignore_css_colour
Browse files Browse the repository at this point in the history
Add --ignore-css-colour option to html2text example.
  • Loading branch information
jugglerchris authored Apr 7, 2024
2 parents c5c49ec + eab35f3 commit 77d8e73
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions examples/html2text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::io::Write;
#[cfg(unix)]
use html2text::render::text_renderer::RichAnnotation;
#[cfg(unix)]
fn default_colour_map(annotations: &[RichAnnotation], s: &str) -> String {
fn default_colour_map(annotations: &[RichAnnotation], s: &str, use_css_colours: bool) -> String {
use termion::color::*;
use RichAnnotation::*;
// Explicit CSS colours override any other colours
Expand Down Expand Up @@ -60,13 +60,17 @@ fn default_colour_map(annotations: &[RichAnnotation], s: &str) -> String {
}
}
Colour(c) => {
start.push(format!("{}", Fg(Rgb(c.r, c.g, c.b))));
finish.push(format!("{}", Fg(Reset)));
have_explicit_colour = true;
if use_css_colours {
start.push(format!("{}", Fg(Rgb(c.r, c.g, c.b))));
finish.push(format!("{}", Fg(Reset)));
have_explicit_colour = true;
}
}
BgColour(c) => {
start.push(format!("{}", Bg(Rgb(c.r, c.g, c.b))));
finish.push(format!("{}", Bg(Reset)));
if use_css_colours {
start.push(format!("{}", Bg(Rgb(c.r, c.g, c.b))));
finish.push(format!("{}", Bg(Reset)));
}
}
_ => {}
}
Expand Down Expand Up @@ -102,8 +106,14 @@ where
if flags.use_colour {
let conf = config::rich();
let conf = update_config(conf, &flags);
#[cfg(feature = "css")]
let use_css_colours = !flags.ignore_css_colours;
#[cfg(not(feature = "css"))]
let use_css_colours = false;
return conf
.coloured(input, flags.width, default_colour_map)
.coloured(input, flags.width, move |anns, s| {
default_colour_map(anns, s, use_css_colours)
})
.unwrap();
}
}
Expand All @@ -125,6 +135,8 @@ struct Flags {
use_colour: bool,
#[cfg(feature = "css")]
use_css: bool,
#[cfg(feature = "css")]
ignore_css_colours: bool,
}

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

Expand Down Expand Up @@ -178,6 +192,9 @@ fn main() {
#[cfg(feature = "css")]
ap.refer(&mut flags.use_css)
.add_option(&["--css"], StoreTrue, "Enable CSS");
#[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)");
ap.parse_args_or_exit();
}

Expand Down

0 comments on commit 77d8e73

Please sign in to comment.