Skip to content

Commit

Permalink
Merge pull request #365 from Ambrevar/v2
Browse files Browse the repository at this point in the history
Merge Parser into Processor
  • Loading branch information
rtfb authored Jun 9, 2017
2 parents e7910a8 + 2501229 commit 70c446a
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 147 deletions.
90 changes: 45 additions & 45 deletions block.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var (
// Parse block-level data.
// Note: this function and many that it calls assume that
// the input buffer ends with a newline.
func (p *Parser) block(data []byte) {
func (p *Markdown) block(data []byte) {
// this is called recursively: enforce a maximum depth
if p.nesting >= p.maxNesting {
return
Expand Down Expand Up @@ -71,7 +71,7 @@ func (p *Parser) block(data []byte) {
// % stuff
// % more stuff
// % even more stuff
if p.flags&Titleblock != 0 {
if p.extensions&Titleblock != 0 {
if data[0] == '%' {
if i := p.titleBlock(data, true); i > 0 {
data = data[i:]
Expand Down Expand Up @@ -109,7 +109,7 @@ func (p *Parser) block(data []byte) {
// return n * fact(n-1)
// }
// ```
if p.flags&FencedCode != 0 {
if p.extensions&FencedCode != 0 {
if i := p.fencedCodeBlock(data, true); i > 0 {
data = data[i:]
continue
Expand Down Expand Up @@ -147,7 +147,7 @@ func (p *Parser) block(data []byte) {
// ------|-----|---------
// Bob | 31 | 555-1234
// Alice | 27 | 555-4321
if p.flags&Tables != 0 {
if p.extensions&Tables != 0 {
if i := p.table(data); i > 0 {
data = data[i:]
continue
Expand Down Expand Up @@ -182,7 +182,7 @@ func (p *Parser) block(data []byte) {
//
// Term 2
// : Definition c
if p.flags&DefinitionLists != 0 {
if p.extensions&DefinitionLists != 0 {
if p.dliPrefix(data) > 0 {
data = data[p.list(data, ListTypeDefinition):]
continue
Expand All @@ -197,19 +197,19 @@ func (p *Parser) block(data []byte) {
p.nesting--
}

func (p *Parser) addBlock(typ NodeType, content []byte) *Node {
func (p *Markdown) addBlock(typ NodeType, content []byte) *Node {
p.closeUnmatchedBlocks()
container := p.addChild(typ, 0)
container.content = content
return container
}

func (p *Parser) isPrefixHeading(data []byte) bool {
func (p *Markdown) isPrefixHeading(data []byte) bool {
if data[0] != '#' {
return false
}

if p.flags&SpaceHeadings != 0 {
if p.extensions&SpaceHeadings != 0 {
level := 0
for level < 6 && level < len(data) && data[level] == '#' {
level++
Expand All @@ -221,7 +221,7 @@ func (p *Parser) isPrefixHeading(data []byte) bool {
return true
}

func (p *Parser) prefixHeading(data []byte) int {
func (p *Markdown) prefixHeading(data []byte) int {
level := 0
for level < 6 && level < len(data) && data[level] == '#' {
level++
Expand All @@ -230,7 +230,7 @@ func (p *Parser) prefixHeading(data []byte) int {
end := skipUntilChar(data, i, '\n')
skip := end
id := ""
if p.flags&HeadingIDs != 0 {
if p.extensions&HeadingIDs != 0 {
j, k := 0, 0
// find start/end of heading id
for j = i; j < end-1 && (data[j] != '{' || data[j+1] != '#'); j++ {
Expand All @@ -257,7 +257,7 @@ func (p *Parser) prefixHeading(data []byte) int {
end--
}
if end > i {
if id == "" && p.flags&AutoHeadingIDs != 0 {
if id == "" && p.extensions&AutoHeadingIDs != 0 {
id = sanitized_anchor_name.Create(string(data[i:end]))
}
block := p.addBlock(Heading, data[i:end])
Expand All @@ -267,7 +267,7 @@ func (p *Parser) prefixHeading(data []byte) int {
return skip
}

func (p *Parser) isUnderlinedHeading(data []byte) int {
func (p *Markdown) isUnderlinedHeading(data []byte) int {
// test of level 1 heading
if data[0] == '=' {
i := skipChar(data, 1, '=')
Expand All @@ -291,7 +291,7 @@ func (p *Parser) isUnderlinedHeading(data []byte) int {
return 0
}

func (p *Parser) titleBlock(data []byte, doRender bool) int {
func (p *Markdown) titleBlock(data []byte, doRender bool) int {
if data[0] != '%' {
return 0
}
Expand All @@ -315,7 +315,7 @@ func (p *Parser) titleBlock(data []byte, doRender bool) int {
return consumed
}

func (p *Parser) html(data []byte, doRender bool) int {
func (p *Markdown) html(data []byte, doRender bool) int {
var i, j int

// identify the opening tag
Expand Down Expand Up @@ -419,7 +419,7 @@ func finalizeHTMLBlock(block *Node) {
}

// HTML comment, lax form
func (p *Parser) htmlComment(data []byte, doRender bool) int {
func (p *Markdown) htmlComment(data []byte, doRender bool) int {
i := p.inlineHTMLComment(data)
// needs to end with a blank line
if j := p.isEmpty(data[i:]); j > 0 {
Expand All @@ -439,7 +439,7 @@ func (p *Parser) htmlComment(data []byte, doRender bool) int {
}

// HR, which is the only self-closing block tag considered
func (p *Parser) htmlHr(data []byte, doRender bool) int {
func (p *Markdown) htmlHr(data []byte, doRender bool) int {
if len(data) < 4 {
return 0
}
Expand Down Expand Up @@ -472,7 +472,7 @@ func (p *Parser) htmlHr(data []byte, doRender bool) int {
return 0
}

func (p *Parser) htmlFindTag(data []byte) (string, bool) {
func (p *Markdown) htmlFindTag(data []byte) (string, bool) {
i := 0
for i < len(data) && isalnum(data[i]) {
i++
Expand All @@ -484,7 +484,7 @@ func (p *Parser) htmlFindTag(data []byte) (string, bool) {
return "", false
}

func (p *Parser) htmlFindEnd(tag string, data []byte) int {
func (p *Markdown) htmlFindEnd(tag string, data []byte) int {
// assume data[0] == '<' && data[1] == '/' already tested
if tag == "hr" {
return 2
Expand All @@ -508,7 +508,7 @@ func (p *Parser) htmlFindEnd(tag string, data []byte) int {
return i
}

if p.flags&LaxHTMLBlocks != 0 {
if p.extensions&LaxHTMLBlocks != 0 {
return i
}
if skip = p.isEmpty(data[i:]); skip == 0 {
Expand All @@ -519,7 +519,7 @@ func (p *Parser) htmlFindEnd(tag string, data []byte) int {
return i + skip
}

func (*Parser) isEmpty(data []byte) int {
func (*Markdown) isEmpty(data []byte) int {
// it is okay to call isEmpty on an empty buffer
if len(data) == 0 {
return 0
Expand All @@ -537,7 +537,7 @@ func (*Parser) isEmpty(data []byte) int {
return i
}

func (*Parser) isHRule(data []byte) bool {
func (*Markdown) isHRule(data []byte) bool {
i := 0

// skip up to three spaces
Expand Down Expand Up @@ -667,7 +667,7 @@ func isFenceLine(data []byte, syntax *string, oldmarker string) (end int, marker
// fencedCodeBlock returns the end index if data contains a fenced code block at the beginning,
// or 0 otherwise. It writes to out if doRender is true, otherwise it has no side effects.
// If doRender is true, a final newline is mandatory to recognize the fenced code block.
func (p *Parser) fencedCodeBlock(data []byte, doRender bool) int {
func (p *Markdown) fencedCodeBlock(data []byte, doRender bool) int {
var syntax string
beg, marker := isFenceLine(data, &syntax, "")
if beg == 0 || beg >= len(data) {
Expand Down Expand Up @@ -739,7 +739,7 @@ func finalizeCodeBlock(block *Node) {
block.content = nil
}

func (p *Parser) table(data []byte) int {
func (p *Markdown) table(data []byte) int {
table := p.addBlock(Table, nil)
i, columns := p.tableHeader(data)
if i == 0 {
Expand Down Expand Up @@ -782,7 +782,7 @@ func isBackslashEscaped(data []byte, i int) bool {
return backslashes&1 == 1
}

func (p *Parser) tableHeader(data []byte) (size int, columns []CellAlignFlags) {
func (p *Markdown) tableHeader(data []byte) (size int, columns []CellAlignFlags) {
i := 0
colCount := 1
for i = 0; i < len(data) && data[i] != '\n'; i++ {
Expand Down Expand Up @@ -895,7 +895,7 @@ func (p *Parser) tableHeader(data []byte) (size int, columns []CellAlignFlags) {
return
}

func (p *Parser) tableRow(data []byte, columns []CellAlignFlags, header bool) {
func (p *Markdown) tableRow(data []byte, columns []CellAlignFlags, header bool) {
p.addBlock(TableRow, nil)
i, col := 0, 0

Expand Down Expand Up @@ -939,7 +939,7 @@ func (p *Parser) tableRow(data []byte, columns []CellAlignFlags, header bool) {
}

// returns blockquote prefix length
func (p *Parser) quotePrefix(data []byte) int {
func (p *Markdown) quotePrefix(data []byte) int {
i := 0
for i < 3 && i < len(data) && data[i] == ' ' {
i++
Expand All @@ -955,7 +955,7 @@ func (p *Parser) quotePrefix(data []byte) int {

// blockquote ends with at least one blank line
// followed by something without a blockquote prefix
func (p *Parser) terminateBlockquote(data []byte, beg, end int) bool {
func (p *Markdown) terminateBlockquote(data []byte, beg, end int) bool {
if p.isEmpty(data[beg:]) <= 0 {
return false
}
Expand All @@ -966,7 +966,7 @@ func (p *Parser) terminateBlockquote(data []byte, beg, end int) bool {
}

// parse a blockquote fragment
func (p *Parser) quote(data []byte) int {
func (p *Markdown) quote(data []byte) int {
block := p.addBlock(BlockQuote, nil)
var raw bytes.Buffer
beg, end := 0, 0
Expand All @@ -976,7 +976,7 @@ func (p *Parser) quote(data []byte) int {
// fenced code and if one's found, incorporate it altogether,
// irregardless of any contents inside it
for end < len(data) && data[end] != '\n' {
if p.flags&FencedCode != 0 {
if p.extensions&FencedCode != 0 {
if i := p.fencedCodeBlock(data[end:], false); i > 0 {
// -1 to compensate for the extra end++ after the loop:
end += i - 1
Expand Down Expand Up @@ -1004,7 +1004,7 @@ func (p *Parser) quote(data []byte) int {
}

// returns prefix length for block code
func (p *Parser) codePrefix(data []byte) int {
func (p *Markdown) codePrefix(data []byte) int {
if len(data) >= 1 && data[0] == '\t' {
return 1
}
Expand All @@ -1014,7 +1014,7 @@ func (p *Parser) codePrefix(data []byte) int {
return 0
}

func (p *Parser) code(data []byte) int {
func (p *Markdown) code(data []byte) int {
var work bytes.Buffer

i := 0
Expand Down Expand Up @@ -1064,7 +1064,7 @@ func (p *Parser) code(data []byte) int {
}

// returns unordered list item prefix
func (p *Parser) uliPrefix(data []byte) int {
func (p *Markdown) uliPrefix(data []byte) int {
i := 0
// start with up to 3 spaces
for i < len(data) && i < 3 && data[i] == ' ' {
Expand All @@ -1082,7 +1082,7 @@ func (p *Parser) uliPrefix(data []byte) int {
}

// returns ordered list item prefix
func (p *Parser) oliPrefix(data []byte) int {
func (p *Markdown) oliPrefix(data []byte) int {
i := 0

// start with up to 3 spaces
Expand All @@ -1107,7 +1107,7 @@ func (p *Parser) oliPrefix(data []byte) int {
}

// returns definition list item prefix
func (p *Parser) dliPrefix(data []byte) int {
func (p *Markdown) dliPrefix(data []byte) int {
if len(data) < 2 {
return 0
}
Expand All @@ -1123,7 +1123,7 @@ func (p *Parser) dliPrefix(data []byte) int {
}

// parse ordered or unordered list block
func (p *Parser) list(data []byte, flags ListType) int {
func (p *Markdown) list(data []byte, flags ListType) int {
i := 0
flags |= ListItemBeginningOfList
block := p.addBlock(List, nil)
Expand Down Expand Up @@ -1191,7 +1191,7 @@ func finalizeList(block *Node) {

// Parse a single list item.
// Assumes initial prefix is already removed if this is a sublist.
func (p *Parser) listItem(data []byte, flags *ListType) int {
func (p *Markdown) listItem(data []byte, flags *ListType) int {
// keep track of the indentation of the first line
itemIndent := 0
if data[0] == '\t' {
Expand Down Expand Up @@ -1383,7 +1383,7 @@ gatherlines:
}

// render a single paragraph that has already been parsed out
func (p *Parser) renderParagraph(data []byte) {
func (p *Markdown) renderParagraph(data []byte) {
if len(data) == 0 {
return
}
Expand All @@ -1408,13 +1408,13 @@ func (p *Parser) renderParagraph(data []byte) {
p.addBlock(Paragraph, data[beg:end])
}

func (p *Parser) paragraph(data []byte) int {
func (p *Markdown) paragraph(data []byte) int {
// prev: index of 1st char of previous line
// line: index of 1st char of current line
// i: index of cursor/end of current line
var prev, line, i int
tabSize := TabSizeDefault
if p.flags&TabSizeEight != 0 {
if p.extensions&TabSizeEight != 0 {
tabSize = TabSizeDouble
}
// keep going until we find something to mark the end of the paragraph
Expand All @@ -1435,7 +1435,7 @@ func (p *Parser) paragraph(data []byte) int {
// did we find a blank line marking the end of the paragraph?
if n := p.isEmpty(current); n > 0 {
// did this blank line followed by a definition list item?
if p.flags&DefinitionLists != 0 {
if p.extensions&DefinitionLists != 0 {
if i < len(data)-1 && data[i+1] == ':' {
return p.list(data[prev:], ListTypeDefinition)
}
Expand All @@ -1461,7 +1461,7 @@ func (p *Parser) paragraph(data []byte) int {
}

id := ""
if p.flags&AutoHeadingIDs != 0 {
if p.extensions&AutoHeadingIDs != 0 {
id = sanitized_anchor_name.Create(string(data[prev:eol]))
}

Expand All @@ -1478,7 +1478,7 @@ func (p *Parser) paragraph(data []byte) int {
}

// if the next line starts a block of HTML, then the paragraph ends here
if p.flags&LaxHTMLBlocks != 0 {
if p.extensions&LaxHTMLBlocks != 0 {
if data[i] == '<' && p.html(current, false) > 0 {
// rewind to before the HTML block
p.renderParagraph(data[:i])
Expand All @@ -1493,23 +1493,23 @@ func (p *Parser) paragraph(data []byte) int {
}

// if there's a fenced code block, paragraph is over
if p.flags&FencedCode != 0 {
if p.extensions&FencedCode != 0 {
if p.fencedCodeBlock(current, false) > 0 {
p.renderParagraph(data[:i])
return i
}
}

// if there's a definition list item, prev line is a definition term
if p.flags&DefinitionLists != 0 {
if p.extensions&DefinitionLists != 0 {
if p.dliPrefix(current) != 0 {
ret := p.list(data[prev:], ListTypeDefinition)
return ret
}
}

// if there's a list after this, paragraph is over
if p.flags&NoEmptyLineBeforeBlock != 0 {
if p.extensions&NoEmptyLineBeforeBlock != 0 {
if p.uliPrefix(current) != 0 ||
p.oliPrefix(current) != 0 ||
p.quotePrefix(current) != 0 ||
Expand Down
Loading

0 comments on commit 70c446a

Please sign in to comment.