Skip to content

Commit

Permalink
Merge pull request #115 from jugglerchris/update_tracing
Browse files Browse the repository at this point in the history
Update tracing and fix ignoring of !important CSS properties.
  • Loading branch information
jugglerchris authored Jan 13, 2024
2 parents 216be3d + c371428 commit e4f2308
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 23 deletions.
8 changes: 4 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ jobs:
- run: cargo --version
- run: cargo build --features=css
- run: cargo test --features=css
build-1-60:
build-1-63:
docker:
- image: cimg/rust:1.60
- image: cimg/rust:1.63
steps:
- checkout
- run: cargo --version
- run: cargo build
- run: cargo test
# - run: cargo test
build-windows:
executor:
name: win/default
Expand Down Expand Up @@ -63,5 +63,5 @@ workflows:
jobs:
- "build-stable"
- "build-css"
- "build-1-60"
- "build-1-63"
- "build-windows"
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ Possible log types:
- `[fixed]` for any bug fixes.
- `[security]` to invite users to upgrade in case of vulnerabilities.

### 0.11.0

- [fixed] CSS: rules marked !important were ignored.
- [changed] html\_trace feature now uses the `log` crate.
- [changed] Bumped MSRV to 1.63 (matching Debian stable) due to some dependencies.

### 0.10.3

- [fixed] A panic on some unlucky text wrapping coincidences.
Expand Down
11 changes: 7 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[package]
name = "html2text"
version = "0.10.3"
version = "0.11.0"
authors = ["Chris Emerson <[email protected]>"]
description = "Render HTML as plain text."
repository = "https://github.com/jugglerchris/rust-html2text/"
readme = "README.md"
documentation = "https://docs.rs/html2text/"
edition = "2021"
rust-version = "1.60"
rust-version = "1.63"
categories = ["text-processing"]

keywords = ["html", "text"]
Expand All @@ -24,10 +24,11 @@ thiserror = "1.0.50"
lightningcss = { version = "1.0.0-alpha.51", optional=true }
# Keep dashmap back; 5.5 has a higher MSRV.
dashmap = "~5.4"
log = { version = "0.4.20", optional = true }

[features]
html_trace = []
html_trace_bt = ["dep:backtrace"]
html_trace = ["dep:log"]
html_trace_bt = ["html_trace", "dep:backtrace"]
default = []
css = ["dep:lightningcss"]

Expand All @@ -40,7 +41,9 @@ name = "html2text"
path = "examples/html2text.rs"

[dev-dependencies]
env_logger = "0.10.1"
argparse = "0.2.2"
log = "0.4.20"

[target.'cfg(unix)'.dev-dependencies]
termion = "2.0"
6 changes: 6 additions & 0 deletions examples/html2text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use html2text::config::{Config, self};
use html2text::render::text_renderer::{TextDecorator, TrivialDecorator};
use std::io;
use std::io::Write;
use log::trace;

