Commit 77786698c4

77786698c4957e2e12a5cc850e3b0cfc3f2408ec

parent: 8eca60e89a

Unregistered key

vaw <git@nlih.de> · 2026-11-02 12:46 UTC
committer: <noreply@github.com>

Support the start attribute for ordered lists (#852)

Layout: unified · split

src/message/html.rs +34 −12
@@ -27,6 +27,7 @@ use crate::util::{join_cell_text, space_text};
27pub struct BulletIterator { 27pub struct BulletIterator {
28 style: ListStyle, 28 style: ListStyle,
29 pos: usize, 29 pos: usize,
30 /// The number of characters the number takes up
30 len: usize, 31 len: usize,
31} 32}
32 33
@@ -34,7 +35,7 @@ impl BulletIterator {
34 fn width(&self) -> usize { 35 fn width(&self) -> usize {
35 match self.style { 36 match self.style {
36 ListStyle::Unordered => 2, 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 type Item = String; 44 type Item = String;
44 45
45 fn next(&mut self) -> Option<Self::Item> { 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 let bullet = match self.style { 47 let bullet = match self.style {
53 ListStyle::Unordered => "- ".to_string(), 48 ListStyle::Unordered => "- ".to_string(),
54 ListStyle::Ordered => { 49 ListStyle::Ordered(_) => {
55 let w = self.len.to_string().len(); 50 let w = self.len;
56 format!("{: >w$}. ", self.pos, w = w) 51 format!("{: >w$}. ", self.pos, w = w)
57 }, 52 },
58 }; 53 };
59 54
55 self.pos += 1;
56
60 return Some(bullet); 57 return Some(bullet);
61 } 58 }
62} 59}
@@ -64,13 +61,21 @@ impl Iterator for BulletIterator {
64/// Whether this list is ordered or unordered. 61/// Whether this list is ordered or unordered.
65#[derive(Clone, Copy, Debug)] 62#[derive(Clone, Copy, Debug)]
66pub enum ListStyle { 63pub enum ListStyle {
67 Ordered, 64 /// The `usize` is the start index.
65 Ordered(usize),
68 Unordered, 66 Unordered,
69} 67}
70 68
71impl ListStyle { 69impl ListStyle {
72 fn bullets(&self, len: usize) -> BulletIterator { 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 handles.iter().filter_map(|h| li2t(h, state)).collect() 716 handles.iter().filter_map(|h| li2t(h, state)).collect()
712} 717}
713 718
719fn 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
714fn attrs_to_alt(attrs: &[Attribute]) -> Option<String> { 733fn attrs_to_alt(attrs: &[Attribute]) -> Option<String> {
715 for attr in attrs { 734 for attr in attrs {
716 if attr.name.local.as_ref() != "alt" { 735 if attr.name.local.as_ref() != "alt" {
@@ -871,7 +890,10 @@ fn h2t(hdl: &Handle, state: &mut TreeGenState) -> StyleTreeChildren {
871 890
872 // Lists 891 // Lists
873 "ol" => { 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 "ul" => { 898 "ul" => {
877 StyleTreeNode::List(lic2t(&node.children.borrow(), state), ListStyle::Unordered) 899 StyleTreeNode::List(lic2t(&node.children.borrow(), state), ListStyle::Unordered)