Skip to content

Commit

Permalink
Updates ready for release.
Browse files Browse the repository at this point in the history
Remove ansi_colour feature - the function is available all the time now.
  • Loading branch information
jugglerchris committed Dec 15, 2023
1 parent bc7636e commit 42fe364
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Possible log types:
- [fixed] Fixed #88: panic when a width of zero passed in (thanks bingen13)
- [fixed] Fixed #90: Fixed a divide-by-zero panic with colspan=0 (thanks mtorromeo)
- [added] Add very basic CSS colour support (under the css feature flag)
- [changed] Removed ansi\_colours feature (from\_read\_coloured is always available)
- [changed] Overhauled error handling. Internally (and in the lower level
API) errors (mainly "TooNarrow") are passed around with `Result`. Fixed
some panics and infinite loops. (Thanks WIZeaz for fuzzing)
Expand Down
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ lightningcss = { version = "1.0.0-alpha.51", optional=true }
html_trace = []
html_trace_bt = ["backtrace"]
default = []
ansi_colours = []
css = ["dep:lightningcss"]

[[example]]
Expand Down
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,27 @@ read keys from stdin.

## Cargo Features


|Feature| Description|
|-------|------------|
|css | Limited handling of CSS, adding Coloured nodes to the render tree. |
|html\_trace| Add verbose internal logging (not recommended) |
|html\_trace\_bt| Add backtraces to the verbose internal logging |

### CSS support

When the `css` feature is enabled, some simple CSS handling is done.

* The contents of \<style\> elements are parsed and some colour rules are extracted
* Some simplified selector matching is done: currently `<span class="foo">` with
CSS rules similar to `.foo { color:#123456; }`. This will add `Coloured(...)` nodes
to the render tree when matching.

The CSS handling is expected to improve in future (PRs welcome), but not to a full-
blown browser style system, which would be overkill for terminal output.

There are two ways to make use of the colours:
* Use `from_read_rich()` or one of its variants. One of the annotations you may get
back is `Colour(..)`.
* Use `from_read_coloured()`. This is similar to `from_read()`, but you provide
a function to add terminal colours (or other styling) based on the same
RichAnnotations. See examples/html2text.rs for an example using termion.
13 changes: 3 additions & 10 deletions examples/html2text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ use argparse::{ArgumentParser, Store, StoreOption, StoreTrue};
use std::io;
use std::io::Write;

#[cfg(feature = "ansi_colours")]
use html2text::render::text_renderer::RichAnnotation;
#[cfg(feature = "ansi_colours")]
use termion;

#[cfg(feature = "ansi_colours")]
fn default_colour_map(annotations: &[RichAnnotation], s: &str) -> String {
use termion::color::*;
use RichAnnotation::*;
Expand Down Expand Up @@ -76,15 +73,12 @@ fn default_colour_map(annotations: &[RichAnnotation], s: &str) -> String {
result
}

fn translate<R>(input: R, width: usize, literal: bool, _use_colour: bool) -> String
fn translate<R>(input: R, width: usize, literal: bool, use_colour: bool) -> String
where
R: io::Read,
{
#[cfg(feature = "ansi_colours")]
{
if _use_colour {
return html2text::from_read_coloured(input, width, default_colour_map).unwrap();
};
if use_colour {
return html2text::from_read_coloured(input, width, default_colour_map).unwrap();
}
if literal {
let decorator = html2text::render::text_renderer::TrivialDecorator::new();
Expand Down Expand Up @@ -124,7 +118,6 @@ fn main() {
StoreTrue,
"Output only literal text (no decorations)",
);
#[cfg(feature = "ansi_colours")]
ap.refer(&mut use_colour)
.add_option(&["--colour"], StoreTrue, "Use ANSI terminal colours");
ap.parse_args_or_exit();
Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1850,10 +1850,8 @@ where
.expect("Failed to convert to HTML")
}

#[cfg(feature = "ansi_colours")]
mod ansi_colours;

#[cfg(feature = "ansi_colours")]
pub use ansi_colours::from_read_coloured;

#[cfg(test)]
Expand Down

0 comments on commit 42fe364

Please sign in to comment.