Ordered list: detect starting number

This keeps track of the starting number of an ordered list and uses it
when not zero. Fails the tests so it will probably have to wait until
we have the Mmark extension merged.

Signed-off-by: Miek Gieben <miek@miek.nl>
This commit is contained in:
Miek Gieben 2018-07-29 17:03:36 +01:00
parent aecd94f204
commit 0355745bf4
3 changed files with 20 additions and 7 deletions

View File

@ -137,6 +137,7 @@ type List struct {
Tight bool // Skip <p>s around list item data if true
BulletChar byte // '*', '+' or '-' in bullet lists
Delimiter byte // '.' or ')' after the number in ordered lists
Start int // for ordered lists this indicates the starting number if > 0
RefLink []byte // If not nil, turns this list item into a footnote item and triggers different rendering
IsFootnotesList bool // This is a list of footnotes
}

View File

@ -653,6 +653,9 @@ func (r *Renderer) listEnter(w io.Writer, nodeData *ast.List) {
openTag := "<ul"
if nodeData.ListFlags&ast.ListTypeOrdered != 0 {
if nodeData.Start > 0 {
attrs = append(attrs, fmt.Sprintf(`start="%d"`, nodeData.Start))
}
openTag = "<ol"
}
if nodeData.ListFlags&ast.ListTypeDefinition != 0 {

View File

@ -4,6 +4,7 @@ import (
"bytes"
"html"
"regexp"
"strconv"
"unicode"
"github.com/gomarkdown/markdown/ast"
@ -229,7 +230,7 @@ func (p *Parser) block(data []byte) {
//
// also works with + or -
if p.uliPrefix(data) > 0 {
data = data[p.list(data, 0):]
data = data[p.list(data, 0, 0):]
continue
}
@ -237,8 +238,15 @@ func (p *Parser) block(data []byte) {
//
// 1. Item 1
// 2. Item 2
if p.oliPrefix(data) > 0 {
data = data[p.list(data, ast.ListTypeOrdered):]
if i := p.oliPrefix(data); i > 0 {
start := 0
if i > 2 {
start, _ = strconv.Atoi(string(data[:i-2])) // this cannot fail because we just est. the thing *is* a number.
if start == 1 {
start = 0
}
}
data = data[p.list(data, ast.ListTypeOrdered, start):]
continue
}
@ -252,7 +260,7 @@ func (p *Parser) block(data []byte) {
// : Definition c
if p.extensions&DefinitionLists != 0 {
if p.dliPrefix(data) > 0 {
data = data[p.list(data, ast.ListTypeDefinition):]
data = data[p.list(data, ast.ListTypeDefinition, 0):]
continue
}
}
@ -1192,12 +1200,13 @@ func (p *Parser) dliPrefix(data []byte) int {
}
// parse ordered or unordered list block
func (p *Parser) list(data []byte, flags ast.ListType) int {
func (p *Parser) list(data []byte, flags ast.ListType, start int) int {
i := 0
flags |= ast.ListItemBeginningOfList
list := &ast.List{
ListFlags: flags,
Tight: true,
Start: start,
}
block := p.addBlock(list)
@ -1533,7 +1542,7 @@ func (p *Parser) paragraph(data []byte) int {
// did this blank line followed by a definition list item?
if p.extensions&DefinitionLists != 0 {
if i < len(data)-1 && data[i+1] == ':' {
listLen := p.list(data[prev:], ast.ListTypeDefinition)
listLen := p.list(data[prev:], ast.ListTypeDefinition, 0)
return prev + listLen
}
}
@ -1600,7 +1609,7 @@ func (p *Parser) paragraph(data []byte) int {
// if there's a definition list item, prev line is a definition term
if p.extensions&DefinitionLists != 0 {
if p.dliPrefix(current) != 0 {
ret := p.list(data[prev:], ast.ListTypeDefinition)
ret := p.list(data[prev:], ast.ListTypeDefinition, 0)
return ret + prev
}
}