Skip to content

Commit

Permalink
fix(examples/html2text): use yansi instead of termion for colouring
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeromos Kovács committed Nov 4, 2024
1 parent 77250cc commit 5ab1d8f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 41 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ path = "examples/html2text.rs"
env_logger = "0.10.1"
argparse = "0.2.2"
log = "0.4.20"
yansi = { version = "1.0.1", features = ["hyperlink"] }

[target.'cfg(unix)'.dev-dependencies]
termion = "4.0"
76 changes: 35 additions & 41 deletions examples/html2text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,88 +7,84 @@ use log::trace;
use std::io;
use std::io::Write;

#[cfg(unix)]
use html2text::render::RichAnnotation;
#[cfg(unix)]
fn default_colour_map(
annotations: &[RichAnnotation],
s: &str,
use_css_colours: bool,
no_default_colours: bool,
) -> String {
use termion::color::*;
use yansi::{hyperlink::HyperlinkExt, Paint};
use RichAnnotation::*;
// Explicit CSS colours override any other colours
let mut have_explicit_colour = no_default_colours;
let mut start = Vec::new();
let mut finish = Vec::new();
let mut styled = s.to_string();
trace!("default_colour_map: str={s}, annotations={annotations:?}");
for annotation in annotations.iter() {
match annotation {
Default => {}
Link(_) => {
start.push(format!("{}", termion::style::Underline));
finish.push(format!("{}", termion::style::Reset));
}
Image(_) => {
styled = match annotation {
Default => styled,
Link(url) => styled.link(url).blue().underline().to_string(),
Image(img) => {
if !have_explicit_colour {
start.push(format!("{}", Fg(Blue)));
finish.push(format!("{}", Fg(Reset)));
format!(
"{} {}",
styled.yellow().italic(),
"img".underline().blue().link(img)
)
} else {
styled
}
}
Emphasis => {
start.push(format!("{}", termion::style::Bold));
finish.push(format!("{}", termion::style::Reset));
}
Emphasis => styled.italic().to_string(),
Strong => {
if !have_explicit_colour {
start.push(format!("{}", Fg(LightYellow)));
finish.push(format!("{}", Fg(Reset)));
styled.bold().to_string()
} else {
styled
}
}
Strikeout => {
if !have_explicit_colour {
start.push(format!("{}", Fg(LightBlack)));
finish.push(format!("{}", Fg(Reset)));
styled.strike().to_string()
} else {
styled
}
}
Code => {
if !have_explicit_colour {
start.push(format!("{}", Fg(Blue)));
finish.push(format!("{}", Fg(Reset)));
styled.blue().to_string()
} else {
styled
}
}
Preformat(_) => {
if !have_explicit_colour {
start.push(format!("{}", Fg(Blue)));
finish.push(format!("{}", Fg(Reset)));
styled.blue().to_string()
} else {
styled
}
}
Colour(c) => {
if use_css_colours {
start.push(format!("{}", Fg(Rgb(c.r, c.g, c.b))));
finish.push(format!("{}", Fg(Reset)));
have_explicit_colour = true;
styled.rgb(c.r, c.g, c.b).to_string()
} else {
styled
}
}
BgColour(c) => {
if use_css_colours {
start.push(format!("{}", Bg(Rgb(c.r, c.g, c.b))));
finish.push(format!("{}", Bg(Reset)));
styled.on_rgb(c.r, c.g, c.b).to_string()
} else {
styled
}
}
_ => {}
_ => styled,
}
}
// Reverse the finish sequences
finish.reverse();
let mut result = start.join("");
result.push_str(s);
for s in finish {
result.push_str(&s);
}
trace!("default_colour_map: output={result}");
result
trace!("default_colour_map: output={styled}");
styled
}

fn update_config<T: TextDecorator>(mut config: Config<T>, flags: &Flags) -> Config<T> {
Expand All @@ -106,7 +102,6 @@ fn translate<R>(input: R, flags: Flags, literal: bool) -> String
where
R: io::Read,
{
#[cfg(unix)]
{
if flags.use_colour {
let conf = config::rich();
Expand Down Expand Up @@ -207,7 +202,6 @@ fn main() {
StoreTrue,
"Output only literal text (no decorations)",
);
#[cfg(unix)]
ap.refer(&mut flags.use_colour).add_option(
&["--colour"],
StoreTrue,
Expand Down

0 comments on commit 5ab1d8f

Please sign in to comment.