From efa37dd2837904578c5fc15d35ceec7dc93ea6b3 Mon Sep 17 00:00:00 2001 From: link2xt Date: Thu, 22 Jun 2023 00:27:49 +0000 Subject: [PATCH] fix: preserve indentation when converting plaintext to HTML --- src/plaintext.rs | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/plaintext.rs b/src/plaintext.rs index 22780e993..afd75b981 100644 --- a/src/plaintext.rs +++ b/src/plaintext.rs @@ -96,7 +96,12 @@ impl PlainText { line += "
\n"; } - ret += &*line; + let len_with_indentation = line.len(); + let line = line.trim_start_matches(' '); + for _ in line.len()..len_with_indentation { + ret += " "; + } + ret += line; } ret += "\n"; ret @@ -267,7 +272,32 @@ line
still line
>quote
>still quote
- >no quote
+ >no quote
+ +"# + ); + } + + #[test] + fn test_plain_to_html_indentation() { + let html = PlainText { + text: "def foo():\n pass\n\ndef bar(x):\n return x + 5".to_string(), + flowed: false, + delsp: false, + } + .to_html(); + assert_eq!( + html, + r#" + + + + +def foo():
+    pass
+
+def bar(x):
+    return x + 5
"# );