| @@ -237,6 +237,7 @@ pub enum StyleTreeNode { |
| 237 | 237 | Image(Option<String>), |
| 238 | 238 | List(StyleTreeChildren, ListStyle), |
| 239 | 239 | Paragraph(Box<StyleTreeNode>), |
| 240 | Pre(Box<StyleTreeNode>), |
| 240 | 241 | Reply(Box<StyleTreeNode>), |
| 241 | 242 | Ruler, |
| 242 | 243 | Style(Box<StyleTreeNode>, Style), |
| @@ -311,6 +312,39 @@ impl StyleTreeNode { |
| 311 | 312 | child.print(printer, style); |
| 312 | 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 | 348 | StyleTreeNode::Reply(child) => { |
| 315 | 349 | if printer.hide_reply() { |
| 316 | 350 | return; |
| @@ -585,6 +619,7 @@ fn h2t(hdl: &Handle) -> StyleTreeChildren { |
| 585 | 619 | // Other text blocks. |
| 586 | 620 | "blockquote" => StyleTreeNode::Blockquote(c2t(&node.children.borrow())), |
| 587 | 621 | "div" | "p" => StyleTreeNode::Paragraph(c2t(&node.children.borrow())), |
| 622 | "pre" => StyleTreeNode::Pre(c2t(&node.children.borrow())), |
| 588 | 623 | |
| 589 | 624 | // No children. |
| 590 | 625 | "hr" => StyleTreeNode::Ruler, |
| @@ -593,7 +628,7 @@ fn h2t(hdl: &Handle) -> StyleTreeChildren { |
| 593 | 628 | "img" => StyleTreeNode::Image(attrs_to_alt(&attrs.borrow())), |
| 594 | 629 | |
| 595 | 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 | 632 | *c2t(&node.children.borrow()) |
| 598 | 633 | }, |
| 599 | 634 | |
| @@ -630,6 +665,7 @@ pub fn parse_matrix_html(s: &str) -> StyleTree { |
| 630 | 665 | pub mod tests { |
| 631 | 666 | use super::*; |
| 632 | 667 | use crate::util::space_span; |
| 668 | use pretty_assertions::assert_eq; |
| 633 | 669 | |
| 634 | 670 | #[test] |
| 635 | 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() -> 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 | } |