Add support for custom numbering of ordered list using [@\d+]

org mode supports [1] setting the value attribute [2] of ordered list items to
change the numbering for the current and following items. Let's do the same. As
the attribute has no meaning for other types of lists [2] we'll just not
support it for those cases [3].

[1] https://orgmode.org/manual/Plain-Lists.html#Plain-Lists
[2] https://developer.mozilla.org/en-US/docs/Web/HTML/Element/li#attributes
[3]
Org mode seems to instead set the id attribute for e.g. unordered lists
starting with `[@\d+]\s` - but I don't really see the value in that and will
skip that for now.
This commit is contained in:
Niklas Fasching 2021-04-17 15:39:14 +02:00
parent b3edd6c182
commit 21f1af7d48
6 changed files with 38 additions and 6 deletions

View file

@ -15,6 +15,7 @@ type List struct {
type ListItem struct {
Bullet string
Status string
Value string
Children []Node
}
@ -28,6 +29,7 @@ type DescriptiveListItem struct {
var unorderedListRegexp = regexp.MustCompile(`^(\s*)([+*-])(\s+(.*)|$)`)
var orderedListRegexp = regexp.MustCompile(`^(\s*)(([0-9]+|[a-zA-Z])[.)])(\s+(.*)|$)`)
var descriptiveListItemRegexp = regexp.MustCompile(`\s::(\s|$)`)
var listItemValueRegexp = regexp.MustCompile(`\[@(\d+)\]\s`)
var listItemStatusRegexp = regexp.MustCompile(`\[( |X|-)\]\s`)
func lexList(line string) (token, bool) {
@ -80,9 +82,12 @@ func (d *Document) parseList(i int, parentStop stopFn) (int, Node) {
func (d *Document) parseListItem(l List, i int, parentStop stopFn) (int, Node) {
start, nodes, bullet := i, []Node{}, d.tokens[i].matches[2]
minIndent, dterm, content, status := d.tokens[i].lvl+len(bullet), "", d.tokens[i].content, ""
minIndent, dterm, content, status, value := d.tokens[i].lvl+len(bullet), "", d.tokens[i].content, "", ""
originalBaseLvl := d.baseLvl
d.baseLvl = minIndent + 1
if m := listItemValueRegexp.FindStringSubmatch(content); m != nil && l.Kind == "ordered" {
value, content = m[1], content[len("[@] ")+len(m[1]):]
}
if m := listItemStatusRegexp.FindStringSubmatch(content); m != nil {
status, content = m[1], content[len("[ ] "):]
}
@ -110,7 +115,7 @@ func (d *Document) parseListItem(l List, i int, parentStop stopFn) (int, Node) {
if l.Kind == "descriptive" {
return i - start, DescriptiveListItem{bullet, status, d.parseInline(dterm), nodes}
}
return i - start, ListItem{bullet, status, nodes}
return i - start, ListItem{bullet, status, value, nodes}
}
func (n List) String() string { return orgWriter.WriteNodesAsString(n) }