Skip to content

Commit 70c446a

Browse files
authored
Merge pull request #365 from Ambrevar/v2
Merge Parser into Processor
2 parents e7910a8 + 2501229 commit 70c446a

File tree

5 files changed

+134
-147
lines changed

5 files changed

+134
-147
lines changed

block.go

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ var (
3434
// Parse block-level data.
3535
// Note: this function and many that it calls assume that
3636
// the input buffer ends with a newline.
37-
func (p *Parser) block(data []byte) {
37+
func (p *Markdown) block(data []byte) {
3838
// this is called recursively: enforce a maximum depth
3939
if p.nesting >= p.maxNesting {
4040
return
@@ -71,7 +71,7 @@ func (p *Parser) block(data []byte) {
7171
// % stuff
7272
// % more stuff
7373
// % even more stuff
74-
if p.flags&Titleblock != 0 {
74+
if p.extensions&Titleblock != 0 {
7575
if data[0] == '%' {
7676
if i := p.titleBlock(data, true); i > 0 {
7777
data = data[i:]
@@ -109,7 +109,7 @@ func (p *Parser) block(data []byte) {
109109
// return n * fact(n-1)
110110
// }
111111
// ```
112-
if p.flags&FencedCode != 0 {
112+
if p.extensions&FencedCode != 0 {
113113
if i := p.fencedCodeBlock(data, true); i > 0 {
114114
data = data[i:]
115115
continue
@@ -147,7 +147,7 @@ func (p *Parser) block(data []byte) {
147147
// ------|-----|---------
148148
// Bob | 31 | 555-1234
149149
// Alice | 27 | 555-4321
150-
if p.flags&Tables != 0 {
150+
if p.extensions&Tables != 0 {
151151
if i := p.table(data); i > 0 {
152152
data = data[i:]
153153
continue
@@ -182,7 +182,7 @@ func (p *Parser) block(data []byte) {
182182
//
183183
// Term 2
184184
// : Definition c
185-
if p.flags&DefinitionLists != 0 {
185+
if p.extensions&DefinitionLists != 0 {
186186
if p.dliPrefix(data) > 0 {
187187
data = data[p.list(data, ListTypeDefinition):]
188188
continue
@@ -197,19 +197,19 @@ func (p *Parser) block(data []byte) {
197197
p.nesting--
198198
}
199199

200-
func (p *Parser) addBlock(typ NodeType, content []byte) *Node {
200+
func (p *Markdown) addBlock(typ NodeType, content []byte) *Node {
201201
p.closeUnmatchedBlocks()
202202
container := p.addChild(typ, 0)
203203
container.content = content
204204
return container
205205
}
206206

207-
func (p *Parser) isPrefixHeading(data []byte) bool {
207+
func (p *Markdown) isPrefixHeading(data []byte) bool {
208208
if data[0] != '#' {
209209
return false
210210
}
211211

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

224-
func (p *Parser) prefixHeading(data []byte) int {
224+
func (p *Markdown) prefixHeading(data []byte) int {
225225
level := 0
226226
for level < 6 && level < len(data) && data[level] == '#' {
227227
level++
@@ -230,7 +230,7 @@ func (p *Parser) prefixHeading(data []byte) int {
230230
end := skipUntilChar(data, i, '\n')
231231
skip := end
232232
id := ""
233-
if p.flags&HeadingIDs != 0 {
233+
if p.extensions&HeadingIDs != 0 {
234234
j, k := 0, 0
235235
// find start/end of heading id
236236
for j = i; j < end-1 && (data[j] != '{' || data[j+1] != '#'); j++ {
@@ -257,7 +257,7 @@ func (p *Parser) prefixHeading(data []byte) int {
257257
end--
258258
}
259259
if end > i {
260-
if id == "" && p.flags&AutoHeadingIDs != 0 {
260+
if id == "" && p.extensions&AutoHeadingIDs != 0 {
261261
id = sanitized_anchor_name.Create(string(data[i:end]))
262262
}
263263
block := p.addBlock(Heading, data[i:end])
@@ -267,7 +267,7 @@ func (p *Parser) prefixHeading(data []byte) int {
267267
return skip
268268
}
269269

270-
func (p *Parser) isUnderlinedHeading(data []byte) int {
270+
func (p *Markdown) isUnderlinedHeading(data []byte) int {
271271
// test of level 1 heading
272272
if data[0] == '=' {
273273
i := skipChar(data, 1, '=')
@@ -291,7 +291,7 @@ func (p *Parser) isUnderlinedHeading(data []byte) int {
291291
return 0
292292
}
293293

294-
func (p *Parser) titleBlock(data []byte, doRender bool) int {
294+
func (p *Markdown) titleBlock(data []byte, doRender bool) int {
295295
if data[0] != '%' {
296296
return 0
297297
}
@@ -315,7 +315,7 @@ func (p *Parser) titleBlock(data []byte, doRender bool) int {
315315
return consumed
316316
}
317317

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

321321
// identify the opening tag
@@ -419,7 +419,7 @@ func finalizeHTMLBlock(block *Node) {
419419
}
420420

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

441441
// HR, which is the only self-closing block tag considered
442-
func (p *Parser) htmlHr(data []byte, doRender bool) int {
442+
func (p *Markdown) htmlHr(data []byte, doRender bool) int {
443443
if len(data) < 4 {
444444
return 0
445445
}
@@ -472,7 +472,7 @@ func (p *Parser) htmlHr(data []byte, doRender bool) int {
472472
return 0
473473
}
474474

475-
func (p *Parser) htmlFindTag(data []byte) (string, bool) {
475+
func (p *Markdown) htmlFindTag(data []byte) (string, bool) {
476476
i := 0
477477
for i < len(data) && isalnum(data[i]) {
478478
i++
@@ -484,7 +484,7 @@ func (p *Parser) htmlFindTag(data []byte) (string, bool) {
484484
return "", false
485485
}
486486

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

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

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

540-
func (*Parser) isHRule(data []byte) bool {
540+
func (*Markdown) isHRule(data []byte) bool {
541541
i := 0
542542

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

742-
func (p *Parser) table(data []byte) int {
742+
func (p *Markdown) table(data []byte) int {
743743
table := p.addBlock(Table, nil)
744744
i, columns := p.tableHeader(data)
745745
if i == 0 {
@@ -782,7 +782,7 @@ func isBackslashEscaped(data []byte, i int) bool {
782782
return backslashes&1 == 1
783783
}
784784

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

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

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

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

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

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

10061006
// returns prefix length for block code
1007-
func (p *Parser) codePrefix(data []byte) int {
1007+
func (p *Markdown) codePrefix(data []byte) int {
10081008
if len(data) >= 1 && data[0] == '\t' {
10091009
return 1
10101010
}
@@ -1014,7 +1014,7 @@ func (p *Parser) codePrefix(data []byte) int {
10141014
return 0
10151015
}
10161016

1017-
func (p *Parser) code(data []byte) int {
1017+
func (p *Markdown) code(data []byte) int {
10181018
var work bytes.Buffer
10191019

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

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

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

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

11091109
// returns definition list item prefix
1110-
func (p *Parser) dliPrefix(data []byte) int {
1110+
func (p *Markdown) dliPrefix(data []byte) int {
11111111
if len(data) < 2 {
11121112
return 0
11131113
}
@@ -1123,7 +1123,7 @@ func (p *Parser) dliPrefix(data []byte) int {
11231123
}
11241124

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

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

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

1411-
func (p *Parser) paragraph(data []byte) int {
1411+
func (p *Markdown) paragraph(data []byte) int {
14121412
// prev: index of 1st char of previous line
14131413
// line: index of 1st char of current line
14141414
// i: index of cursor/end of current line
14151415
var prev, line, i int
14161416
tabSize := TabSizeDefault
1417-
if p.flags&TabSizeEight != 0 {
1417+
if p.extensions&TabSizeEight != 0 {
14181418
tabSize = TabSizeDouble
14191419
}
14201420
// keep going until we find something to mark the end of the paragraph
@@ -1435,7 +1435,7 @@ func (p *Parser) paragraph(data []byte) int {
14351435
// did we find a blank line marking the end of the paragraph?
14361436
if n := p.isEmpty(current); n > 0 {
14371437
// did this blank line followed by a definition list item?
1438-
if p.flags&DefinitionLists != 0 {
1438+
if p.extensions&DefinitionLists != 0 {
14391439
if i < len(data)-1 && data[i+1] == ':' {
14401440
return p.list(data[prev:], ListTypeDefinition)
14411441
}
@@ -1461,7 +1461,7 @@ func (p *Parser) paragraph(data []byte) int {
14611461
}
14621462

14631463
id := ""
1464-
if p.flags&AutoHeadingIDs != 0 {
1464+
if p.extensions&AutoHeadingIDs != 0 {
14651465
id = sanitized_anchor_name.Create(string(data[prev:eol]))
14661466
}
14671467

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

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

14951495
// if there's a fenced code block, paragraph is over
1496-
if p.flags&FencedCode != 0 {
1496+
if p.extensions&FencedCode != 0 {
14971497
if p.fencedCodeBlock(current, false) > 0 {
14981498
p.renderParagraph(data[:i])
14991499
return i
15001500
}
15011501
}
15021502

15031503
// if there's a definition list item, prev line is a definition term
1504-
if p.flags&DefinitionLists != 0 {
1504+
if p.extensions&DefinitionLists != 0 {
15051505
if p.dliPrefix(current) != 0 {
15061506
ret := p.list(data[prev:], ListTypeDefinition)
15071507
return ret
15081508
}
15091509
}
15101510

15111511
// if there's a list after this, paragraph is over
1512-
if p.flags&NoEmptyLineBeforeBlock != 0 {
1512+
if p.extensions&NoEmptyLineBeforeBlock != 0 {
15131513
if p.uliPrefix(current) != 0 ||
15141514
p.oliPrefix(current) != 0 ||
15151515
p.quotePrefix(current) != 0 ||

0 commit comments

Comments
 (0)