From dfc69eba4a59cf870b5f8a6785a69427ac078191 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fan=20=C3=87elik?= Date: Mon, 11 Sep 2023 03:46:12 +0300 Subject: [PATCH 1/3] Support multiple attributes --- src/main.rs | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4e2d590..5fbc55a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,6 +29,7 @@ struct Config { pretty_print: bool, remove_nodes: Option>, attributes: Option>, + attribute_separator: String, } impl Config { @@ -58,6 +59,7 @@ impl Config { pretty_print: matches.is_present("pretty_print"), remove_nodes, attributes, + attribute_separator: matches.value_of("attribute_separator").unwrap_or(" ").to_string(), selector, }) } @@ -76,16 +78,22 @@ impl Default for Config { text_only: false, remove_nodes: None, attributes: Some(vec![]), + attribute_separator: " ".to_string(), } } } -fn select_attributes(node: &NodeRef, attributes: &[String], output: &mut dyn io::Write) { +fn select_attributes(node: &NodeRef, attributes: &Vec, attribute_separator: &String, output: &mut dyn io::Write) { if let Some(as_element) = node.as_element() { - for attr in attributes { + let mut it = attributes.iter().peekable(); + while let Some(attr) = it.next() { if let Ok(elem_atts) = as_element.attributes.try_borrow() { if let Some(val) = elem_atts.get(attr.as_str()) { - writeln!(output, "{}", val).ok(); + if it.peek().is_some() { + write!(output, "{}{}", val, attribute_separator).ok(); + } else { + writeln!(output, "{}", val).ok(); + } } } } @@ -152,8 +160,19 @@ fn get_config<'a, 'b>() -> App<'a, 'b> { Arg::with_name("attribute") .short("a") .long("attribute") + .multiple(true) + .number_of_values(1) + .takes_value(true) + .value_name("ATTRIBUTE") + .help("Only return this attribute (if present) from selected elements. May be specified multiple times"), + ) + .arg( + Arg::with_name("attribute_separator") + .short("s") + .long("attribute-separator") .takes_value(true) - .help("Only return this attribute (if present) from selected elements"), + .value_name("SEPARATOR") + .help("The separator to use when printing attributes. Defaults to a space"), ) .arg( Arg::with_name("base") @@ -236,8 +255,8 @@ fn main() -> Result<(), Box> { .for_each(|matched_noderef| { let node = matched_noderef.as_node(); - if let Some(attributes) = &config.attributes { - select_attributes(node, attributes, &mut output); + if let Some(attributes) = config.attributes.as_ref() { + select_attributes(node, attributes, &config.attribute_separator, &mut output); return; } From 02c4e36a8b34f416b41e0d891e64b48d90547481 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fan=20=C3=87elik?= Date: Wed, 4 Oct 2023 01:05:16 +0300 Subject: [PATCH 2/3] End line with newline if value is none --- src/main.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main.rs b/src/main.rs index 5fbc55a..392ba47 100644 --- a/src/main.rs +++ b/src/main.rs @@ -94,6 +94,8 @@ fn select_attributes(node: &NodeRef, attributes: &Vec, attribute_separat } else { writeln!(output, "{}", val).ok(); } + } else { + writeln!(output, "").ok(); } } } From c3e74c0d4157e10059d7ed8da028566539345d2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fan=20=C3=87elik?= Date: Wed, 4 Oct 2023 01:29:57 +0300 Subject: [PATCH 3/3] Check for next attribute --- src/main.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 392ba47..e1d28d5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -95,7 +95,11 @@ fn select_attributes(node: &NodeRef, attributes: &Vec, attribute_separat writeln!(output, "{}", val).ok(); } } else { - writeln!(output, "").ok(); + if it.peek().is_some() { + write!(output, "{}{}", "", attribute_separator).ok(); + } else { + writeln!(output, "{}", "").ok(); + } } } }