crabmail

Static HTML email archive viewer in Rust
git clone git://git.alexwennerberg.com/crabmail
Log | Files | Refs | README | LICENSE

commit 602fe341637767889f56ef8caf8dd003b3ae7439
parent e397382cb5771217c37428a38e4fd305a7ce4544
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Mon,  3 Jan 2022 22:29:26 -0800

Display format=flowed properly

Diffstat:
Msrc/main.rs | 3+++
Msrc/utils.rs | 28++++++++++++++++++++++++++++
2 files changed, 31 insertions(+), 0 deletions(-)

diff --git a/src/main.rs b/src/main.rs @@ -423,6 +423,9 @@ fn local_parse_email(parsed_mail: &ParsedMail) -> Result<Email> { } else { if top.ctype.mimetype == "text/plain" { body = top.get_body().unwrap_or(nobody.to_owned()); + if parsed_mail.ctype.params.get("format") == Some(&"flowed".to_owned()) { + body = utils::unformat_flowed(&body); + } mime = top.ctype.mimetype.clone(); break; } diff --git a/src/utils.rs b/src/utils.rs @@ -64,6 +64,34 @@ pub fn email_body(body: &str) -> String { String::from_utf8(bytes).unwrap() } +// stolen from https://github.com/deltachat/deltachat-core-rust/blob/master/src/format_flowed.rs +// undoes format=flowed +pub fn unformat_flowed(text: &str) -> String { + let mut result = String::new(); + let mut skip_newline = true; + + for line in text.split('\n') { + // Revert space-stuffing + let line = line.strip_prefix(' ').unwrap_or(line); + + if !skip_newline { + result.push('\n'); + } + + if let Some(line) = line.strip_suffix(' ') { + // Flowed line + result += line; + result.push(' '); + skip_newline = true; + } else { + // Fixed line + result += line; + skip_newline = false; + } + } + result +} + // less efficient, easier api pub fn xml_safe(text: &str) -> String { // note we escape more than we need to