| @@ -15,6 +15,7 @@ use std::ops::Deref; |
| 15 | use css_color_parser::Color as CssColor; |
15 | use css_color_parser::Color as CssColor; |
| 16 | use markup5ever_rcdom::{Handle, NodeData, RcDom}; |
16 | use markup5ever_rcdom::{Handle, NodeData, RcDom}; |
| 17 | use unicode_segmentation::UnicodeSegmentation; |
17 | use unicode_segmentation::UnicodeSegmentation; |
| |
18 | use url::Url; |
| 18 | |
19 | |
| 19 | use html5ever::{ |
20 | use html5ever::{ |
| 20 | driver::{parse_fragment, ParseOpts}, |
21 | driver::{parse_fragment, ParseOpts}, |
| @@ -102,6 +103,12 @@ pub struct TableRow { |
| 102 | } |
103 | } |
| 103 | |
104 | |
| 104 | impl TableRow { |
105 | impl TableRow { |
| |
106 | pub fn gather_links(&self, urls: &mut Vec<(char, Url)>) { |
| |
107 | for (_, cell) in &self.cells { |
| |
108 | cell.gather_links(urls); |
| |
109 | } |
| |
110 | } |
| |
111 | |
| 105 | fn columns(&self) -> usize { |
112 | fn columns(&self) -> usize { |
| 106 | self.cells.len() |
113 | self.cells.len() |
| 107 | } |
114 | } |
| @@ -113,6 +120,12 @@ pub struct TableSection { |
| 113 | } |
120 | } |
| 114 | |
121 | |
| 115 | impl TableSection { |
122 | impl TableSection { |
| |
123 | pub fn gather_links(&self, urls: &mut Vec<(char, Url)>) { |
| |
124 | for row in &self.rows { |
| |
125 | row.gather_links(urls); |
| |
126 | } |
| |
127 | } |
| |
128 | |
| 116 | fn columns(&self) -> usize { |
129 | fn columns(&self) -> usize { |
| 117 | self.rows.iter().map(TableRow::columns).max().unwrap_or(0) |
130 | self.rows.iter().map(TableRow::columns).max().unwrap_or(0) |
| 118 | } |
131 | } |
| @@ -129,6 +142,12 @@ impl Table { |
| 129 | self.sections.iter().map(TableSection::columns).max().unwrap_or(0) |
142 | self.sections.iter().map(TableSection::columns).max().unwrap_or(0) |
| 130 | } |
143 | } |
| 131 | |
144 | |
| |
145 | pub fn gather_links(&self, urls: &mut Vec<(char, Url)>) { |
| |
146 | for section in &self.sections { |
| |
147 | section.gather_links(urls); |
| |
148 | } |
| |
149 | } |
| |
150 | |
| 132 | fn to_text(&self, width: usize, style: Style) -> Text { |
151 | fn to_text(&self, width: usize, style: Style) -> Text { |
| 133 | let mut text = Text::default(); |
152 | let mut text = Text::default(); |
| 134 | let columns = self.columns(); |
153 | let columns = self.columns(); |
| @@ -237,6 +256,7 @@ impl Table { |
| 237 | |
256 | |
| 238 | /// A processed HTML element that we can render to the terminal. |
257 | /// A processed HTML element that we can render to the terminal. |
| 239 | pub enum StyleTreeNode { |
258 | pub enum StyleTreeNode { |
| |
259 | Anchor(Box<StyleTreeNode>, char, Url), |
| 240 | Blockquote(Box<StyleTreeNode>), |
260 | Blockquote(Box<StyleTreeNode>), |
| 241 | Break, |
261 | Break, |
| 242 | Code(Box<StyleTreeNode>, Option<String>), |
262 | Code(Box<StyleTreeNode>, Option<String>), |
| @@ -260,10 +280,51 @@ impl StyleTreeNode { |
| 260 | printer.finish() |
280 | printer.finish() |
| 261 | } |
281 | } |
| 262 | |
282 | |
| |
283 | pub fn gather_links(&self, urls: &mut Vec<(char, Url)>) { |
| |
284 | match self { |
| |
285 | StyleTreeNode::Anchor(_, c, url) => { |
| |
286 | urls.push((*c, url.clone())); |
| |
287 | }, |
| |
288 | |
| |
289 | StyleTreeNode::Blockquote(child) | |
| |
290 | StyleTreeNode::Code(child, _) | |
| |
291 | StyleTreeNode::Header(child, _) | |
| |
292 | StyleTreeNode::Paragraph(child) | |
| |
293 | StyleTreeNode::Pre(child) | |
| |
294 | StyleTreeNode::Reply(child) | |
| |
295 | StyleTreeNode::Style(child, _) => { |
| |
296 | child.gather_links(urls); |
| |
297 | }, |
| |
298 | |
| |
299 | StyleTreeNode::List(children, _) | StyleTreeNode::Sequence(children) => { |
| |
300 | for child in children { |
| |
301 | child.gather_links(urls); |
| |
302 | } |
| |
303 | }, |
| |
304 | |
| |
305 | StyleTreeNode::Table(table) => { |
| |
306 | table.gather_links(urls); |
| |
307 | }, |
| |
308 | |
| |
309 | StyleTreeNode::Image(_) => {}, |
| |
310 | StyleTreeNode::Ruler => {}, |
| |
311 | StyleTreeNode::Text(_) => {}, |
| |
312 | StyleTreeNode::Break => {}, |
| |
313 | } |
| |
314 | } |
| |
315 | |
| 263 | pub fn print<'a>(&'a self, printer: &mut TextPrinter<'a>, style: Style) { |
316 | pub fn print<'a>(&'a self, printer: &mut TextPrinter<'a>, style: Style) { |
| 264 | let width = printer.width(); |
317 | let width = printer.width(); |
| 265 | |
318 | |
| 266 | match self { |
319 | match self { |
| |
320 | StyleTreeNode::Anchor(child, c, _) => { |
| |
321 | let bold = style.add_modifier(StyleModifier::BOLD); |
| |
322 | child.print(printer, bold); |
| |
323 | |
| |
324 | let link = format!("[{c}]"); |
| |
325 | let span = Span::styled(link, style); |
| |
326 | printer.push_span_nobreak(span); |
| |
327 | }, |
| 267 | StyleTreeNode::Blockquote(child) => { |
328 | StyleTreeNode::Blockquote(child) => { |
| 268 | let mut subp = printer.sub(4); |
329 | let mut subp = printer.sub(4); |
| 269 | child.print(&mut subp, style); |
330 | child.print(&mut subp, style); |
| @@ -393,6 +454,16 @@ pub struct StyleTree { |
| 393 | } |
454 | } |
| 394 | |
455 | |
| 395 | impl StyleTree { |
456 | impl StyleTree { |
| |
457 | pub fn get_links(&self) -> Vec<(char, Url)> { |
| |
458 | let mut links = Vec::new(); |
| |
459 | |
| |
460 | for child in &self.children { |
| |
461 | child.gather_links(&mut links); |
| |
462 | } |
| |
463 | |
| |
464 | return links; |
| |
465 | } |
| |
466 | |
| 396 | pub fn to_text(&self, width: usize, style: Style, hide_reply: bool) -> Text<'_> { |
467 | pub fn to_text(&self, width: usize, style: Style, hide_reply: bool) -> Text<'_> { |
| 397 | let mut printer = TextPrinter::new(width, style, hide_reply); |
468 | let mut printer = TextPrinter::new(width, style, hide_reply); |
| 398 | |
469 | |
| @@ -404,17 +475,41 @@ impl StyleTree { |
| 404 | } |
475 | } |
| 405 | } |
476 | } |
| 406 | |
477 | |
| 407 | fn c2c(handles: &[Handle]) -> Vec<StyleTreeNode> { |
478 | pub struct TreeGenState { |
| 408 | handles.iter().flat_map(h2t).collect() |
479 | link_num: u8, |
| 409 | } |
480 | } |
| 410 | |
481 | |
| 411 | fn c2t(handles: &[Handle]) -> Box<StyleTreeNode> { |
482 | impl TreeGenState { |
| 412 | let node = StyleTreeNode::Sequence(c2c(handles)); |
483 | fn next_link_char(&mut self) -> Option<char> { |
| |
484 | let num = self.link_num; |
| |
485 | |
| |
486 | if num < 62 { |
| |
487 | self.link_num = num + 1; |
| |
488 | } |
| |
489 | |
| |
490 | if num < 10 { |
| |
491 | Some((num + b'0') as char) |
| |
492 | } else if num < 36 { |
| |
493 | Some((num - 10 + b'a') as char) |
| |
494 | } else if num < 62 { |
| |
495 | Some((num - 36 + b'A') as char) |
| |
496 | } else { |
| |
497 | None |
| |
498 | } |
| |
499 | } |
| |
500 | } |
| |
501 | |
| |
502 | fn c2c(handles: &[Handle], state: &mut TreeGenState) -> Vec<StyleTreeNode> { |
| |
503 | handles.iter().flat_map(|h| h2t(h, state)).collect() |
| |
504 | } |
| |
505 | |
| |
506 | fn c2t(handles: &[Handle], state: &mut TreeGenState) -> Box<StyleTreeNode> { |
| |
507 | let node = StyleTreeNode::Sequence(c2c(handles, state)); |
| 413 | |
508 | |
| 414 | Box::new(node) |
509 | Box::new(node) |
| 415 | } |
510 | } |
| 416 | |
511 | |
| 417 | fn get_node(hdl: &Handle, want: &str) -> Option<StyleTreeNode> { |
512 | fn get_node(hdl: &Handle, want: &str, state: &mut TreeGenState) -> Option<StyleTreeNode> { |
| 418 | let node = hdl.deref(); |
513 | let node = hdl.deref(); |
| 419 | |
514 | |
| 420 | if let NodeData::Element { name, .. } = &node.data { |
515 | if let NodeData::Element { name, .. } = &node.data { |
| @@ -422,26 +517,26 @@ fn get_node(hdl: &Handle, want: &str) -> Option<StyleTreeNode> { |
| 422 | return None; |
517 | return None; |
| 423 | } |
518 | } |
| 424 | |
519 | |
| 425 | let c = c2c(&node.children.borrow()); |
520 | let c = c2c(&node.children.borrow(), state); |
| 426 | return Some(StyleTreeNode::Sequence(c)); |
521 | return Some(StyleTreeNode::Sequence(c)); |
| 427 | } else { |
522 | } else { |
| 428 | return None; |
523 | return None; |
| 429 | } |
524 | } |
| 430 | } |
525 | } |
| 431 | |
526 | |
| 432 | fn li2t(hdl: &Handle) -> Option<StyleTreeNode> { |
527 | fn li2t(hdl: &Handle, state: &mut TreeGenState) -> Option<StyleTreeNode> { |
| 433 | get_node(hdl, "li") |
528 | get_node(hdl, "li", state) |
| 434 | } |
529 | } |
| 435 | |
530 | |
| 436 | fn table_cell(hdl: &Handle) -> Option<(CellType, StyleTreeNode)> { |
531 | fn table_cell(hdl: &Handle, state: &mut TreeGenState) -> Option<(CellType, StyleTreeNode)> { |
| 437 | if let Some(node) = get_node(hdl, "th") { |
532 | if let Some(node) = get_node(hdl, "th", state) { |
| 438 | return Some((CellType::Header, node)); |
533 | return Some((CellType::Header, node)); |
| 439 | } |
534 | } |
| 440 | |
535 | |
| 441 | Some((CellType::Data, get_node(hdl, "td")?)) |
536 | Some((CellType::Data, get_node(hdl, "td", state)?)) |
| 442 | } |
537 | } |
| 443 | |
538 | |
| 444 | fn table_row(hdl: &Handle) -> Option<TableRow> { |
539 | fn table_row(hdl: &Handle, state: &mut TreeGenState) -> Option<TableRow> { |
| 445 | let node = hdl.deref(); |
540 | let node = hdl.deref(); |
| 446 | |
541 | |
| 447 | if let NodeData::Element { name, .. } = &node.data { |
542 | if let NodeData::Element { name, .. } = &node.data { |
| @@ -449,20 +544,20 @@ fn table_row(hdl: &Handle) -> Option<TableRow> { |
| 449 | return None; |
544 | return None; |
| 450 | } |
545 | } |
| 451 | |
546 | |
| 452 | let cells = table_cells(&node.children.borrow()); |
547 | let cells = table_cells(&node.children.borrow(), state); |
| 453 | return Some(TableRow { cells }); |
548 | return Some(TableRow { cells }); |
| 454 | } else { |
549 | } else { |
| 455 | return None; |
550 | return None; |
| 456 | } |
551 | } |
| 457 | } |
552 | } |
| 458 | |
553 | |
| 459 | fn table_section(hdl: &Handle) -> Option<TableSection> { |
554 | fn table_section(hdl: &Handle, state: &mut TreeGenState) -> Option<TableSection> { |
| 460 | let node = hdl.deref(); |
555 | let node = hdl.deref(); |
| 461 | |
556 | |
| 462 | if let NodeData::Element { name, .. } = &node.data { |
557 | if let NodeData::Element { name, .. } = &node.data { |
| 463 | match name.local.as_ref() { |
558 | match name.local.as_ref() { |
| 464 | "thead" | "tbody" => { |
559 | "thead" | "tbody" => { |
| 465 | let rows = table_rows(&node.children.borrow()); |
560 | let rows = table_rows(&node.children.borrow(), state); |
| 466 | |
561 | |
| 467 | Some(TableSection { rows }) |
562 | Some(TableSection { rows }) |
| 468 | }, |
563 | }, |
| @@ -473,20 +568,20 @@ fn table_section(hdl: &Handle) -> Option<TableSection> { |
| 473 | } |
568 | } |
| 474 | } |
569 | } |
| 475 | |
570 | |
| 476 | fn table_cells(handles: &[Handle]) -> Vec<(CellType, StyleTreeNode)> { |
571 | fn table_cells(handles: &[Handle], state: &mut TreeGenState) -> Vec<(CellType, StyleTreeNode)> { |
| 477 | handles.iter().filter_map(table_cell).collect() |
572 | handles.iter().filter_map(|h| table_cell(h, state)).collect() |
| 478 | } |
573 | } |
| 479 | |
574 | |
| 480 | fn table_rows(handles: &[Handle]) -> Vec<TableRow> { |
575 | fn table_rows(handles: &[Handle], state: &mut TreeGenState) -> Vec<TableRow> { |
| 481 | handles.iter().filter_map(table_row).collect() |
576 | handles.iter().filter_map(|h| table_row(h, state)).collect() |
| 482 | } |
577 | } |
| 483 | |
578 | |
| 484 | fn table_sections(handles: &[Handle]) -> Vec<TableSection> { |
579 | fn table_sections(handles: &[Handle], state: &mut TreeGenState) -> Vec<TableSection> { |
| 485 | handles.iter().filter_map(table_section).collect() |
580 | handles.iter().filter_map(|h| table_section(h, state)).collect() |
| 486 | } |
581 | } |
| 487 | |
582 | |
| 488 | fn lic2t(handles: &[Handle]) -> StyleTreeChildren { |
583 | fn lic2t(handles: &[Handle], state: &mut TreeGenState) -> StyleTreeChildren { |
| 489 | handles.iter().filter_map(li2t).collect() |
584 | handles.iter().filter_map(|h| li2t(h, state)).collect() |
| 490 | } |
585 | } |
| 491 | |
586 | |
| 492 | fn attrs_to_alt(attrs: &[Attribute]) -> Option<String> { |
587 | fn attrs_to_alt(attrs: &[Attribute]) -> Option<String> { |
| @@ -501,6 +596,18 @@ fn attrs_to_alt(attrs: &[Attribute]) -> Option<String> { |
| 501 | return None; |
596 | return None; |
| 502 | } |
597 | } |
| 503 | |
598 | |
| |
599 | fn attrs_to_href(attrs: &[Attribute]) -> Option<String> { |
| |
600 | for attr in attrs { |
| |
601 | if attr.name.local.as_ref() != "href" { |
| |
602 | continue; |
| |
603 | } |
| |
604 | |
| |
605 | return Some(attr.value.to_string()); |
| |
606 | } |
| |
607 | |
| |
608 | return None; |
| |
609 | } |
| |
610 | |
| 504 | fn attrs_to_language(attrs: &[Attribute]) -> Option<String> { |
611 | fn attrs_to_language(attrs: &[Attribute]) -> Option<String> { |
| 505 | for attr in attrs { |
612 | for attr in attrs { |
| 506 | if attr.name.local.as_ref() != "class" { |
613 | if attr.name.local.as_ref() != "class" { |
| @@ -541,75 +648,95 @@ fn attrs_to_style(attrs: &[Attribute]) -> Style { |
| 541 | return style; |
648 | return style; |
| 542 | } |
649 | } |
| 543 | |
650 | |
| 544 | fn h2t(hdl: &Handle) -> StyleTreeChildren { |
651 | fn h2t(hdl: &Handle, state: &mut TreeGenState) -> StyleTreeChildren { |
| 545 | let node = hdl.deref(); |
652 | let node = hdl.deref(); |
| 546 | |
653 | |
| 547 | let tree = match &node.data { |
654 | let tree = match &node.data { |
| 548 | NodeData::Document => *c2t(node.children.borrow().as_slice()), |
655 | NodeData::Document => *c2t(node.children.borrow().as_slice(), state), |
| 549 | NodeData::Text { contents } => StyleTreeNode::Text(contents.borrow().to_string()), |
656 | NodeData::Text { contents } => StyleTreeNode::Text(contents.borrow().to_string()), |
| 550 | NodeData::Element { name, attrs, .. } => { |
657 | NodeData::Element { name, attrs, .. } => { |
| 551 | match name.local.as_ref() { |
658 | match name.local.as_ref() { |
| 552 | // Message that this one replies to. |
659 | // Message that this one replies to. |
| 553 | "mx-reply" => StyleTreeNode::Reply(c2t(&node.children.borrow())), |
660 | "mx-reply" => StyleTreeNode::Reply(c2t(&node.children.borrow(), state)), |
| |
661 | |
| |
662 | // Links |
| |
663 | "a" => { |
| |
664 | let c = c2t(&node.children.borrow(), state); |
| |
665 | let h = attrs_to_href(&attrs.borrow()).and_then(|u| Url::parse(&u).ok()); |
| |
666 | |
| |
667 | if let Some(h) = h { |
| |
668 | if let Some(n) = state.next_link_char() { |
| |
669 | StyleTreeNode::Anchor(c, n, h) |
| |
670 | } else { |
| |
671 | *c |
| |
672 | } |
| |
673 | } else { |
| |
674 | *c |
| |
675 | } |
| |
676 | }, |
| 554 | |
677 | |
| 555 | // Style change |
678 | // Style change |
| 556 | "b" | "strong" => { |
679 | "b" | "strong" => { |
| 557 | let c = c2t(&node.children.borrow()); |
680 | let c = c2t(&node.children.borrow(), state); |
| 558 | let s = Style::default().add_modifier(StyleModifier::BOLD); |
681 | let s = Style::default().add_modifier(StyleModifier::BOLD); |
| 559 | |
682 | |
| 560 | StyleTreeNode::Style(c, s) |
683 | StyleTreeNode::Style(c, s) |
| 561 | }, |
684 | }, |
| 562 | "font" => { |
685 | "font" => { |
| 563 | let c = c2t(&node.children.borrow()); |
686 | let c = c2t(&node.children.borrow(), state); |
| 564 | let s = attrs_to_style(&attrs.borrow()); |
687 | let s = attrs_to_style(&attrs.borrow()); |
| 565 | |
688 | |
| 566 | StyleTreeNode::Style(c, s) |
689 | StyleTreeNode::Style(c, s) |
| 567 | }, |
690 | }, |
| 568 | "em" | "i" => { |
691 | "em" | "i" => { |
| 569 | let c = c2t(&node.children.borrow()); |
692 | let c = c2t(&node.children.borrow(), state); |
| 570 | let s = Style::default().add_modifier(StyleModifier::ITALIC); |
693 | let s = Style::default().add_modifier(StyleModifier::ITALIC); |
| 571 | |
694 | |
| 572 | StyleTreeNode::Style(c, s) |
695 | StyleTreeNode::Style(c, s) |
| 573 | }, |
696 | }, |
| 574 | "span" => { |
697 | "span" => { |
| 575 | let c = c2t(&node.children.borrow()); |
698 | let c = c2t(&node.children.borrow(), state); |
| 576 | let s = attrs_to_style(&attrs.borrow()); |
699 | let s = attrs_to_style(&attrs.borrow()); |
| 577 | |
700 | |
| 578 | StyleTreeNode::Style(c, s) |
701 | StyleTreeNode::Style(c, s) |
| 579 | }, |
702 | }, |
| 580 | "del" | "strike" => { |
703 | "del" | "strike" => { |
| 581 | let c = c2t(&node.children.borrow()); |
704 | let c = c2t(&node.children.borrow(), state); |
| 582 | let s = Style::default().add_modifier(StyleModifier::CROSSED_OUT); |
705 | let s = Style::default().add_modifier(StyleModifier::CROSSED_OUT); |
| 583 | |
706 | |
| 584 | StyleTreeNode::Style(c, s) |
707 | StyleTreeNode::Style(c, s) |
| 585 | }, |
708 | }, |
| 586 | "u" => { |
709 | "u" => { |
| 587 | let c = c2t(&node.children.borrow()); |
710 | let c = c2t(&node.children.borrow(), state); |
| 588 | let s = Style::default().add_modifier(StyleModifier::UNDERLINED); |
711 | let s = Style::default().add_modifier(StyleModifier::UNDERLINED); |
| 589 | |
712 | |
| 590 | StyleTreeNode::Style(c, s) |
713 | StyleTreeNode::Style(c, s) |
| 591 | }, |
714 | }, |
| 592 | |
715 | |
| 593 | // Lists |
716 | // Lists |
| 594 | "ol" => StyleTreeNode::List(lic2t(&node.children.borrow()), ListStyle::Ordered), |
717 | "ol" => { |
| 595 | "ul" => StyleTreeNode::List(lic2t(&node.children.borrow()), ListStyle::Unordered), |
718 | StyleTreeNode::List(lic2t(&node.children.borrow(), state), ListStyle::Ordered) |
| |
719 | }, |
| |
720 | "ul" => { |
| |
721 | StyleTreeNode::List(lic2t(&node.children.borrow(), state), ListStyle::Unordered) |
| |
722 | }, |
| 596 | |
723 | |
| 597 | // Headers |
724 | // Headers |
| 598 | "h1" => StyleTreeNode::Header(c2t(&node.children.borrow()), 1), |
725 | "h1" => StyleTreeNode::Header(c2t(&node.children.borrow(), state), 1), |
| 599 | "h2" => StyleTreeNode::Header(c2t(&node.children.borrow()), 2), |
726 | "h2" => StyleTreeNode::Header(c2t(&node.children.borrow(), state), 2), |
| 600 | "h3" => StyleTreeNode::Header(c2t(&node.children.borrow()), 3), |
727 | "h3" => StyleTreeNode::Header(c2t(&node.children.borrow(), state), 3), |
| 601 | "h4" => StyleTreeNode::Header(c2t(&node.children.borrow()), 4), |
728 | "h4" => StyleTreeNode::Header(c2t(&node.children.borrow(), state), 4), |
| 602 | "h5" => StyleTreeNode::Header(c2t(&node.children.borrow()), 5), |
729 | "h5" => StyleTreeNode::Header(c2t(&node.children.borrow(), state), 5), |
| 603 | "h6" => StyleTreeNode::Header(c2t(&node.children.borrow()), 6), |
730 | "h6" => StyleTreeNode::Header(c2t(&node.children.borrow(), state), 6), |
| 604 | |
731 | |
| 605 | // Table |
732 | // Table |
| 606 | "table" => { |
733 | "table" => { |
| 607 | let sections = table_sections(&node.children.borrow()); |
734 | let sections = table_sections(&node.children.borrow(), state); |
| 608 | let caption = node |
735 | let caption = node |
| 609 | .children |
736 | .children |
| 610 | .borrow() |
737 | .borrow() |
| 611 | .iter() |
738 | .iter() |
| 612 | .find_map(|hdl| get_node(hdl, "caption")) |
739 | .find_map(|hdl| get_node(hdl, "caption", state)) |
| 613 | .map(Box::new); |
740 | .map(Box::new); |
| 614 | let table = Table { caption, sections }; |
741 | let table = Table { caption, sections }; |
| 615 | |
742 | |
| @@ -618,16 +745,16 @@ fn h2t(hdl: &Handle) -> StyleTreeChildren { |
| 618 | |
745 | |
| 619 | // Code blocks. |
746 | // Code blocks. |
| 620 | "code" => { |
747 | "code" => { |
| 621 | let c = c2t(&node.children.borrow()); |
748 | let c = c2t(&node.children.borrow(), state); |
| 622 | let l = attrs_to_language(&attrs.borrow()); |
749 | let l = attrs_to_language(&attrs.borrow()); |
| 623 | |
750 | |
| 624 | StyleTreeNode::Code(c, l) |
751 | StyleTreeNode::Code(c, l) |
| 625 | }, |
752 | }, |
| 626 | |
753 | |
| 627 | // Other text blocks. |
754 | // Other text blocks. |
| 628 | "blockquote" => StyleTreeNode::Blockquote(c2t(&node.children.borrow())), |
755 | "blockquote" => StyleTreeNode::Blockquote(c2t(&node.children.borrow(), state)), |
| 629 | "div" | "p" => StyleTreeNode::Paragraph(c2t(&node.children.borrow())), |
756 | "div" | "p" => StyleTreeNode::Paragraph(c2t(&node.children.borrow(), state)), |
| 630 | "pre" => StyleTreeNode::Pre(c2t(&node.children.borrow())), |
757 | "pre" => StyleTreeNode::Pre(c2t(&node.children.borrow(), state)), |
| 631 | |
758 | |
| 632 | // No children. |
759 | // No children. |
| 633 | "hr" => StyleTreeNode::Ruler, |
760 | "hr" => StyleTreeNode::Ruler, |
| @@ -636,8 +763,8 @@ fn h2t(hdl: &Handle) -> StyleTreeChildren { |
| 636 | "img" => StyleTreeNode::Image(attrs_to_alt(&attrs.borrow())), |
763 | "img" => StyleTreeNode::Image(attrs_to_alt(&attrs.borrow())), |
| 637 | |
764 | |
| 638 | // These don't render in any special way. |
765 | // These don't render in any special way. |
| 639 | "a" | "details" | "html" | "summary" | "sub" | "sup" => { |
766 | "details" | "html" | "summary" | "sub" | "sup" => { |
| 640 | *c2t(&node.children.borrow()) |
767 | *c2t(&node.children.borrow(), state) |
| 641 | }, |
768 | }, |
| 642 | |
769 | |
| 643 | _ => return vec![], |
770 | _ => return vec![], |
| @@ -654,7 +781,10 @@ fn h2t(hdl: &Handle) -> StyleTreeChildren { |
| 654 | } |
781 | } |
| 655 | |
782 | |
| 656 | fn dom_to_style_tree(dom: RcDom) -> StyleTree { |
783 | fn dom_to_style_tree(dom: RcDom) -> StyleTree { |
| 657 | StyleTree { children: h2t(&dom.document) } |
784 | let mut state = TreeGenState { link_num: 0 }; |
| |
785 | let children = h2t(&dom.document, &mut state); |
| |
786 | |
| |
787 | StyleTree { children } |
| 658 | } |
788 | } |
| 659 | |
789 | |
| 660 | /// Parse an HTML document from a string. |
790 | /// Parse an HTML document from a string. |