1
0
mirror of https://github.com/danog/blackfriday.git synced 2024-11-26 20:14:43 +01:00

Make ListData a nested struct instead of pointer

This commit is contained in:
Vytautas Šaltenis 2016-04-01 11:21:25 +03:00
parent 2a07386455
commit 60026cc3c6
4 changed files with 22 additions and 35 deletions

View File

@ -1109,12 +1109,8 @@ func (p *parser) list(data []byte, flags ListType) int {
i := 0
flags |= ListItemBeginningOfList
block := p.addBlock(List, nil)
block.ListData = &ListData{ // TODO: fill in the real ListData
Flags: flags,
Tight: true,
BulletChar: '*',
Delimiter: 0,
}
block.ListFlags = flags
block.Tight = true
for i < len(data) {
skip := p.listItem(data[i:], &flags)
@ -1184,9 +1180,12 @@ func (p *parser) listItem(data []byte, flags *ListType) int {
itemIndent++
}
var bulletChar byte = '*'
i := p.uliPrefix(data)
if i == 0 {
i = p.oliPrefix(data)
} else {
bulletChar = data[i-2]
}
if i == 0 {
i = p.dliPrefix(data)
@ -1327,12 +1326,10 @@ gatherlines:
rawBytes := raw.Bytes()
block := p.addBlock(Item, nil)
block.ListData = &ListData{ // TODO: fill in the real ListData
Flags: *flags,
Tight: false,
BulletChar: '*',
Delimiter: 0,
}
block.ListFlags = *flags
block.Tight = false
block.BulletChar = bulletChar
block.Delimiter = '.' // Only '.' is possible in Markdown, but ')' will also be possible in CommonMark
// render the contents of the list item
if *flags&ListItemContainsBlock != 0 && *flags&ListTypeTerm == 0 {

16
html.go
View File

@ -1045,15 +1045,15 @@ func itemOpenCR(node *Node) bool {
return false
}
ld := node.Parent.ListData
return !ld.Tight && ld.Flags&ListTypeDefinition == 0
return !ld.Tight && ld.ListFlags&ListTypeDefinition == 0
}
func skipParagraphTags(node *Node) bool {
grandparent := node.Parent.Parent
if grandparent == nil || grandparent.ListData == nil {
if grandparent == nil || grandparent.Type != List {
return false
}
tightOrTerm := grandparent.ListData.Tight || node.Parent.ListData.Flags&ListTypeTerm != 0
tightOrTerm := grandparent.Tight || node.Parent.ListFlags&ListTypeTerm != 0
return grandparent.Type == List && tightOrTerm
}
@ -1261,10 +1261,10 @@ func (r *HTML) RenderNode(w io.Writer, node *Node, entering bool) {
break
case List:
tagName := "ul"
if node.ListData.Flags&ListTypeOrdered != 0 {
if node.ListFlags&ListTypeOrdered != 0 {
tagName = "ol"
}
if node.ListData.Flags&ListTypeDefinition != 0 {
if node.ListFlags&ListTypeDefinition != 0 {
tagName = "dl"
}
if entering {
@ -1273,7 +1273,7 @@ func (r *HTML) RenderNode(w io.Writer, node *Node, entering bool) {
// attrs.push(['start', start.toString()]);
// }
r.cr(w)
if node.Parent.Type == Item && node.Parent.Parent.ListData.Tight {
if node.Parent.Type == Item && node.Parent.Parent.Tight {
r.cr(w)
}
r.out(w, tag(tagName, attrs, false))
@ -1293,10 +1293,10 @@ func (r *HTML) RenderNode(w io.Writer, node *Node, entering bool) {
}
case Item:
tagName := "li"
if node.ListData.Flags&ListTypeDefinition != 0 {
if node.ListFlags&ListTypeDefinition != 0 {
tagName = "dd"
}
if node.ListData.Flags&ListTypeTerm != 0 {
if node.ListFlags&ListTypeTerm != 0 {
tagName = "dt"
}
if entering {

View File

@ -469,12 +469,7 @@ func (p *parser) parseRefsToAST() {
finalizeHtmlBlock(p.addBlock(HtmlBlock, []byte(`<div class="footnotes">`)))
p.addBlock(HorizontalRule, nil)
block := p.addBlock(List, nil)
block.ListData = &ListData{ // TODO: fill in the real ListData
Flags: ListTypeOrdered,
Tight: false,
BulletChar: '*',
Delimiter: 0,
}
block.ListFlags = ListTypeOrdered
flags := ListItemBeginningOfList
// Note: this loop is intentionally explicit, not range-form. This is
// because the body of the loop will append nested footnotes to p.notes and
@ -483,13 +478,8 @@ func (p *parser) parseRefsToAST() {
for i := 0; i < len(p.notes); i++ {
ref := p.notes[i]
block := p.addBlock(Item, nil)
block.ListData = &ListData{ // TODO: fill in the real ListData
Flags: ListTypeOrdered,
Tight: false,
BulletChar: '*',
Delimiter: 0,
RefLink: ref.link,
}
block.ListFlags = ListTypeOrdered
block.RefLink = ref.link
if ref.hasBlock {
flags |= ListItemContainsBlock
p.block(ref.title)

View File

@ -66,7 +66,7 @@ func (t NodeType) String() string {
}
type ListData struct {
Flags ListType
ListFlags ListType
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
@ -93,7 +93,7 @@ type Node struct {
Level uint32 // If Type == Header, this holds the heading level number
Literal []byte
ListData *ListData // If Type == List, this holds list info
ListData // If Type == List, this holds list info
// TODO: move these fenced code block fields to a substruct
IsFenced bool // If Type == CodeBlock, specifies whether it's a fenced code block or an indented one
Info []byte // If Type == CodeBlock, this holds the info string