#[cfg(unix)]
use html2text::render::text_renderer::RichAnnotation;
Expand All @@ -19,6 +20,7 @@ fn default_colour_map(annotations: &[RichAnnotation], s: &str) -> String {
let mut have_explicit_colour = false;
let mut start = Vec::new();
let mut finish = Vec::new();
trace!("default_colour_map: str={s}, annotations={annotations:?}");
for annotation in annotations.iter() {
match annotation {
Default => {}
Expand Down Expand Up @@ -79,6 +81,7 @@ fn default_colour_map(annotations: &[RichAnnotation], s: &str) -> String {
for s in finish {
result.push_str(&s);
}
trace!("default_colour_map: output={result}");
result
}

Expand Down Expand Up @@ -129,6 +132,9 @@ struct Flags {
}

fn main() {
#[cfg(feature = "html_trace")]
env_logger::init();

let mut infile: Option<String> = None;
let mut outfile: Option<String> = None;
let mut flags = Flags {
Expand Down
25 changes: 16 additions & 9 deletions src/css.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,13 @@ pub struct StyleData {
}

pub(crate) fn parse_style_attribute(text: &str) -> Result<Vec<Style>> {
html_trace_quiet!("Parsing inline style: {text}");
let sattr = StyleAttribute::parse(text, ParserOptions::default())
.map_err(|_| crate::Error::CssParseError)?;

Ok(styles_from_properties(&sattr.declarations))
let styles = styles_from_properties(&sattr.declarations);
html_trace_quiet!("Parsed inline style: {:?}", styles);
Ok(styles)
}

fn is_transparent(color: &CssColor) -> bool {
Expand All @@ -198,7 +201,9 @@ fn is_transparent(color: &CssColor) -> bool {

fn styles_from_properties(decls: &DeclarationBlock<'_>) -> Vec<Style> {
let mut styles = Vec::new();
for decl in &decls.declarations {
html_trace_quiet!("styles:from_properties: {decls:?}");
for decl in decls.declarations.iter().chain(decls.important_declarations.iter()) {
html_trace_quiet!("styles:from_properties: {decl:?}");
match decl {
Property::Color(color) => {
if is_transparent(&color) {
Expand All @@ -224,7 +229,9 @@ fn styles_from_properties(decls: &DeclarationBlock<'_>) -> Vec<Style> {
styles.push(Style::DisplayNone);
}
}
_ => {}
_ => {
html_trace_quiet!("CSS: Unhandled property {:?}", decl);
}
}
}
styles
Expand All @@ -245,12 +252,15 @@ impl StyleData {
for selector in &style.selectors.0 {
match Selector::try_from(selector) {
Ok(selector) => {
self.rules.push(Ruleset {
let ruleset = Ruleset {
selector,
styles: styles.clone()
});
};
html_trace_quiet!("Adding ruleset {ruleset:?}");
self.rules.push(ruleset);
}
Err(_) => {
html_trace!("Ignoring selector {:?}", selector);
continue;
}
}
Expand Down Expand Up @@ -322,7 +332,7 @@ fn combine_vecs(vecs: Vec<Vec<String>>) -> Vec<String> {

fn extract_style_nodes<'a, 'b, T: Write>(
handle: Handle,
err_out: &'b mut T,
_err_out: &'b mut T,
) -> TreeMapResult<'a, (), Handle, Vec<String>> {
use TreeMapResult::*;

Expand All @@ -345,9 +355,7 @@ fn extract_style_nodes<'a, 'b, T: Write>(
Finished(vec![result])
}
_ => {
html_trace!("Unhandled element: {:?}\n", name.local);
pending(handle, |_, cs| Ok(Some(combine_vecs(cs))))
//None
}
}
}
Expand All @@ -356,7 +364,6 @@ fn extract_style_nodes<'a, 'b, T: Write>(
}
_ => {
// NodeData doesn't have a Debug impl.
write!(err_out, "Unhandled node type.\n").unwrap();
Nothing
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ macro_rules! html_trace {
#[cfg(feature = "html_trace_bt")]
{
let bt = ::backtrace::Backtrace::new();
eprintln!( concat!($fmt, " at {:?}"), bt );
log::info!( concat!($fmt, " at {:?}"), bt );
}
#[cfg(not(feature = "html_trace_bt"))]
{
eprintln!($fmt);
log::info!($fmt);
}
};
($fmt:expr, $( $args:expr ),*) => {
#[cfg(feature = "html_trace_bt")]
{
let bt = ::backtrace::Backtrace::new();
eprintln!( concat!($fmt, " at {:?}"), $( $args ),* , bt );
log::info!( concat!($fmt, " at {:?}"), $( $args ),* , bt );
}
#[cfg(not(feature = "html_trace_bt"))]
{
eprintln!($fmt, $( $args ),*);
log::info!($fmt, $( $args ),*);
}
};
}
Expand All @@ -53,10 +53,10 @@ macro_rules! html_trace {
#[doc(hidden)]
macro_rules! html_trace_quiet {
($fmt:expr) => {
eprintln!( $fmt );
log::trace!( $fmt );
};
($fmt:expr, $( $args:expr ),*) => {
eprintln!( $fmt, $( $args ),* );
log::trace!( $fmt, $( $args ),* );
};
}

Expand Down
26 changes: 26 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2017,6 +2017,32 @@ text
"#);
}

#[test]
fn test_coloured_important()
{
use super::test_colour_map;
let config = crate::config::rich()
.use_doc_css();
let dom = config.parse_html(&br##"
<style>
.red {
color:#FF0000 !important;
}
</style>
<p>Test paragraph with <span class="red">red</span> text</p>
"##[..]).unwrap();
let rt = config.dom_to_render_tree(&dom).unwrap();
assert_eq!(config.render_coloured(rt.clone(), 10, test_colour_map).unwrap(),
r#"Test
paragraph
with <R>red</R>
text
"#);
assert_eq!(config.render_coloured(rt.clone(), 100, test_colour_map).unwrap(),
r#"Test paragraph with <R>red</R> text
"#);
}

#[test]
fn test_wrap_word_boundaries() {
let html = br#"<head><style>em { color: white; }</style></head>
Expand Down

0 comments on commit e4f2308

Please sign in to comment.