Commit 77786698c4
Unregistered key
Layout: unified · split
src/message/html.rs +34 −12
| @@ -27,6 +27,7 @@ use crate::util::{join_cell_text, space_text}; | ||
| 27 | 27 | pub struct BulletIterator { |
| 28 | 28 | style: ListStyle, |
| 29 | 29 | pos: usize, |
| 30 | /// The number of characters the number takes up | |
| 30 | 31 | len: usize, |
| 31 | 32 | } |
| 32 | 33 | |
| @@ -34,7 +35,7 @@ impl BulletIterator { | ||
| 34 | 35 | fn width(&self) -> usize { |
| 35 | 36 | match self.style { |
| 36 | 37 | ListStyle::Unordered => 2, |
| 37 | ListStyle::Ordered => self.len.to_string().len() + 2, | |
| 38 | ListStyle::Ordered(_) => self.len + 2, | |
| 38 | 39 | } |
| 39 | 40 | } |
| 40 | 41 | } |
| @@ -43,20 +44,16 @@ impl Iterator for BulletIterator { | ||
| 43 | 44 | type Item = String; |
| 44 | 45 | |
| 45 | 46 | fn next(&mut self) -> Option<Self::Item> { |
| 46 | if self.pos == self.len { | |
| 47 | return None; | |
| 48 | } | |
| 49 | ||
| 50 | self.pos += 1; | |
| 51 | ||
| 52 | 47 | let bullet = match self.style { |
| 53 | 48 | ListStyle::Unordered => "- ".to_string(), |
| 54 | ListStyle::Ordered => { | |
| 55 | let w = self.len.to_string().len(); | |
| 49 | ListStyle::Ordered(_) => { | |
| 50 | let w = self.len; | |
| 56 | 51 | format!("{: >w$}. ", self.pos, w = w) |
| 57 | 52 | }, |
| 58 | 53 | }; |
| 59 | 54 | |
| 55 | self.pos += 1; | |
| 56 | ||
| 60 | 57 | return Some(bullet); |
| 61 | 58 | } |
| 62 | 59 | } |
| @@ -64,13 +61,21 @@ impl Iterator for BulletIterator { | ||
| 64 | 61 | /// Whether this list is ordered or unordered. |
| 65 | 62 | #[derive(Clone, Copy, Debug)] |
| 66 | 63 | pub enum ListStyle { |
| 67 | Ordered, | |
| 64 | /// The `usize` is the start index. | |
| 65 | Ordered(usize), | |
| 68 | 66 | Unordered, |
| 69 | 67 | } |
| 70 | 68 | |
| 71 | 69 | impl ListStyle { |
| 72 | 70 | fn bullets(&self, len: usize) -> BulletIterator { |
| 73 | BulletIterator { style: *self, pos: 0, len } | |
| 71 | let pos = match self { | |
| 72 | Self::Ordered(start) => *start, | |
| 73 | Self::Unordered => 0, | |
| 74 | }; | |
| 75 | ||
| 76 | let len = (pos + len).to_string().len(); | |
| 77 | ||
| 78 | BulletIterator { style: *self, pos, len } | |
| 74 | 79 | } |
| 75 | 80 | } |
| 76 | 81 | |
| @@ -711,6 +716,20 @@ fn lic2t(handles: &[Handle], state: &mut TreeGenState) -> StyleTreeChildren { | ||
| 711 | 716 | handles.iter().filter_map(|h| li2t(h, state)).collect() |
| 712 | 717 | } |
| 713 | 718 | |
| 719 | fn attrs_to_start(attrs: &[Attribute]) -> Option<usize> { | |
| 720 | for attr in attrs { | |
| 721 | if attr.name.local.as_ref() != "start" { | |
| 722 | continue; | |
| 723 | } | |
| 724 | ||
| 725 | if let Ok(start) = usize::from_str(attr.value.as_ref()) { | |
| 726 | return Some(start); | |
| 727 | } | |
| 728 | } | |
| 729 | ||
| 730 | return None; | |
| 731 | } | |
| 732 | ||
| 714 | 733 | fn attrs_to_alt(attrs: &[Attribute]) -> Option<String> { |
| 715 | 734 | for attr in attrs { |
| 716 | 735 | if attr.name.local.as_ref() != "alt" { |
| @@ -871,7 +890,10 @@ fn h2t(hdl: &Handle, state: &mut TreeGenState) -> StyleTreeChildren { | ||
| 871 | 890 | |
| 872 | 891 | // Lists |
| 873 | 892 | "ol" => { |
| 874 | StyleTreeNode::List(lic2t(&node.children.borrow(), state), ListStyle::Ordered) | |
| 893 | StyleTreeNode::List( | |
| 894 | lic2t(&node.children.borrow(), state), | |
| 895 | ListStyle::Ordered(attrs_to_start(&attrs.borrow()).unwrap_or(1)), | |
| 896 | ) | |
| 875 | 897 | }, |
| 876 | 898 | "ul" => { |
| 877 | 899 | StyleTreeNode::List(lic2t(&node.children.borrow(), state), ListStyle::Unordered) |