Commit df3148b9f5

df3148b9f57029a6b6197454f00ab0046ac92dea

parent: 95af00ba93

Unregistered key

Ulyssa <git@ulyssa.dev> · 2023-10-08 01:24 UTC

Links should be "openable" (#43)

Layout: unified · split

src/base.rs +7
@@ -274,6 +274,9 @@ pub enum IambAction {
274 /// Perform an action on the currently selected message. 274 /// Perform an action on the currently selected message.
275 Message(MessageAction), 275 Message(MessageAction),
276 276
277 /// Open a URL.
278 OpenLink(String),
279
277 /// Perform an action on the currently focused room. 280 /// Perform an action on the currently focused room.
278 Room(RoomAction), 281 Room(RoomAction),
279 282
@@ -327,6 +330,7 @@ impl ApplicationAction for IambAction {
327 IambAction::Homeserver(..) => SequenceStatus::Break, 330 IambAction::Homeserver(..) => SequenceStatus::Break,
328 IambAction::Message(..) => SequenceStatus::Break, 331 IambAction::Message(..) => SequenceStatus::Break,
329 IambAction::Room(..) => SequenceStatus::Break, 332 IambAction::Room(..) => SequenceStatus::Break,
333 IambAction::OpenLink(..) => SequenceStatus::Break,
330 IambAction::Send(..) => SequenceStatus::Break, 334 IambAction::Send(..) => SequenceStatus::Break,
331 IambAction::ToggleScrollbackFocus => SequenceStatus::Break, 335 IambAction::ToggleScrollbackFocus => SequenceStatus::Break,
332 IambAction::Verify(..) => SequenceStatus::Break, 336 IambAction::Verify(..) => SequenceStatus::Break,
@@ -338,6 +342,7 @@ impl ApplicationAction for IambAction {
338 match self { 342 match self {
339 IambAction::Homeserver(..) => SequenceStatus::Atom, 343 IambAction::Homeserver(..) => SequenceStatus::Atom,
340 IambAction::Message(..) => SequenceStatus::Atom, 344 IambAction::Message(..) => SequenceStatus::Atom,
345 IambAction::OpenLink(..) => SequenceStatus::Atom,
341 IambAction::Room(..) => SequenceStatus::Atom, 346 IambAction::Room(..) => SequenceStatus::Atom,
342 IambAction::Send(..) => SequenceStatus::Atom, 347 IambAction::Send(..) => SequenceStatus::Atom,
343 IambAction::ToggleScrollbackFocus => SequenceStatus::Atom, 348 IambAction::ToggleScrollbackFocus => SequenceStatus::Atom,
@@ -351,6 +356,7 @@ impl ApplicationAction for IambAction {
351 IambAction::Homeserver(..) => SequenceStatus::Ignore, 356 IambAction::Homeserver(..) => SequenceStatus::Ignore,
352 IambAction::Message(..) => SequenceStatus::Ignore, 357 IambAction::Message(..) => SequenceStatus::Ignore,
353 IambAction::Room(..) => SequenceStatus::Ignore, 358 IambAction::Room(..) => SequenceStatus::Ignore,
359 IambAction::OpenLink(..) => SequenceStatus::Ignore,
354 IambAction::Send(..) => SequenceStatus::Ignore, 360 IambAction::Send(..) => SequenceStatus::Ignore,
355 IambAction::ToggleScrollbackFocus => SequenceStatus::Ignore, 361 IambAction::ToggleScrollbackFocus => SequenceStatus::Ignore,
356 IambAction::Verify(..) => SequenceStatus::Ignore, 362 IambAction::Verify(..) => SequenceStatus::Ignore,
@@ -364,6 +370,7 @@ impl ApplicationAction for IambAction {
364 IambAction::Message(..) => false, 370 IambAction::Message(..) => false,
365 IambAction::Room(..) => false, 371 IambAction::Room(..) => false,
366 IambAction::Send(..) => false, 372 IambAction::Send(..) => false,
373 IambAction::OpenLink(..) => false,
367 IambAction::ToggleScrollbackFocus => false, 374 IambAction::ToggleScrollbackFocus => false,
368 IambAction::Verify(..) => false, 375 IambAction::Verify(..) => false,
369 IambAction::VerifyRequest(..) => false, 376 IambAction::VerifyRequest(..) => false,
src/main.rs +8
@@ -528,6 +528,14 @@ impl Application {
528 self.screen.current_window_mut()?.send_command(act, ctx, store).await? 528 self.screen.current_window_mut()?.send_command(act, ctx, store).await?
529 }, 529 },
530 530
531 IambAction::OpenLink(url) => {
532 tokio::task::spawn_blocking(move || {
533 return open::that(url);
534 });
535
536 None
537 },
538
531 IambAction::Verify(act, user_dev) => { 539 IambAction::Verify(act, user_dev) => {
532 if let Some(sas) = store.application.verifications.get(&user_dev) { 540 if let Some(sas) = store.application.verifications.get(&user_dev) {
533 self.worker.verify(act, sas.clone())? 541 self.worker.verify(act, sas.clone())?
src/message/html.rs +179 −49
@@ -15,6 +15,7 @@ use std::ops::Deref;
15use css_color_parser::Color as CssColor; 15use css_color_parser::Color as CssColor;
16use markup5ever_rcdom::{Handle, NodeData, RcDom}; 16use markup5ever_rcdom::{Handle, NodeData, RcDom};
17use unicode_segmentation::UnicodeSegmentation; 17use unicode_segmentation::UnicodeSegmentation;
18use url::Url;
18 19
19use html5ever::{ 20use html5ever::{
20 driver::{parse_fragment, ParseOpts}, 21 driver::{parse_fragment, ParseOpts},
@@ -102,6 +103,12 @@ pub struct TableRow {
102} 103}
103 104
104impl TableRow { 105impl 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
115impl TableSection { 122impl 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.
239pub enum StyleTreeNode { 258pub 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
395impl StyleTree { 456impl 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
407fn c2c(handles: &[Handle]) -> Vec<StyleTreeNode> { 478pub struct TreeGenState {
408 handles.iter().flat_map(h2t).collect() 479 link_num: u8,
409} 480}
410 481
411fn c2t(handles: &[Handle]) -> Box<StyleTreeNode> { 482impl 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
502fn c2c(handles: &[Handle], state: &mut TreeGenState) -> Vec<StyleTreeNode> {
503 handles.iter().flat_map(|h| h2t(h, state)).collect()
504}
505
506fn 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
417fn get_node(hdl: &Handle, want: &str) -> Option<StyleTreeNode> { 512fn 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
432fn li2t(hdl: &Handle) -> Option<StyleTreeNode> { 527fn li2t(hdl: &Handle, state: &mut TreeGenState) -> Option<StyleTreeNode> {
433 get_node(hdl, "li") 528 get_node(hdl, "li", state)
434} 529}
435 530
436fn table_cell(hdl: &Handle) -> Option<(CellType, StyleTreeNode)> { 531fn 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
444fn table_row(hdl: &Handle) -> Option<TableRow> { 539fn 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
459fn table_section(hdl: &Handle) -> Option<TableSection> { 554fn 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
476fn table_cells(handles: &[Handle]) -> Vec<(CellType, StyleTreeNode)> { 571fn 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
480fn table_rows(handles: &[Handle]) -> Vec<TableRow> { 575fn 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
484fn table_sections(handles: &[Handle]) -> Vec<TableSection> { 579fn 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
488fn lic2t(handles: &[Handle]) -> StyleTreeChildren { 583fn 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
492fn attrs_to_alt(attrs: &[Attribute]) -> Option<String> { 587fn 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
599fn 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
504fn attrs_to_language(attrs: &[Attribute]) -> Option<String> { 611fn 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
544fn h2t(hdl: &Handle) -> StyleTreeChildren { 651fn 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
656fn dom_to_style_tree(dom: RcDom) -> StyleTree { 783fn 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.
src/windows/room/chat.rs +30 −2
@@ -9,6 +9,7 @@ use edit::edit as external_edit;
9use modalkit::editing::store::RegisterError; 9use modalkit::editing::store::RegisterError;
10use std::process::Command; 10use std::process::Command;
11use tokio; 11use tokio;
12use url::Url;
12 13
13use matrix_sdk::{ 14use matrix_sdk::{
14 attachment::AttachmentConfig, 15 attachment::AttachmentConfig,
@@ -31,7 +32,7 @@ use matrix_sdk::{
31}; 32};
32 33
33use modalkit::{ 34use modalkit::{
34 input::dialog::PromptYesNo, 35 input::dialog::{MultiChoice, MultiChoiceItem, PromptYesNo},
35 tui::{ 36 tui::{
36 buffer::Buffer, 37 buffer::Buffer,
37 layout::Rect, 38 layout::Rect,
@@ -203,7 +204,34 @@ impl ChatState {
203 MessageType::Image(c) => (c.source.clone(), c.body.as_str()), 204 MessageType::Image(c) => (c.source.clone(), c.body.as_str()),
204 MessageType::Video(c) => (c.source.clone(), c.body.as_str()), 205 MessageType::Video(c) => (c.source.clone(), c.body.as_str()),
205 _ => { 206 _ => {
206 return Err(IambError::NoAttachment.into()); 207 if !flags.contains(DownloadFlags::OPEN) {
208 return Err(IambError::NoAttachment.into());
209 }
210
211 let links = if let Some(html) = &msg.html {
212 html.get_links()
213 } else if let Ok(url) = Url::parse(&msg.event.body()) {
214 vec![('0', url)]
215 } else {
216 vec![]
217 };
218
219 if links.is_empty() {
220 return Err(IambError::NoAttachment.into());
221 }
222
223 let choices = links
224 .into_iter()
225 .map(|l| {
226 let url = l.1.to_string();
227 let act = IambAction::OpenLink(url.clone()).into();
228 MultiChoiceItem::new(l.0, url, vec![act])
229 })
230 .collect();
231 let dialog = MultiChoice::new(choices);
232 let err = UIError::NeedConfirm(Box::new(dialog));
233
234 return Err(err);
207 }, 235 },
208 }; 236 };
209 237