From 5c88a7faee48dd26253fa5c2822680ec33d8b266 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Fri, 23 Apr 2021 16:41:22 -0300 Subject: [PATCH] Textwrap, but it is breaking long links --- html5test/Cargo.lock | 23 +++++++++++++++++++++++ html5test/Cargo.toml | 1 + html5test/src/main.rs | 13 +++++++++++-- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/html5test/Cargo.lock b/html5test/Cargo.lock index cdcca92..1288027 100644 --- a/html5test/Cargo.lock +++ b/html5test/Cargo.lock @@ -47,6 +47,7 @@ version = "0.1.0" dependencies = [ "html5ever", "markup5ever_rcdom", + "textwrap", ] [[package]] @@ -239,6 +240,12 @@ version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cbce6d4507c7e4a3962091436e56e95290cb71fa302d0d270e32130b75fbff27" +[[package]] +name = "smawk" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f67ad224767faa3c7d8b6d91985b78e70a1324408abcb1cfcc2be4c06bc06043" + [[package]] name = "string_cache" version = "0.8.1" @@ -286,6 +293,16 @@ dependencies = [ "utf-8", ] +[[package]] +name = "textwrap" +version = "0.13.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd05616119e612a8041ef58f2b578906cc2531a6069047ae092cfb86a325d835" +dependencies = [ + "smawk", + "unicode-width", +] + [[package]] name = "time" version = "0.1.44" @@ -297,6 +314,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "unicode-width" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" + [[package]] name = "unicode-xid" version = "0.2.1" diff --git a/html5test/Cargo.toml b/html5test/Cargo.toml index 6ad1c47..5dc1170 100644 --- a/html5test/Cargo.toml +++ b/html5test/Cargo.toml @@ -9,3 +9,4 @@ edition = "2018" [dependencies] html5ever = "0.25" markup5ever_rcdom = "0.1" +textwrap = "0.13" diff --git a/html5test/src/main.rs b/html5test/src/main.rs index 5b9793c..a676559 100644 --- a/html5test/src/main.rs +++ b/html5test/src/main.rs @@ -5,6 +5,8 @@ use markup5ever_rcdom::NodeData; use markup5ever_rcdom::RcDom; use std::borrow::Borrow; use std::default::Default; +use textwrap::fill; +use textwrap::Options; fn go_children(input: &Handle, result: &mut String) { for child in input.children.borrow().iter() { @@ -41,8 +43,12 @@ fn walk(input: &Handle, result: &mut String) { .iter() .find(|attr| attr.name.local.to_string() == "class"); if let Some(class) = classes { - if !class.value.to_string().contains("invisible") { + let classes = class.value.to_string(); + if !classes.contains("invisible") { go_children(input, result); + if classes.contains("ellipsis") { + result.push_str("..."); + } } } else { go_children(input, result); @@ -98,5 +104,8 @@ fn main() { let mut result = String::new(); walk(&dom.document, &mut result); println!("---------------------------------"); - println!("{}", result); + let options = Options::new(70) + .initial_indent(" ") + .subsequent_indent(" "); + println!("{}", fill(&result.trim(), &options)); }