From 8ad91b23d6caddd1c5b627f8060c99a71ce9cea9 Mon Sep 17 00:00:00 2001 From: alex wennerberg Date: Mon, 3 Jan 2022 08:57:38 -0800 Subject: [PATCH] Update README, get started --- Cargo.lock | 7 +++++++ Cargo.toml | 4 +--- README | 13 +++++++++++++ README.md | 3 --- src/lib.rs | 1 + src/main.rs | 32 ++++++++++++++++++++++++++++++++ 6 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 Cargo.lock create mode 100644 README delete mode 100644 README.md create mode 100644 src/lib.rs diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..ad1a474 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "nanohtml2text" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index cb02011..be85f14 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,6 @@ [package] -name = "html2text-lite" +name = "nanohtml2text" version = "0.1.0" edition = "2018" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] diff --git a/README b/README new file mode 100644 index 0000000..8174ca4 --- /dev/null +++ b/README @@ -0,0 +1,13 @@ +nanohtml2text +============= + +0-dependency library to convert HTML to text; an alternative to https://crates.io/crates/html2text that doesn't use a full browser-grade HTML parser + +Based on https://github.com/k3a/html2text -- basically a line-for-line rewreite + +Useful for displaying HTML emails + +Usage: + +let s = "Hacker mode" +html2text(s); diff --git a/README.md b/README.md deleted file mode 100644 index 6d98cc7..0000000 --- a/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# HTML to text - -Like html2text, but doesn't require a full browser-grade html parser diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..bda254f --- /dev/null +++ b/src/lib.rs @@ -0,0 +1 @@ +fn html2text() {} diff --git a/src/main.rs b/src/main.rs index e7a11a9..f7895d8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,35 @@ +// almost a line for line rewrite of https://github.com/k3a/html2text/blob/master/html2text.go fn main() { println!("Hello, world!"); } + +fn write_space(s: &mut String) {} + +fn html2text(input: &str) -> String { + let in_len = input.len(); + let mut tag_start = 0; + let mut in_ent = false; + let mut bad_tag_stack_depth = 0; + let mut should_output = true; + let mut can_print_new_line = false; + let mut out_buf = String::new(); + for (i, r) in input.chars().enumerate() { + if in_len > 0 && i == in_len - 1 { + can_print_new_line = false + } + if r.is_whitespace() { + if should_output && bad_tag_stack_depth == 0 && !in_ent { + write_space(&mut out_buf); + } + continue; + } else if r == ';' && in_ent { + in_ent = false; + continue; + } else if r == '&' && should_output { + let mut ent_name = String::new(); + in_ent = false; + // parse the entity name, max 10 chars + } + } + out_buf +}