Commit b1ccec6732

b1ccec6732cab1406f9e0c40f6fd1a8ea871812a

parent: 6e8e12b579

Unregistered key

Ulyssa <git@ulyssa.dev> · 2023-07-08 04:42 UTC

Code blocks get rendered without line breaks (#122)

Layout: unified · split

Cargo.lock +23
@@ -873,6 +873,12 @@ version = "1.3.3"
873source = "registry+https://github.com/rust-lang/crates.io-index" 873source = "registry+https://github.com/rust-lang/crates.io-index"
874checksum = "8c1bba4f227a4a53d12b653f50ca7bf10c9119ae2aba56aff9e0338b5c98f36a" 874checksum = "8c1bba4f227a4a53d12b653f50ca7bf10c9119ae2aba56aff9e0338b5c98f36a"
875 875
876[[package]]
877name = "diff"
878version = "0.1.13"
879source = "registry+https://github.com/rust-lang/crates.io-index"
880checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8"
881
876[[package]] 882[[package]]
877name = "digest" 883name = "digest"
878version = "0.9.0" 884version = "0.9.0"
@@ -1535,6 +1541,7 @@ dependencies = [
1535 "mime_guess", 1541 "mime_guess",
1536 "modalkit", 1542 "modalkit",
1537 "open", 1543 "open",
1544 "pretty_assertions",
1538 "regex", 1545 "regex",
1539 "rpassword", 1546 "rpassword",
1540 "serde", 1547 "serde",
@@ -2578,6 +2585,16 @@ version = "0.1.1"
2578source = "registry+https://github.com/rust-lang/crates.io-index" 2585source = "registry+https://github.com/rust-lang/crates.io-index"
2579checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c" 2586checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c"
2580 2587
2588[[package]]
2589name = "pretty_assertions"
2590version = "1.4.0"
2591source = "registry+https://github.com/rust-lang/crates.io-index"
2592checksum = "af7cee1a6c8a5b9208b3cb1061f10c0cb689087b3d8ce85fb9d2dd7a29b6ba66"
2593dependencies = [
2594 "diff",
2595 "yansi",
2596]
2597
2581[[package]] 2598[[package]]
2582name = "proc-macro-crate" 2599name = "proc-macro-crate"
2583version = "1.3.1" 2600version = "1.3.1"
@@ -4519,6 +4536,12 @@ dependencies = [
4519 "linked-hash-map", 4536 "linked-hash-map",
4520] 4537]
4521 4538
4539[[package]]
4540name = "yansi"
4541version = "0.5.1"
4542source = "registry+https://github.com/rust-lang/crates.io-index"
4543checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec"
4544
4522[[package]] 4545[[package]]
4523name = "zeroize" 4546name = "zeroize"
4524version = "1.3.0" 4547version = "1.3.0"
Cargo.toml +1
@@ -66,6 +66,7 @@ features = ["macros", "net", "rt-multi-thread", "sync", "time"]
66 66
67[dev-dependencies] 67[dev-dependencies]
68lazy_static = "1.4.0" 68lazy_static = "1.4.0"
69pretty_assertions = "1.4.0"
69 70
70[profile.release] 71[profile.release]
71lto = true 72lto = true
src/message/html.rs +109 −1
@@ -237,6 +237,7 @@ pub enum StyleTreeNode {
237 Image(Option<String>), 237 Image(Option<String>),
238 List(StyleTreeChildren, ListStyle), 238 List(StyleTreeChildren, ListStyle),
239 Paragraph(Box<StyleTreeNode>), 239 Paragraph(Box<StyleTreeNode>),
240 Pre(Box<StyleTreeNode>),
240 Reply(Box<StyleTreeNode>), 241 Reply(Box<StyleTreeNode>),
241 Ruler, 242 Ruler,
242 Style(Box<StyleTreeNode>, Style), 243 Style(Box<StyleTreeNode>, Style),
@@ -311,6 +312,39 @@ impl StyleTreeNode {
311 child.print(printer, style); 312 child.print(printer, style);
312 printer.commit(); 313 printer.commit();
313 }, 314 },
315 StyleTreeNode::Pre(child) => {
316 let mut subp = printer.sub(2).literal(true);
317 let subw = subp.width();
318
319 child.print(&mut subp, style);
320
321 printer.commit();
322 printer.push_line(
323 vec![
324 Span::styled(line::TOP_LEFT, style),
325 Span::styled(line::HORIZONTAL.repeat(subw), style),
326 Span::styled(line::TOP_RIGHT, style),
327 ]
328 .into(),
329 );
330
331 for mut line in subp.finish() {
332 line.0.insert(0, Span::styled(line::VERTICAL, style));
333 line.0.push(Span::styled(line::VERTICAL, style));
334 printer.push_line(line);
335 }
336
337 printer.push_line(
338 vec![
339 Span::styled(line::BOTTOM_LEFT, style),
340 Span::styled(line::HORIZONTAL.repeat(subw), style),
341 Span::styled(line::BOTTOM_RIGHT, style),
342 ]
343 .into(),
344 );
345
346 printer.commit();
347 },
314 StyleTreeNode::Reply(child) => { 348 StyleTreeNode::Reply(child) => {
315 if printer.hide_reply() { 349 if printer.hide_reply() {
316 return; 350 return;
@@ -585,6 +619,7 @@ fn h2t(hdl: &Handle) -> StyleTreeChildren {
585 // Other text blocks. 619 // Other text blocks.
586 "blockquote" => StyleTreeNode::Blockquote(c2t(&node.children.borrow())), 620 "blockquote" => StyleTreeNode::Blockquote(c2t(&node.children.borrow())),
587 "div" | "p" => StyleTreeNode::Paragraph(c2t(&node.children.borrow())), 621 "div" | "p" => StyleTreeNode::Paragraph(c2t(&node.children.borrow())),
622 "pre" => StyleTreeNode::Pre(c2t(&node.children.borrow())),
588 623
589 // No children. 624 // No children.
590 "hr" => StyleTreeNode::Ruler, 625 "hr" => StyleTreeNode::Ruler,
@@ -593,7 +628,7 @@ fn h2t(hdl: &Handle) -> StyleTreeChildren {
593 "img" => StyleTreeNode::Image(attrs_to_alt(&attrs.borrow())), 628 "img" => StyleTreeNode::Image(attrs_to_alt(&attrs.borrow())),
594 629
595 // These don't render in any special way. 630 // These don't render in any special way.
596 "a" | "details" | "html" | "pre" | "summary" | "sub" | "sup" => { 631 "a" | "details" | "html" | "summary" | "sub" | "sup" => {
597 *c2t(&node.children.borrow()) 632 *c2t(&node.children.borrow())
598 }, 633 },
599 634
@@ -630,6 +665,7 @@ pub fn parse_matrix_html(s: &str) -> StyleTree {
630pub mod tests { 665pub mod tests {
631 use super::*; 666 use super::*;
632 use crate::util::space_span; 667 use crate::util::space_span;
668 use pretty_assertions::assert_eq;
633 669
634 #[test] 670 #[test]
635 fn test_header() { 671 fn test_header() {
@@ -1173,4 +1209,76 @@ pub mod tests {
1173 ]) 1209 ])
1174 ); 1210 );
1175 } 1211 }
1212
1213 #[test]
1214 fn test_pre_tag() {
1215 let s = concat!(
1216 "<pre><code class=\"language-rust\">",
1217 "fn hello() -&gt; usize {\n",
1218 " return 5;\n",
1219 "}\n",
1220 "</code></pre>\n"
1221 );
1222 let tree = parse_matrix_html(s);
1223 let text = tree.to_text(25, Style::default(), true);
1224 assert_eq!(text.lines.len(), 5);
1225 assert_eq!(
1226 text.lines[0],
1227 Spans(vec![
1228 Span::raw(line::TOP_LEFT),
1229 Span::raw(line::HORIZONTAL.repeat(23)),
1230 Span::raw(line::TOP_RIGHT)
1231 ])
1232 );
1233 assert_eq!(
1234 text.lines[1],
1235 Spans(vec![
1236 Span::raw(line::VERTICAL),
1237 Span::raw("fn"),
1238 Span::raw(" "),
1239 Span::raw("hello"),
1240 Span::raw("("),
1241 Span::raw(")"),
1242 Span::raw(" "),
1243 Span::raw("-"),
1244 Span::raw(">"),
1245 Span::raw(" "),
1246 Span::raw("usize"),
1247 Span::raw(" "),
1248 Span::raw("{"),
1249 Span::raw(" "),
1250 Span::raw(line::VERTICAL)
1251 ])
1252 );
1253 assert_eq!(
1254 text.lines[2],
1255 Spans(vec![
1256 Span::raw(line::VERTICAL),
1257 Span::raw(" "),
1258 Span::raw("return"),
1259 Span::raw(" "),
1260 Span::raw("5"),
1261 Span::raw(";"),
1262 Span::raw(" "),
1263 Span::raw(line::VERTICAL)
1264 ])
1265 );
1266 assert_eq!(
1267 text.lines[3],
1268 Spans(vec![
1269 Span::raw(line::VERTICAL),
1270 Span::raw("}"),
1271 Span::raw(" ".repeat(22)),
1272 Span::raw(line::VERTICAL)
1273 ])
1274 );
1275 assert_eq!(
1276 text.lines[4],
1277 Spans(vec![
1278 Span::raw(line::BOTTOM_LEFT),
1279 Span::raw(line::HORIZONTAL.repeat(23)),
1280 Span::raw(line::BOTTOM_RIGHT)
1281 ])
1282 );
1283 }
1176} 1284}
src/message/printer.rs +15 −2
@@ -17,6 +17,7 @@ pub struct TextPrinter<'a> {
17 alignment: Alignment, 17 alignment: Alignment,
18 curr_spans: Vec<Span<'a>>, 18 curr_spans: Vec<Span<'a>>,
19 curr_width: usize, 19 curr_width: usize,
20 literal: bool,
20} 21}
21 22
22impl<'a> TextPrinter<'a> { 23impl<'a> TextPrinter<'a> {
@@ -30,6 +31,7 @@ impl<'a> TextPrinter<'a> {
30 alignment: Alignment::Left, 31 alignment: Alignment::Left,
31 curr_spans: vec![], 32 curr_spans: vec![],
32 curr_width: 0, 33 curr_width: 0,
34 literal: false,
33 } 35 }
34 } 36 }
35 37
@@ -38,6 +40,11 @@ impl<'a> TextPrinter<'a> {
38 self 40 self
39 } 41 }
40 42
43 pub fn literal(mut self, literal: bool) -> Self {
44 self.literal = literal;
45 self
46 }
47
41 pub fn hide_reply(&self) -> bool { 48 pub fn hide_reply(&self) -> bool {
42 self.hide_reply 49 self.hide_reply
43 } 50 }
@@ -56,6 +63,7 @@ impl<'a> TextPrinter<'a> {
56 alignment: self.alignment, 63 alignment: self.alignment,
57 curr_spans: vec![], 64 curr_spans: vec![],
58 curr_width: 0, 65 curr_width: 0,
66 literal: self.literal,
59 } 67 }
60 } 68 }
61 69
@@ -162,11 +170,16 @@ impl<'a> TextPrinter<'a> {
162 170
163 for mut word in UnicodeSegmentation::split_word_bounds(s) { 171 for mut word in UnicodeSegmentation::split_word_bounds(s) {
164 if let "\n" | "\r\n" = word { 172 if let "\n" | "\r\n" = word {
173 if self.literal {
174 self.commit();
175 continue;
176 }
177
165 // Render embedded newlines as spaces. 178 // Render embedded newlines as spaces.
166 word = " "; 179 word = " ";
167 } 180 }
168 181
169 if self.curr_width == 0 && word.chars().all(char::is_whitespace) { 182 if !self.literal && self.curr_width == 0 && word.chars().all(char::is_whitespace) {
170 // Drop leading whitespace. 183 // Drop leading whitespace.
171 continue; 184 continue;
172 } 185 }
@@ -182,7 +195,7 @@ impl<'a> TextPrinter<'a> {
182 // Word doesn't fit on this line, so start a new one. 195 // Word doesn't fit on this line, so start a new one.
183 self.commit(); 196 self.commit();
184 197
185 if word.chars().all(char::is_whitespace) { 198 if !self.literal && word.chars().all(char::is_whitespace) {
186 // Drop leading whitespace. 199 // Drop leading whitespace.
187 continue; 200 continue;
188 } 201 }