| @@ -15,6 +15,7 @@ use std::ops::Deref; |
| 15 | 15 | use css_color_parser::Color as CssColor; |
| 16 | 16 | use markup5ever_rcdom::{Handle, NodeData, RcDom}; |
| 17 | 17 | use unicode_segmentation::UnicodeSegmentation; |
| 18 | use url::Url; |
| 18 | 19 | |
| 19 | 20 | use html5ever::{ |
| 20 | 21 | driver::{parse_fragment, ParseOpts}, |
| @@ -102,6 +103,12 @@ pub struct TableRow { |
| 102 | 103 | } |
| 103 | 104 | |
| 104 | 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 | 112 | fn columns(&self) -> usize { |
| 106 | 113 | self.cells.len() |
| 107 | 114 | } |
| @@ -113,6 +120,12 @@ pub struct TableSection { |
| 113 | 120 | } |
| 114 | 121 | |
| 115 | 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 | 129 | fn columns(&self) -> usize { |
| 117 | 130 | self.rows.iter().map(TableRow::columns).max().unwrap_or(0) |
| 118 | 131 | } |
| @@ -129,6 +142,12 @@ impl Table { |
| 129 | 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 | 151 | fn to_text(&self, width: usize, style: Style) -> Text { |
| 133 | 152 | let mut text = Text::default(); |
| 134 | 153 | let columns = self.columns(); |
| @@ -237,6 +256,7 @@ impl Table { |
| 237 | 256 | |
| 238 | 257 | /// A processed HTML element that we can render to the terminal. |
| 239 | 258 | pub enum StyleTreeNode { |
| 259 | Anchor(Box<StyleTreeNode>, char, Url), |
| 240 | 260 | Blockquote(Box<StyleTreeNode>), |
| 241 | 261 | Break, |
| 242 | 262 | Code(Box<StyleTreeNode>, Option<String>), |
| @@ -260,10 +280,51 @@ impl StyleTreeNode { |
| 260 | 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 | 316 | pub fn print<'a>(&'a self, printer: &mut TextPrinter<'a>, style: Style) { |
| 264 | 317 | let width = printer.width(); |
| 265 | 318 | |
| 266 | 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 | 328 | StyleTreeNode::Blockquote(child) => { |
| 268 | 329 | let mut subp = printer.sub(4); |
| 269 | 330 | child.print(&mut subp, style); |
| @@ -393,6 +454,16 @@ pub struct StyleTree { |
| 393 | 454 | } |
| 394 | 455 | |
| 395 | 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 | 467 | pub fn to_text(&self, width: usize, style: Style, hide_reply: bool) -> Text<'_> { |
| 397 | 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> { |
| 408 | | handles.iter().flat_map(h2t).collect() |
| 478 | pub struct TreeGenState { |
| 479 | link_num: u8, |
| 409 | 480 | } |
| 410 | 481 | |
| 411 | | fn c2t(handles: &[Handle]) -> Box<StyleTreeNode> { |
| 412 | | let node = StyleTreeNode::Sequence(c2c(handles)); |
| 482 | impl TreeGenState { |
| 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 | 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 | 513 | let node = hdl.deref(); |
| 419 | 514 | |
| 420 | 515 | if let NodeData::Element { name, .. } = &node.data { |
| @@ -422,26 +517,26 @@ fn get_node(hdl: &Handle, want: &str) -> Option<StyleTreeNode> { |
| 422 | 517 | return None; |
| 423 | 518 | } |
| 424 | 519 | |
| 425 | | let c = c2c(&node.children.borrow()); |
| 520 | let c = c2c(&node.children.borrow(), state); |
| 426 | 521 | return Some(StyleTreeNode::Sequence(c)); |
| 427 | 522 | } else { |
| 428 | 523 | return None; |
| 429 | 524 | } |
| 430 | 525 | } |
| 431 | 526 | |
| 432 | | fn li2t(hdl: &Handle) -> Option<StyleTreeNode> { |
| 433 | | get_node(hdl, "li") |
| 527 | fn li2t(hdl: &Handle, state: &mut TreeGenState) -> Option<StyleTreeNode> { |
| 528 | get_node(hdl, "li", state) |
| 434 | 529 | } |
| 435 | 530 | |
| 436 | | fn table_cell(hdl: &Handle) -> Option<(CellType, StyleTreeNode)> { |
| 437 | | if let Some(node) = get_node(hdl, "th") { |
| 531 | fn table_cell(hdl: &Handle, state: &mut TreeGenState) -> Option<(CellType, StyleTreeNode)> { |
| 532 | if let Some(node) = get_node(hdl, "th", state) { |
| 438 | 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 | 540 | let node = hdl.deref(); |
| 446 | 541 | |
| 447 | 542 | if let NodeData::Element { name, .. } = &node.data { |
| @@ -449,20 +544,20 @@ fn table_row(hdl: &Handle) -> Option<TableRow> { |
| 449 | 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 | 548 | return Some(TableRow { cells }); |
| 454 | 549 | } else { |
| 455 | 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 | 555 | let node = hdl.deref(); |
| 461 | 556 | |
| 462 | 557 | if let NodeData::Element { name, .. } = &node.data { |
| 463 | 558 | match name.local.as_ref() { |
| 464 | 559 | "thead" | "tbody" => { |
| 465 | | let rows = table_rows(&node.children.borrow()); |
| 560 | let rows = table_rows(&node.children.borrow(), state); |
| 466 | 561 | |
| 467 | 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)> { |
| 477 | | handles.iter().filter_map(table_cell).collect() |
| 571 | fn table_cells(handles: &[Handle], state: &mut TreeGenState) -> Vec<(CellType, StyleTreeNode)> { |
| 572 | handles.iter().filter_map(|h| table_cell(h, state)).collect() |
| 478 | 573 | } |
| 479 | 574 | |
| 480 | | fn table_rows(handles: &[Handle]) -> Vec<TableRow> { |
| 481 | | handles.iter().filter_map(table_row).collect() |
| 575 | fn table_rows(handles: &[Handle], state: &mut TreeGenState) -> Vec<TableRow> { |
| 576 | handles.iter().filter_map(|h| table_row(h, state)).collect() |
| 482 | 577 | } |
| 483 | 578 | |
| 484 | | fn table_sections(handles: &[Handle]) -> Vec<TableSection> { |
| 485 | | handles.iter().filter_map(table_section).collect() |
| 579 | fn table_sections(handles: &[Handle], state: &mut TreeGenState) -> Vec<TableSection> { |
| 580 | handles.iter().filter_map(|h| table_section(h, state)).collect() |
| 486 | 581 | } |
| 487 | 582 | |
| 488 | | fn lic2t(handles: &[Handle]) -> StyleTreeChildren { |
| 489 | | handles.iter().filter_map(li2t).collect() |
| 583 | fn lic2t(handles: &[Handle], state: &mut TreeGenState) -> StyleTreeChildren { |
| 584 | handles.iter().filter_map(|h| li2t(h, state)).collect() |
| 490 | 585 | } |
| 491 | 586 | |
| 492 | 587 | fn attrs_to_alt(attrs: &[Attribute]) -> Option<String> { |
| @@ -501,6 +596,18 @@ fn attrs_to_alt(attrs: &[Attribute]) -> Option<String> { |
| 501 | 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 | 611 | fn attrs_to_language(attrs: &[Attribute]) -> Option<String> { |
| 505 | 612 | for attr in attrs { |
| 506 | 613 | if attr.name.local.as_ref() != "class" { |
| @@ -541,75 +648,95 @@ fn attrs_to_style(attrs: &[Attribute]) -> Style { |
| 541 | 648 | return style; |
| 542 | 649 | } |
| 543 | 650 | |
| 544 | | fn h2t(hdl: &Handle) -> StyleTreeChildren { |
| 651 | fn h2t(hdl: &Handle, state: &mut TreeGenState) -> StyleTreeChildren { |
| 545 | 652 | let node = hdl.deref(); |
| 546 | 653 | |
| 547 | 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 | 656 | NodeData::Text { contents } => StyleTreeNode::Text(contents.borrow().to_string()), |
| 550 | 657 | NodeData::Element { name, attrs, .. } => { |
| 551 | 658 | match name.local.as_ref() { |
| 552 | 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 | 678 | // Style change |
| 556 | 679 | "b" | "strong" => { |
| 557 | | let c = c2t(&node.children.borrow()); |
| 680 | let c = c2t(&node.children.borrow(), state); |
| 558 | 681 | let s = Style::default().add_modifier(StyleModifier::BOLD); |
| 559 | 682 | |
| 560 | 683 | StyleTreeNode::Style(c, s) |
| 561 | 684 | }, |
| 562 | 685 | "font" => { |
| 563 | | let c = c2t(&node.children.borrow()); |
| 686 | let c = c2t(&node.children.borrow(), state); |
| 564 | 687 | let s = attrs_to_style(&attrs.borrow()); |
| 565 | 688 | |
| 566 | 689 | StyleTreeNode::Style(c, s) |
| 567 | 690 | }, |
| 568 | 691 | "em" | "i" => { |
| 569 | | let c = c2t(&node.children.borrow()); |
| 692 | let c = c2t(&node.children.borrow(), state); |
| 570 | 693 | let s = Style::default().add_modifier(StyleModifier::ITALIC); |
| 571 | 694 | |
| 572 | 695 | StyleTreeNode::Style(c, s) |
| 573 | 696 | }, |
| 574 | 697 | "span" => { |
| 575 | | let c = c2t(&node.children.borrow()); |
| 698 | let c = c2t(&node.children.borrow(), state); |
| 576 | 699 | let s = attrs_to_style(&attrs.borrow()); |
| 577 | 700 | |
| 578 | 701 | StyleTreeNode::Style(c, s) |
| 579 | 702 | }, |
| 580 | 703 | "del" | "strike" => { |
| 581 | | let c = c2t(&node.children.borrow()); |
| 704 | let c = c2t(&node.children.borrow(), state); |
| 582 | 705 | let s = Style::default().add_modifier(StyleModifier::CROSSED_OUT); |
| 583 | 706 | |
| 584 | 707 | StyleTreeNode::Style(c, s) |
| 585 | 708 | }, |
| 586 | 709 | "u" => { |
| 587 | | let c = c2t(&node.children.borrow()); |
| 710 | let c = c2t(&node.children.borrow(), state); |
| 588 | 711 | let s = Style::default().add_modifier(StyleModifier::UNDERLINED); |
| 589 | 712 | |
| 590 | 713 | StyleTreeNode::Style(c, s) |
| 591 | 714 | }, |
| 592 | 715 | |
| 593 | 716 | // Lists |
| 594 | | "ol" => StyleTreeNode::List(lic2t(&node.children.borrow()), ListStyle::Ordered), |
| 595 | | "ul" => StyleTreeNode::List(lic2t(&node.children.borrow()), ListStyle::Unordered), |
| 717 | "ol" => { |
| 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 | 724 | // Headers |
| 598 | | "h1" => StyleTreeNode::Header(c2t(&node.children.borrow()), 1), |
| 599 | | "h2" => StyleTreeNode::Header(c2t(&node.children.borrow()), 2), |
| 600 | | "h3" => StyleTreeNode::Header(c2t(&node.children.borrow()), 3), |
| 601 | | "h4" => StyleTreeNode::Header(c2t(&node.children.borrow()), 4), |
| 602 | | "h5" => StyleTreeNode::Header(c2t(&node.children.borrow()), 5), |
| 603 | | "h6" => StyleTreeNode::Header(c2t(&node.children.borrow()), 6), |
| 725 | "h1" => StyleTreeNode::Header(c2t(&node.children.borrow(), state), 1), |
| 726 | "h2" => StyleTreeNode::Header(c2t(&node.children.borrow(), state), 2), |
| 727 | "h3" => StyleTreeNode::Header(c2t(&node.children.borrow(), state), 3), |
| 728 | "h4" => StyleTreeNode::Header(c2t(&node.children.borrow(), state), 4), |
| 729 | "h5" => StyleTreeNode::Header(c2t(&node.children.borrow(), state), 5), |
| 730 | "h6" => StyleTreeNode::Header(c2t(&node.children.borrow(), state), 6), |
| 604 | 731 | |
| 605 | 732 | // Table |
| 606 | 733 | "table" => { |
| 607 | | let sections = table_sections(&node.children.borrow()); |
| 734 | let sections = table_sections(&node.children.borrow(), state); |
| 608 | 735 | let caption = node |
| 609 | 736 | .children |
| 610 | 737 | .borrow() |
| 611 | 738 | .iter() |
| 612 | | .find_map(|hdl| get_node(hdl, "caption")) |
| 739 | .find_map(|hdl| get_node(hdl, "caption", state)) |
| 613 | 740 | .map(Box::new); |
| 614 | 741 | let table = Table { caption, sections }; |
| 615 | 742 | |
| @@ -618,16 +745,16 @@ fn h2t(hdl: &Handle) -> StyleTreeChildren { |
| 618 | 745 | |
| 619 | 746 | // Code blocks. |
| 620 | 747 | "code" => { |
| 621 | | let c = c2t(&node.children.borrow()); |
| 748 | let c = c2t(&node.children.borrow(), state); |
| 622 | 749 | let l = attrs_to_language(&attrs.borrow()); |
| 623 | 750 | |
| 624 | 751 | StyleTreeNode::Code(c, l) |
| 625 | 752 | }, |
| 626 | 753 | |
| 627 | 754 | // Other text blocks. |
| 628 | | "blockquote" => StyleTreeNode::Blockquote(c2t(&node.children.borrow())), |
| 629 | | "div" | "p" => StyleTreeNode::Paragraph(c2t(&node.children.borrow())), |
| 630 | | "pre" => StyleTreeNode::Pre(c2t(&node.children.borrow())), |
| 755 | "blockquote" => StyleTreeNode::Blockquote(c2t(&node.children.borrow(), state)), |
| 756 | "div" | "p" => StyleTreeNode::Paragraph(c2t(&node.children.borrow(), state)), |
| 757 | "pre" => StyleTreeNode::Pre(c2t(&node.children.borrow(), state)), |
| 631 | 758 | |
| 632 | 759 | // No children. |
| 633 | 760 | "hr" => StyleTreeNode::Ruler, |
| @@ -636,8 +763,8 @@ fn h2t(hdl: &Handle) -> StyleTreeChildren { |
| 636 | 763 | "img" => StyleTreeNode::Image(attrs_to_alt(&attrs.borrow())), |
| 637 | 764 | |
| 638 | 765 | // These don't render in any special way. |
| 639 | | "a" | "details" | "html" | "summary" | "sub" | "sup" => { |
| 640 | | *c2t(&node.children.borrow()) |
| 766 | "details" | "html" | "summary" | "sub" | "sup" => { |
| 767 | *c2t(&node.children.borrow(), state) |
| 641 | 768 | }, |
| 642 | 769 | |
| 643 | 770 | _ => return vec![], |
| @@ -654,7 +781,10 @@ fn h2t(hdl: &Handle) -> StyleTreeChildren { |
| 654 | 781 | } |
| 655 | 782 | |
| 656 | 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 | 790 | /// Parse an HTML document from a string. |