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

example improvements #184

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,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"] }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should remove the termion dependency if it's no longer used.


[target.'cfg(unix)'.dev-dependencies]
termion = "4.0"
8 changes: 4 additions & 4 deletions examples/html2term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ mod top {
}
}
}
Key::Char(' ') | Key::PageDown => {
Key::Char(' ') | Key::PageDown | Key::Ctrl('f') => {
// Ideally, move both the cursor and the top
// visible line down by a whole page
doc_y += height;
Expand All @@ -313,17 +313,17 @@ mod top {
// will take care of the rest of the special
// cases.
}
Key::PageUp => {
Key::PageUp | Key::Ctrl('b') => {
// Ideally, move both the cursor and the top
// visible line up by a whole page. But bound
// both at zero.
doc_y = std::cmp::max(doc_y, height) - height;
top_y = std::cmp::max(top_y, height) - height;
}
Key::Home => {
Key::Home | Key::Char('g') => {
doc_y = 0;
}
Key::End => {
Key::End | Key::Char('G') => {
doc_y = max_y;
}
Key::Char('\t') => {}
Expand Down
92 changes: 50 additions & 42 deletions examples/html2text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,88 +7,91 @@ 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,
hyperlinks: 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(_) => {
if !have_explicit_colour {
start.push(format!("{}", Fg(Blue)));
finish.push(format!("{}", Fg(Reset)));
styled = match annotation {
Default => styled,
Link(url) => {
if hyperlinks {
styled.link(url).blue().underline().to_string()
} else if !have_explicit_colour {
styled.blue().underline().to_string()
} else {
styled
}
}
Emphasis => {
start.push(format!("{}", termion::style::Bold));
finish.push(format!("{}", termion::style::Reset));
Image(img) => {
if hyperlinks {
styled.underline().blue().italic().link(img).to_string()
} else if !have_explicit_colour {
styled.yellow().italic().to_string()
} else {
styled
}
}
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()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The text should already be struck out already by this point (using U+336).

} 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 +109,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 All @@ -121,7 +123,7 @@ where
let use_only_css = false;
return conf
.coloured(input, flags.width, move |anns, s| {
default_colour_map(anns, s, use_css_colours, use_only_css)
default_colour_map(anns, s, use_css_colours, use_only_css, flags.hyperlinks)
})
.unwrap();
}
Expand Down Expand Up @@ -173,6 +175,7 @@ struct Flags {
show_render: bool,
#[cfg(feature = "css")]
show_css: bool,
hyperlinks: bool,
}

fn main() {
Expand All @@ -195,6 +198,7 @@ fn main() {
show_render: false,
#[cfg(feature = "css")]
show_css: false,
hyperlinks: false,
};
let mut literal: bool = false;

Expand Down Expand Up @@ -225,7 +229,6 @@ fn main() {
StoreTrue,
"Output only literal text (no decorations)",
);
#[cfg(unix)]
ap.refer(&mut flags.use_colour).add_option(
&["--colour"],
StoreTrue,
Expand Down Expand Up @@ -259,6 +262,11 @@ fn main() {
StoreTrue,
"Show the parsed CSS instead of rendered output",
);
ap.refer(&mut flags.hyperlinks).add_option(
&["--hyperlinks"],
StoreTrue,
"Show clickable, proper hyperlinks",
);
ap.parse_args_or_exit();
}

Expand Down
5 changes: 0 additions & 5 deletions src/render/text_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1174,11 +1174,6 @@ fn filter_text_strikeout(s: &str) -> Option<String> {
let mut result = String::new();
for c in s.chars() {
result.push(c);
if UnicodeWidthChar::width(c).unwrap_or(0) > 0 {
// This is a character with width (not a combining or other character)
// so add a strikethrough combiner.
result.push('\u{336}');
}
}
Some(result)
}
Expand Down