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

@ -412,11 +412,14 @@ func (w *HTMLWriter) WriteList(l List) {
}
func (w *HTMLWriter) WriteListItem(li ListItem) {
if li.Status != "" {
w.WriteString(fmt.Sprintf("<li class=\"%s\">\n", listItemStatuses[li.Status]))
} else {
w.WriteString("<li>\n")
attributes := ""
if li.Value != "" {
attributes += fmt.Sprintf(` value="%s"`, li.Value)
}
if li.Status != "" {
attributes += fmt.Sprintf(` class="%s"`, listItemStatuses[li.Status])
}
w.WriteString(fmt.Sprintf("<li%s>\n", attributes))
WriteNodes(w, li.Children...)
w.WriteString("</li>\n")
}

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) }

View file

@ -219,6 +219,9 @@ func (w *OrgWriter) WriteListItem(li ListItem) {
content := strings.TrimPrefix(w.String(), w.indent)
w.Builder, w.indent = originalBuilder, originalIndent
w.WriteString(w.indent + li.Bullet)
if li.Value != "" {
w.WriteString(fmt.Sprintf(" [@%s]", li.Value))
}
if li.Status != "" {
w.WriteString(fmt.Sprintf(" [%s]", li.Status))
}

View file

@ -195,3 +195,14 @@ ordered descriptive
<p>unordered 2</p>
</li>
</ul>
<ol>
<li value="2">
<p>use `[@n]` to change the value of list items</p>
</li>
<li class="unchecked">
<p>foobar</p>
</li>
<li value="10" class="checked">
<p>that even works in combination with list statuses (`[ ]`)</p>
</li>
</ol>

View file

@ -68,3 +68,8 @@ some list termination tests
2. ordered descriptive :: 2
- unordered 1
- unordered 2
1. [@2] use `[@n]` to change the value of list items
2. [ ] foobar
3. [@10] [X] that even works in combination with list statuses (`[ ]`)

View file

@ -68,3 +68,8 @@ some list termination tests
2. ordered descriptive :: 2
- unordered 1
- unordered 2
1. [@2] use `[@n]` to change the value of list items
2. [ ] foobar
3. [@10] [X] that even works in combination with list statuses (`[ ]`)