Skip to content

Commit af57319

Browse files
authored
Merge pull request #328 from russross/v2-functional-opts
Change the public interface to use functional options
2 parents 5ebfae5 + 41159b3 commit af57319

File tree

7 files changed

+209
-199
lines changed

7 files changed

+209
-199
lines changed

block.go

Lines changed: 29 additions & 29 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 *Parser) block(data []byte) {
3838
// this is called recursively: enforce a maximum depth
3939
if p.nesting >= p.maxNesting {
4040
return
@@ -197,14 +197,14 @@ func (p *parser) block(data []byte) {
197197
p.nesting--
198198
}
199199

200-
func (p *parser) addBlock(typ NodeType, content []byte) *Node {
200+
func (p *Parser) 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 *Parser) isPrefixHeading(data []byte) bool {
208208
if data[0] != '#' {
209209
return false
210210
}
@@ -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 *Parser) prefixHeading(data []byte) int {
225225
level := 0
226226
for level < 6 && level < len(data) && data[level] == '#' {
227227
level++
@@ -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 *Parser) 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 *Parser) 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 *Parser) 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 *Parser) 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 *Parser) 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 *Parser) 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 *Parser) htmlFindEnd(tag string, data []byte) int {
488488
// assume data[0] == '<' && data[1] == '/' already tested
489489
if tag == "hr" {
490490
return 2
@@ -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 (*Parser) 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 (*Parser) 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 *Parser) 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 *Parser) 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 *Parser) 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 *Parser) 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 *Parser) 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 *Parser) 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 *Parser) quote(data []byte) int {
970970
block := p.addBlock(BlockQuote, nil)
971971
var raw bytes.Buffer
972972
beg, end := 0, 0
@@ -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 *Parser) 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 *Parser) 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 *Parser) 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 *Parser) 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 *Parser) 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 *Parser) 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 *Parser) 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 *Parser) renderParagraph(data []byte) {
13871387
if len(data) == 0 {
13881388
return
13891389
}
@@ -1408,7 +1408,7 @@ 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 *Parser) 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

block_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ func TestPrefixHeaderIdExtensionWithPrefixAndSuffix(t *testing.T) {
253253
}
254254

255255
doTestsParam(t, tests, TestParams{
256-
Options: Options{Extensions: HeadingIDs},
256+
extensions: HeadingIDs,
257257
HTMLFlags: UseXHTML,
258258
HTMLRendererParameters: parameters,
259259
})
@@ -365,7 +365,7 @@ func TestPrefixAutoHeaderIdExtensionWithPrefixAndSuffix(t *testing.T) {
365365
}
366366

367367
doTestsParam(t, tests, TestParams{
368-
Options: Options{Extensions: AutoHeadingIDs},
368+
extensions: AutoHeadingIDs,
369369
HTMLFlags: UseXHTML,
370370
HTMLRendererParameters: parameters,
371371
})

helpers_test.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import (
2121
)
2222

2323
type TestParams struct {
24-
Options
24+
extensions Extensions
25+
referenceOverride ReferenceOverrideFunc
2526
HTMLFlags
2627
HTMLRendererParameters
2728
}
@@ -45,13 +46,15 @@ func execRecoverableTestSuite(t *testing.T, tests []string, params TestParams, s
4546
func runMarkdown(input string, params TestParams) string {
4647
params.HTMLRendererParameters.Flags = params.HTMLFlags
4748
renderer := NewHTMLRenderer(params.HTMLRendererParameters)
48-
return string(Markdown([]byte(input), renderer, params.Options))
49+
return string(Markdown([]byte(input), WithRenderer(renderer),
50+
WithExtensions(params.extensions),
51+
WithRefOverride(params.referenceOverride)))
4952
}
5053

5154
// doTests runs full document tests using MarkdownCommon configuration.
5255
func doTests(t *testing.T, tests []string) {
5356
doTestsParam(t, tests, TestParams{
54-
Options: DefaultOptions,
57+
extensions: CommonExtensions,
5558
HTMLRendererParameters: HTMLRendererParameters{
5659
Flags: CommonHTMLFlags,
5760
},
@@ -60,8 +63,8 @@ func doTests(t *testing.T, tests []string) {
6063

6164
func doTestsBlock(t *testing.T, tests []string, extensions Extensions) {
6265
doTestsParam(t, tests, TestParams{
63-
Options: Options{Extensions: extensions},
64-
HTMLFlags: UseXHTML,
66+
extensions: extensions,
67+
HTMLFlags: UseXHTML,
6568
})
6669
}
6770

@@ -124,8 +127,7 @@ func doSafeTestsInline(t *testing.T, tests []string) {
124127
}
125128

126129
func doTestsInlineParam(t *testing.T, tests []string, params TestParams) {
127-
params.Options.Extensions |= Autolink
128-
params.Options.Extensions |= Strikethrough
130+
params.extensions |= Autolink | Strikethrough
129131
params.HTMLFlags |= UseXHTML
130132
doTestsParam(t, tests, params)
131133
}
@@ -145,7 +147,7 @@ func transformLinks(tests []string, prefix string) []string {
145147
}
146148

147149
func doTestsReference(t *testing.T, files []string, flag Extensions) {
148-
params := TestParams{Options: Options{Extensions: flag}}
150+
params := TestParams{extensions: flag}
149151
execRecoverableTestSuite(t, files, params, func(candidate *string) {
150152
for _, basename := range files {
151153
filename := filepath.Join("testdata", basename+".text")

0 commit comments

Comments
 (0)