From c6de9c44fae7be597d9eef9c3d2576ba49adc0e1 Mon Sep 17 00:00:00 2001 From: Chris Emerson Date: Mon, 18 Sep 2023 07:24:06 +0100 Subject: [PATCH 1/3] Add an experimental Config struct. This aims to become a more general way of adding configuration for the translation. --- src/lib.rs | 66 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 60 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 5d575e0..b930f99 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1528,6 +1528,60 @@ fn render_table_cell( }) } +pub mod config { + //! Configure the HTML to text translation using the `Config` type, which can be + //! constructed using one of the functions in this module. + + use crate::render::text_renderer::{ + PlainDecorator, RichDecorator, TaggedLine, TextDecorator + }; + use super::parse; + + /// Configure the HTML processing. + pub struct Config { + decorator: D, + } + + impl Config { + /// Reads HTML from `input`, and returns a `String` with text wrapped to + /// `width` columns. + pub fn string_from_read(self, input: R, width: usize) -> String { + parse(input).render(width, self.decorator).into_string() + } + + /// Reads HTML from `input`, and returns text wrapped to `width` columns. + /// The text is returned as a `Vec>`; the annotations are vectors + /// of the provided text decorator's `Annotation`. The "outer" annotation comes first in + /// the `Vec`. + pub fn lines_from_read(self, input: R, width: usize) -> Vec>> { + parse(input) + .render(width, self.decorator) + .into_lines() + } + } + + /// Return a Config initialized with a `RichDecorator`. + pub fn rich() -> Config { + Config { + decorator: RichDecorator::new() + } + } + + /// Return a Config initialized with a `PlainDecorator`. + pub fn plain() -> Config { + Config { + decorator: PlainDecorator::new() + } + } + + /// Return a Config initialized with a custom decorator. + pub fn with_decorator(decorator: D) -> Config { + Config { + decorator + } + } +} + /// The structure of an HTML document that can be rendered using a [`TextDecorator`][]. /// /// [`TextDecorator`]: render/text_renderer/trait.TextDecorator.html @@ -1604,7 +1658,8 @@ where R: io::Read, D: TextDecorator, { - parse(input).render(width, decorator).into_string() + config::with_decorator(decorator) + .string_from_read(input, width) } /// Reads HTML from `input`, and returns a `String` with text wrapped to @@ -1613,8 +1668,8 @@ pub fn from_read(input: R, width: usize) -> String where R: io::Read, { - let decorator = PlainDecorator::new(); - from_read_with_decorator(input, width, decorator) + config::plain() + .string_from_read(input, width) } /// Reads HTML from `input`, and returns text wrapped to `width` columns. @@ -1624,9 +1679,8 @@ pub fn from_read_rich(input: R, width: usize) -> Vec Date: Sun, 29 Oct 2023 20:31:27 +0000 Subject: [PATCH 2/3] Bump MSRV (required by the log crate). --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 55ee9ac..756e60b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ repository = "https://github.com/jugglerchris/rust-html2text/" readme = "README.md" documentation = "https://docs.rs/html2text/" edition = "2018" -rust-version = "1.56" +rust-version = "1.60" keywords = ["html", "text"] license = "MIT" From c185a3370fc8ef46eecca0f7356af3f304513a74 Mon Sep 17 00:00:00 2001 From: Chris Emerson Date: Sun, 29 Oct 2023 20:41:04 +0000 Subject: [PATCH 3/3] Bump rustc versions in CI images to match the MSRV. --- .circleci/config.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index b29fdf6..b775004 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -6,15 +6,15 @@ orbs: jobs: build-stable: docker: - - image: cimg/rust:1.59 + - image: cimg/rust:1.73 steps: - checkout - run: cargo --version - run: cargo build - run: cargo test - build-1-56: + build-1-60: docker: - - image: cimg/rust:1.56 + - image: cimg/rust:1.60 steps: - checkout - run: cargo --version @@ -54,5 +54,5 @@ workflows: build: jobs: - "build-stable" - - "build-1-56" + - "build-1-60" - "build-windows"