From 79d6863eeb8713df93ede9746438fd348de5b8cc Mon Sep 17 00:00:00 2001 From: Chris Emerson Date: Sat, 30 Nov 2024 16:54:54 +0000 Subject: [PATCH] cargo fmt --- examples/html2term.rs | 81 +++++++----- src/css.rs | 41 +++--- src/css/parser.rs | 297 +++++++++++++++++++++++++++--------------- src/lib.rs | 5 +- 4 files changed, 261 insertions(+), 163 deletions(-) diff --git a/examples/html2term.rs b/examples/html2term.rs index a2c1bf9..b50aa35 100644 --- a/examples/html2term.rs +++ b/examples/html2term.rs @@ -26,36 +26,21 @@ mod top { style.push_str(&format!("{}", termion::style::Underline)); } RichAnnotation::Image(_) => { - style.push_str(&format!( - "{}", - Fg(LightBlue) - )); + style.push_str(&format!("{}", Fg(LightBlue))); } RichAnnotation::Emphasis => { - style.push_str(&format!( - "{}", - Fg(LightGreen) - )); + style.push_str(&format!("{}", Fg(LightGreen))); } RichAnnotation::Strong => { - style.push_str(&format!( - "{}", - Fg(LightGreen) - )); + style.push_str(&format!("{}", Fg(LightGreen))); } RichAnnotation::Strikeout => (), RichAnnotation::Code => { - style.push_str(&format!( - "{}", - Fg(LightYellow) - )); + style.push_str(&format!("{}", Fg(LightYellow))); } RichAnnotation::Preformat(is_cont) => { if is_cont { - style.push_str(&format!( - "{}", - Fg(LightMagenta) - )); + style.push_str(&format!("{}", Fg(LightMagenta))); } else { style.push_str(&format!("{}", Fg(Magenta))); } @@ -170,7 +155,9 @@ mod top { let mut file = std::fs::File::open(filename).expect("Tried to open file"); - let dom = html2text::config::plain().parse_html(&mut file).expect("Failed to parse HTML"); + let dom = html2text::config::plain() + .parse_html(&mut file) + .expect("Failed to parse HTML"); let mut keys = io::stdin().keys(); @@ -242,7 +229,14 @@ mod top { node = node.nth_child(idx).unwrap(); pth.push_str(&format!("> {}", node.element_name().unwrap())); } - write!(screen, "{}{}{:?}", Goto(1, vis_y_limit as u16), pth, &inspect_path).unwrap(); + write!( + screen, + "{}{}{:?}", + Goto(1, vis_y_limit as u16), + pth, + &inspect_path + ) + .unwrap(); } // 1-based screen coordinates @@ -356,37 +350,60 @@ mod top { } } - fn rerender(dom: &html2text::RcDom, inspect_path: &[usize], width: usize, options: &Options) -> Vec>> { + fn rerender( + dom: &html2text::RcDom, + inspect_path: &[usize], + width: usize, + options: &Options, + ) -> Vec>> { let config = html2text::config::rich(); #[cfg(feature = "css")] let config = if options.use_css { - config.use_doc_css() - .add_agent_css(r#" + config + .use_doc_css() + .add_agent_css( + r#" img { color: #77f; } - "#).unwrap() + "#, + ) + .unwrap() } else { config }; if inspect_path.is_empty() { - let render_tree = config.dom_to_render_tree(&dom).expect("Failed to build render tree"); - config.render_to_lines(render_tree, width).expect("Failed to render") + let render_tree = config + .dom_to_render_tree(&dom) + .expect("Failed to build render tree"); + config + .render_to_lines(render_tree, width) + .expect("Failed to render") } else { let mut path_selector = String::new(); for &idx in &inspect_path[1..] { path_selector.push_str(&format!(" > :nth-child({})", idx)); } let config = config - .add_agent_css(&(format!(r#" + .add_agent_css( + &(format!( + r#" html {} {{ color: white !important; background-color: black !important; display: x-raw-dom; }} - "#, path_selector))).expect("Invalid CSS"); - let render_tree = config.dom_to_render_tree(&dom).expect("Failed to build render tree"); - config.render_to_lines(render_tree, width).expect("Failed to render") + "#, + path_selector + )), + ) + .expect("Invalid CSS"); + let render_tree = config + .dom_to_render_tree(&dom) + .expect("Failed to build render tree"); + config + .render_to_lines(render_tree, width) + .expect("Failed to render") } } } diff --git a/src/css.rs b/src/css.rs index 7417b90..f3207e9 100644 --- a/src/css.rs +++ b/src/css.rs @@ -1,13 +1,14 @@ //! Some basic CSS support. -use std::{io::Write, rc::Rc}; use std::ops::Deref; +use std::{io::Write, rc::Rc}; mod parser; use crate::{ css::parser::parse_rules, markup5ever_rcdom::{ - Handle, NodeData::{self, Comment, Document, Element} + Handle, + NodeData::{self, Comment, Document, Element}, }, tree_map_reduce, Colour, ComputedStyle, Result, Specificity, StyleOrigin, TreeMapResult, WhiteSpace, @@ -225,9 +226,7 @@ impl std::fmt::Display for StyleDecl { } match self.importance { Importance::Default => (), - Importance::Important => { - write!(f, " !important")? - } + Importance::Important => write!(f, " !important")?, } Ok(()) } @@ -325,24 +324,22 @@ fn styles_from_properties(decls: &[parser::Declaration]) -> Vec { overflow_hidden = true; } parser::Decl::Overflow { .. } | parser::Decl::OverflowY { .. } => {} - parser::Decl::Display { value } => { - match value { - parser::Display::None => { - styles.push(StyleDecl { - style: Style::Display(Display::None), - importance: decl.important, - }); - } - #[cfg(feature = "css_ext")] - parser::Display::RawDom => { - styles.push(StyleDecl { - style: Style::Display(Display::ExtRawDom), - importance: decl.important, - }); - } - _ => (), + parser::Decl::Display { value } => match value { + parser::Display::None => { + styles.push(StyleDecl { + style: Style::Display(Display::None), + importance: decl.important, + }); } - } + #[cfg(feature = "css_ext")] + parser::Display::RawDom => { + styles.push(StyleDecl { + style: Style::Display(Display::ExtRawDom), + importance: decl.important, + }); + } + _ => (), + }, parser::Decl::WhiteSpace { value } => { styles.push(StyleDecl { style: Style::WhiteSpace(*value), diff --git a/src/css/parser.rs b/src/css/parser.rs index e5bde97..0d64f0c 100644 --- a/src/css/parser.rs +++ b/src/css/parser.rs @@ -759,7 +759,7 @@ fn parse_class(text: &str) -> IResult<&str, SelectorComponent> { #[derive(Eq, PartialEq, Copy, Clone)] enum Sign { Plus, - Neg + Neg, } impl Sign { @@ -790,50 +790,46 @@ fn parse_nth_child_args(text: &str) -> IResult<&str, SelectorComponent> { let (rest, _) = tag("(")(text)?; let (rest, _) = skip_optional_whitespace(rest)?; - let (rest, (a, b)) = - alt(( - map( - tag("even"), - |_| (2, 0), - ), - map( - tag("odd"), - |_| (2, 1), - ), - // The case where both a and b are specified - map( - tuple(( - opt_sign, opt(digit1), tag("n"), - skip_optional_whitespace, - sign, digit1)), - |(a_sign, a_opt_val, _, - _, - b_sign, b_val)| { - let a = ::from_str(a_opt_val.unwrap_or("1")).unwrap() * a_sign.val(); - let b = ::from_str(b_val).unwrap() * b_sign.val(); - (a, b) - }), - // Just a - map( - tuple((opt_sign, opt(digit1), tag("n"))), - |(a_sign, a_opt_val, _)| { - let a = ::from_str(a_opt_val.unwrap_or("1")).unwrap() * a_sign.val(); - (a, 0) - }), - // Just b - map( - tuple(( - opt_sign, digit1)), - |(b_sign, b_val)| { - let b = ::from_str(b_val).unwrap() * b_sign.val(); - (0, b) - }), - ))(rest)?; + let (rest, (a, b)) = alt(( + map(tag("even"), |_| (2, 0)), + map(tag("odd"), |_| (2, 1)), + // The case where both a and b are specified + map( + tuple(( + opt_sign, + opt(digit1), + tag("n"), + skip_optional_whitespace, + sign, + digit1, + )), + |(a_sign, a_opt_val, _, _, b_sign, b_val)| { + let a = + ::from_str(a_opt_val.unwrap_or("1")).unwrap() * a_sign.val(); + let b = ::from_str(b_val).unwrap() * b_sign.val(); + (a, b) + }, + ), + // Just a + map( + tuple((opt_sign, opt(digit1), tag("n"))), + |(a_sign, a_opt_val, _)| { + let a = + ::from_str(a_opt_val.unwrap_or("1")).unwrap() * a_sign.val(); + (a, 0) + }, + ), + // Just b + map(tuple((opt_sign, digit1)), |(b_sign, b_val)| { + let b = ::from_str(b_val).unwrap() * b_sign.val(); + (0, b) + }), + ))(rest)?; let (rest, _) = tuple((skip_optional_whitespace, tag(")")))(rest)?; let sel = Selector { - components: vec![SelectorComponent::Star] + components: vec![SelectorComponent::Star], }; Ok((rest, SelectorComponent::NthChild { a, b, sel })) } @@ -943,22 +939,21 @@ fn skip_to_end_of_statement(text: &str) -> IResult<&str, ()> { Err(_) => return Ok((rest, ())), }; match &tok { - Token::Ident(..) | - Token::AtKeyword(_) | - Token::Hash(_) | - Token::String(_) | - Token::BadString(_) | - Token::Url(_) | - Token::BadUrl(_) | - Token::Delim(_) | - Token::Number(_) | - Token::Dimension(_, _) | - Token::Percentage(_) | - Token::Colon | - Token::Comma => (), - - Token::Function(_) | - Token::OpenRound => { + Token::Ident(..) + | Token::AtKeyword(_) + | Token::Hash(_) + | Token::String(_) + | Token::BadString(_) + | Token::Url(_) + | Token::BadUrl(_) + | Token::Delim(_) + | Token::Number(_) + | Token::Dimension(_, _) + | Token::Percentage(_) + | Token::Colon + | Token::Comma => (), + + Token::Function(_) | Token::OpenRound => { bra_stack.push(Token::CloseRound); } Token::CDO => { @@ -980,10 +975,7 @@ fn skip_to_end_of_statement(text: &str) -> IResult<&str, ()> { return Ok((rest, ())); } // Standard closing brackets - Token::CDC | - Token::CloseSquare | - Token::CloseRound | - Token::CloseBrace => { + Token::CDC | Token::CloseSquare | Token::CloseRound | Token::CloseBrace => { if bra_stack.last() == Some(&tok) { bra_stack.pop(); @@ -1004,18 +996,17 @@ fn skip_to_end_of_statement(text: &str) -> IResult<&str, ()> { fn parse_at_rule(text: &str) -> IResult<&str, ()> { let (rest, _) = tuple(( - skip_optional_whitespace, - tag("@"), - skip_optional_whitespace, - parse_ident))(text)?; + skip_optional_whitespace, + tag("@"), + skip_optional_whitespace, + parse_ident, + ))(text)?; skip_to_end_of_statement(rest) } fn parse_statement(text: &str) -> IResult<&str, Option> { - alt(( - map(parse_ruleset, Some), - map(parse_at_rule, |_| None)))(text) + alt((map(parse_ruleset, Some), map(parse_at_rule, |_| None)))(text) } pub(crate) fn parse_stylesheet(text: &str) -> IResult<&str, Vec> { @@ -1315,45 +1306,135 @@ mod test { fn test_nth_child() { use SelectorComponent::NthChild; let (_, sel_all) = super::parse_selector("*").unwrap(); - assert_eq!(super::parse_selector(":nth-child(even)").unwrap(), - ("", Selector { - components: vec![NthChild { a: 2, b: 0, sel: sel_all.clone() }] - })); - assert_eq!(super::parse_selector(":nth-child(odd)").unwrap(), - ("", Selector { - components: vec![NthChild { a: 2, b: 1, sel: sel_all.clone() }] - })); - assert_eq!(super::parse_selector(":nth-child(17)").unwrap(), - ("", Selector { - components: vec![NthChild { a: 0, b: 17, sel: sel_all.clone() }] - })); - assert_eq!(super::parse_selector(":nth-child(17n)").unwrap(), - ("", Selector { - components: vec![NthChild { a: 17, b: 0, sel: sel_all.clone() }] - })); - assert_eq!(super::parse_selector(":nth-child(10n-1)").unwrap(), - ("", Selector { - components: vec![NthChild { a: 10, b: -1, sel: sel_all.clone() }] - })); - assert_eq!(super::parse_selector(":nth-child(10n+9)").unwrap(), - ("", Selector { - components: vec![NthChild { a: 10, b: 9, sel: sel_all.clone() }] - })); - assert_eq!(super::parse_selector(":nth-child(-n+3)").unwrap(), - ("", Selector { - components: vec![NthChild { a: -1, b: 3, sel: sel_all.clone() }] - })); - assert_eq!(super::parse_selector(":nth-child(n)").unwrap(), - ("", Selector { - components: vec![NthChild { a: 1, b: 0, sel: sel_all.clone() }] - })); - assert_eq!(super::parse_selector(":nth-child(+n)").unwrap(), - ("", Selector { - components: vec![NthChild { a: 1, b: 0, sel: sel_all.clone() }] - })); - assert_eq!(super::parse_selector(":nth-child(-n)").unwrap(), - ("", Selector { - components: vec![NthChild { a: -1, b: 0, sel: sel_all.clone() }] - })); + assert_eq!( + super::parse_selector(":nth-child(even)").unwrap(), + ( + "", + Selector { + components: vec![NthChild { + a: 2, + b: 0, + sel: sel_all.clone() + }] + } + ) + ); + assert_eq!( + super::parse_selector(":nth-child(odd)").unwrap(), + ( + "", + Selector { + components: vec![NthChild { + a: 2, + b: 1, + sel: sel_all.clone() + }] + } + ) + ); + assert_eq!( + super::parse_selector(":nth-child(17)").unwrap(), + ( + "", + Selector { + components: vec![NthChild { + a: 0, + b: 17, + sel: sel_all.clone() + }] + } + ) + ); + assert_eq!( + super::parse_selector(":nth-child(17n)").unwrap(), + ( + "", + Selector { + components: vec![NthChild { + a: 17, + b: 0, + sel: sel_all.clone() + }] + } + ) + ); + assert_eq!( + super::parse_selector(":nth-child(10n-1)").unwrap(), + ( + "", + Selector { + components: vec![NthChild { + a: 10, + b: -1, + sel: sel_all.clone() + }] + } + ) + ); + assert_eq!( + super::parse_selector(":nth-child(10n+9)").unwrap(), + ( + "", + Selector { + components: vec![NthChild { + a: 10, + b: 9, + sel: sel_all.clone() + }] + } + ) + ); + assert_eq!( + super::parse_selector(":nth-child(-n+3)").unwrap(), + ( + "", + Selector { + components: vec![NthChild { + a: -1, + b: 3, + sel: sel_all.clone() + }] + } + ) + ); + assert_eq!( + super::parse_selector(":nth-child(n)").unwrap(), + ( + "", + Selector { + components: vec![NthChild { + a: 1, + b: 0, + sel: sel_all.clone() + }] + } + ) + ); + assert_eq!( + super::parse_selector(":nth-child(+n)").unwrap(), + ( + "", + Selector { + components: vec![NthChild { + a: 1, + b: 0, + sel: sel_all.clone() + }] + } + ) + ); + assert_eq!( + super::parse_selector(":nth-child(-n)").unwrap(), + ( + "", + Selector { + components: vec![NthChild { + a: -1, + b: 0, + sel: sel_all.clone() + }] + } + ) + ); } } diff --git a/src/lib.rs b/src/lib.rs index e3e112c..6a1f5fa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1530,7 +1530,10 @@ fn process_dom_node( WhiteSpace::Pre, ); let text = RenderNode::new(RenderNodeInfo::Text(result_text)); - return Ok(Finished(RenderNode::new_styled(RenderNodeInfo::Block(vec![text]), computed))); + return Ok(Finished(RenderNode::new_styled( + RenderNodeInfo::Block(vec![text]), + computed, + ))); } _ => (), }