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"
873873source = "registry+https://github.com/rust-lang/crates.io-index"
874874checksum = "8c1bba4f227a4a53d12b653f50ca7bf10c9119ae2aba56aff9e0338b5c98f36a"
875875
876[[package]]
877name = "diff"
878version = "0.1.13"
879source = "registry+https://github.com/rust-lang/crates.io-index"
880checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8"
881
876882[[package]]
877883name = "digest"
878884version = "0.9.0"
@@ -1535,6 +1541,7 @@ dependencies = [
15351541 "mime_guess",
15361542 "modalkit",
15371543 "open",
1544 "pretty_assertions",
15381545 "regex",
15391546 "rpassword",
15401547 "serde",
@@ -2578,6 +2585,16 @@ version = "0.1.1"
25782585source = "registry+https://github.com/rust-lang/crates.io-index"
25792586checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c"
25802587
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
25812598[[package]]
25822599name = "proc-macro-crate"
25832600version = "1.3.1"
@@ -4519,6 +4536,12 @@ dependencies = [
45194536 "linked-hash-map",
45204537]
45214538
4539[[package]]
4540name = "yansi"
4541version = "0.5.1"
4542source = "registry+https://github.com/rust-lang/crates.io-index"
4543checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec"
4544
45224545[[package]]
45234546name = "zeroize"
45244547version = "1.3.0"
Cargo.toml +1
@@ -66,6 +66,7 @@ features = ["macros", "net", "rt-multi-thread", "sync", "time"]
6666
6767[dev-dependencies]
6868lazy_static = "1.4.0"
69pretty_assertions = "1.4.0"
6970
7071[profile.release]
7172lto = true
src/message/html.rs +109 −1
@@ -237,6 +237,7 @@ pub enum StyleTreeNode {
237237 Image(Option<String>),
238238 List(StyleTreeChildren, ListStyle),
239239 Paragraph(Box<StyleTreeNode>),
240 Pre(Box<StyleTreeNode>),
240241 Reply(Box<StyleTreeNode>),
241242 Ruler,
242243 Style(Box<StyleTreeNode>, Style),
@@ -311,6 +312,39 @@ impl StyleTreeNode {
311312 child.print(printer, style);
312313 printer.commit();
313314 },
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 },
314348 StyleTreeNode::Reply(child) => {
315349 if printer.hide_reply() {
316350 return;
@@ -585,6 +619,7 @@ fn h2t(hdl: &Handle) -> StyleTreeChildren {
585619 // Other text blocks.
586620 "blockquote" => StyleTreeNode::Blockquote(c2t(&node.children.borrow())),
587621 "div" | "p" => StyleTreeNode::Paragraph(c2t(&node.children.borrow())),
622 "pre" => StyleTreeNode::Pre(c2t(&node.children.borrow())),
588623
589624 // No children.
590625 "hr" => StyleTreeNode::Ruler,
@@ -593,7 +628,7 @@ fn h2t(hdl: &Handle) -> StyleTreeChildren {
593628 "img" => StyleTreeNode::Image(attrs_to_alt(&attrs.borrow())),
594629
595630 // These don't render in any special way.
596 "a" | "details" | "html" | "pre" | "summary" | "sub" | "sup" => {
631 "a" | "details" | "html" | "summary" | "sub" | "sup" => {
597632 *c2t(&node.children.borrow())
598633 },
599634
@@ -630,6 +665,7 @@ pub fn parse_matrix_html(s: &str) -> StyleTree {
630665pub mod tests {
631666 use super::*;
632667 use crate::util::space_span;
668 use pretty_assertions::assert_eq;
633669
634670 #[test]
635671 fn test_header() {
@@ -1173,4 +1209,76 @@ pub mod tests {
11731209 ])
11741210 );
11751211 }
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 }
11761284}
src/message/printer.rs +15 −2
@@ -17,6 +17,7 @@ pub struct TextPrinter<'a> {
1717 alignment: Alignment,
1818 curr_spans: Vec<Span<'a>>,
1919 curr_width: usize,
20 literal: bool,
2021}
2122
2223impl<'a> TextPrinter<'a> {
@@ -30,6 +31,7 @@ impl<'a> TextPrinter<'a> {
3031 alignment: Alignment::Left,
3132 curr_spans: vec![],
3233 curr_width: 0,
34 literal: false,
3335 }
3436 }
3537
@@ -38,6 +40,11 @@ impl<'a> TextPrinter<'a> {
3840 self
3941 }
4042
43 pub fn literal(mut self, literal: bool) -> Self {
44 self.literal = literal;
45 self
46 }
47
4148 pub fn hide_reply(&self) -> bool {
4249 self.hide_reply
4350 }
@@ -56,6 +63,7 @@ impl<'a> TextPrinter<'a> {
5663 alignment: self.alignment,
5764 curr_spans: vec![],
5865 curr_width: 0,
66 literal: self.literal,
5967 }
6068 }
6169
@@ -162,11 +170,16 @@ impl<'a> TextPrinter<'a> {
162170
163171 for mut word in UnicodeSegmentation::split_word_bounds(s) {
164172 if let "\n" | "\r\n" = word {
173 if self.literal {
174 self.commit();
175 continue;
176 }
177
165178 // Render embedded newlines as spaces.
166179 word = " ";
167180 }
168181
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) {
170183 // Drop leading whitespace.
171184 continue;
172185 }
@@ -182,7 +195,7 @@ impl<'a> TextPrinter<'a> {
182195 // Word doesn't fit on this line, so start a new one.
183196 self.commit();
184197
185 if word.chars().all(char::is_whitespace) {
198 if !self.literal && word.chars().all(char::is_whitespace) {
186199 // Drop leading whitespace.
187200 continue;
188201 }