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:
parent
b3edd6c182
commit
21f1af7d48
6 changed files with 38 additions and 6 deletions
|
@ -412,11 +412,14 @@ func (w *HTMLWriter) WriteList(l List) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *HTMLWriter) WriteListItem(li ListItem) {
|
func (w *HTMLWriter) WriteListItem(li ListItem) {
|
||||||
if li.Status != "" {
|
attributes := ""
|
||||||
w.WriteString(fmt.Sprintf("<li class=\"%s\">\n", listItemStatuses[li.Status]))
|
if li.Value != "" {
|
||||||
} else {
|
attributes += fmt.Sprintf(` value="%s"`, li.Value)
|
||||||
w.WriteString("<li>\n")
|
|
||||||
}
|
}
|
||||||
|
if li.Status != "" {
|
||||||
|
attributes += fmt.Sprintf(` class="%s"`, listItemStatuses[li.Status])
|
||||||
|
}
|
||||||
|
w.WriteString(fmt.Sprintf("<li%s>\n", attributes))
|
||||||
WriteNodes(w, li.Children...)
|
WriteNodes(w, li.Children...)
|
||||||
w.WriteString("</li>\n")
|
w.WriteString("</li>\n")
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,6 +15,7 @@ type List struct {
|
||||||
type ListItem struct {
|
type ListItem struct {
|
||||||
Bullet string
|
Bullet string
|
||||||
Status string
|
Status string
|
||||||
|
Value string
|
||||||
Children []Node
|
Children []Node
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,6 +29,7 @@ type DescriptiveListItem struct {
|
||||||
var unorderedListRegexp = regexp.MustCompile(`^(\s*)([+*-])(\s+(.*)|$)`)
|
var unorderedListRegexp = regexp.MustCompile(`^(\s*)([+*-])(\s+(.*)|$)`)
|
||||||
var orderedListRegexp = regexp.MustCompile(`^(\s*)(([0-9]+|[a-zA-Z])[.)])(\s+(.*)|$)`)
|
var orderedListRegexp = regexp.MustCompile(`^(\s*)(([0-9]+|[a-zA-Z])[.)])(\s+(.*)|$)`)
|
||||||
var descriptiveListItemRegexp = regexp.MustCompile(`\s::(\s|$)`)
|
var descriptiveListItemRegexp = regexp.MustCompile(`\s::(\s|$)`)
|
||||||
|
var listItemValueRegexp = regexp.MustCompile(`\[@(\d+)\]\s`)
|
||||||
var listItemStatusRegexp = regexp.MustCompile(`\[( |X|-)\]\s`)
|
var listItemStatusRegexp = regexp.MustCompile(`\[( |X|-)\]\s`)
|
||||||
|
|
||||||
func lexList(line string) (token, bool) {
|
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) {
|
func (d *Document) parseListItem(l List, i int, parentStop stopFn) (int, Node) {
|
||||||
start, nodes, bullet := i, []Node{}, d.tokens[i].matches[2]
|
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
|
originalBaseLvl := d.baseLvl
|
||||||
d.baseLvl = minIndent + 1
|
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 {
|
if m := listItemStatusRegexp.FindStringSubmatch(content); m != nil {
|
||||||
status, content = m[1], content[len("[ ] "):]
|
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" {
|
if l.Kind == "descriptive" {
|
||||||
return i - start, DescriptiveListItem{bullet, status, d.parseInline(dterm), nodes}
|
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) }
|
func (n List) String() string { return orgWriter.WriteNodesAsString(n) }
|
||||||
|
|
|
@ -219,6 +219,9 @@ func (w *OrgWriter) WriteListItem(li ListItem) {
|
||||||
content := strings.TrimPrefix(w.String(), w.indent)
|
content := strings.TrimPrefix(w.String(), w.indent)
|
||||||
w.Builder, w.indent = originalBuilder, originalIndent
|
w.Builder, w.indent = originalBuilder, originalIndent
|
||||||
w.WriteString(w.indent + li.Bullet)
|
w.WriteString(w.indent + li.Bullet)
|
||||||
|
if li.Value != "" {
|
||||||
|
w.WriteString(fmt.Sprintf(" [@%s]", li.Value))
|
||||||
|
}
|
||||||
if li.Status != "" {
|
if li.Status != "" {
|
||||||
w.WriteString(fmt.Sprintf(" [%s]", li.Status))
|
w.WriteString(fmt.Sprintf(" [%s]", li.Status))
|
||||||
}
|
}
|
||||||
|
|
11
org/testdata/lists.html
vendored
11
org/testdata/lists.html
vendored
|
@ -195,3 +195,14 @@ ordered descriptive
|
||||||
<p>unordered 2</p>
|
<p>unordered 2</p>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</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>
|
||||||
|
|
5
org/testdata/lists.org
vendored
5
org/testdata/lists.org
vendored
|
@ -68,3 +68,8 @@ some list termination tests
|
||||||
2. ordered descriptive :: 2
|
2. ordered descriptive :: 2
|
||||||
- unordered 1
|
- unordered 1
|
||||||
- unordered 2
|
- 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 (`[ ]`)
|
||||||
|
|
5
org/testdata/lists.pretty_org
vendored
5
org/testdata/lists.pretty_org
vendored
|
@ -68,3 +68,8 @@ some list termination tests
|
||||||
2. ordered descriptive :: 2
|
2. ordered descriptive :: 2
|
||||||
- unordered 1
|
- unordered 1
|
||||||
- unordered 2
|
- 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 (`[ ]`)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue