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};
2727pub struct BulletIterator {
2828 style: ListStyle,
2929 pos: usize,
30 /// The number of characters the number takes up
3031 len: usize,
3132}
3233
@@ -34,7 +35,7 @@ impl BulletIterator {
3435 fn width(&self) -> usize {
3536 match self.style {
3637 ListStyle::Unordered => 2,
37 ListStyle::Ordered => self.len.to_string().len() + 2,
38 ListStyle::Ordered(_) => self.len + 2,
3839 }
3940 }
4041}
@@ -43,20 +44,16 @@ impl Iterator for BulletIterator {
4344 type Item = String;
4445
4546 fn next(&mut self) -> Option<Self::Item> {
46 if self.pos == self.len {
47 return None;
48 }
49
50 self.pos += 1;
51
5247 let bullet = match self.style {
5348 ListStyle::Unordered => "- ".to_string(),
54 ListStyle::Ordered => {
55 let w = self.len.to_string().len();
49 ListStyle::Ordered(_) => {
50 let w = self.len;
5651 format!("{: >w$}. ", self.pos, w = w)
5752 },
5853 };
5954
55 self.pos += 1;
56
6057 return Some(bullet);
6158 }
6259}
@@ -64,13 +61,21 @@ impl Iterator for BulletIterator {
6461/// Whether this list is ordered or unordered.
6562#[derive(Clone, Copy, Debug)]
6663pub enum ListStyle {
67 Ordered,
64 /// The `usize` is the start index.
65 Ordered(usize),
6866 Unordered,
6967}
7068
7169impl ListStyle {
7270 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 }
7479 }
7580}
7681
@@ -711,6 +716,20 @@ fn lic2t(handles: &[Handle], state: &mut TreeGenState) -> StyleTreeChildren {
711716 handles.iter().filter_map(|h| li2t(h, state)).collect()
712717}
713718
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
714733fn attrs_to_alt(attrs: &[Attribute]) -> Option<String> {
715734 for attr in attrs {
716735 if attr.name.local.as_ref() != "alt" {
@@ -871,7 +890,10 @@ fn h2t(hdl: &Handle, state: &mut TreeGenState) -> StyleTreeChildren {
871890
872891 // Lists
873892 "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 )
875897 },
876898 "ul" => {
877899 StyleTreeNode::List(lic2t(&node.children.borrow(), state), ListStyle::Unordered)