From 832ef9be61048d95fa7279078daacdc1170a049c Mon Sep 17 00:00:00 2001 From: Krzysztof Kowalczyk Date: Sun, 28 Jan 2018 18:27:38 -0800 Subject: [PATCH] un-export some stuff from parser package --- block_test.go | 105 ---------------------------------------- parser/block.go | 20 ++++---- parser/doc.go | 4 -- parser/parser.go | 9 ++-- parser/parser_test.go | 110 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 126 insertions(+), 122 deletions(-) delete mode 100644 parser/doc.go create mode 100644 parser/parser_test.go diff --git a/block_test.go b/block_test.go index d09109a..efa3792 100644 --- a/block_test.go +++ b/block_test.go @@ -1652,108 +1652,3 @@ func TestSpaceHeadings(t *testing.T) { } doTestsParam(t, tests, TestParams{extensions: parser.SpaceHeadings}) } - -func TestIsFenceLine(t *testing.T) { - t.Parallel() - tests := []struct { - data []byte - syntaxRequested bool - wantEnd int - wantMarker string - wantSyntax string - }{ - { - data: []byte("```"), - wantEnd: 3, - wantMarker: "```", - }, - { - data: []byte("```\nstuff here\n"), - wantEnd: 4, - wantMarker: "```", - }, - { - data: []byte("```\nstuff here\n"), - syntaxRequested: true, - wantEnd: 4, - wantMarker: "```", - }, - { - data: []byte("stuff here\n```\n"), - wantEnd: 0, - }, - { - data: []byte("```"), - syntaxRequested: true, - wantEnd: 3, - wantMarker: "```", - }, - { - data: []byte("``` go"), - syntaxRequested: true, - wantEnd: 6, - wantMarker: "```", - wantSyntax: "go", - }, - } - - for _, test := range tests { - var syntax *string - if test.syntaxRequested { - syntax = new(string) - } - end, marker := parser.IsFenceLine(test.data, syntax, "```") - if got, want := end, test.wantEnd; got != want { - t.Errorf("got end %v, want %v", got, want) - } - if got, want := marker, test.wantMarker; got != want { - t.Errorf("got marker %q, want %q", got, want) - } - if test.syntaxRequested { - if got, want := *syntax, test.wantSyntax; got != want { - t.Errorf("got syntax %q, want %q", got, want) - } - } - } -} - -func TestSanitizedAnchorName(t *testing.T) { - tests := []struct { - text string - want string - }{ - { - text: "This is a header", - want: "this-is-a-header", - }, - { - text: "This is also a header", - want: "this-is-also-a-header", - }, - { - text: "main.go", - want: "main-go", - }, - { - text: "Article 123", - want: "article-123", - }, - { - text: "<- Let's try this, shall we?", - want: "let-s-try-this-shall-we", - }, - { - text: " ", - want: "", - }, - { - text: "Hello, 世界", - want: "hello-世界", - }, - } - for _, test := range tests { - if got := parser.SanitizeAnchorName(test.text); got != test.want { - t.Errorf("SanitizedAnchorName(%q):\ngot %q\nwant %q", test.text, got, test.want) - } - } -} diff --git a/parser/block.go b/parser/block.go index 1f33c0d..7fb38b5 100644 --- a/parser/block.go +++ b/parser/block.go @@ -66,9 +66,9 @@ var ( } ) -// SanitizeAnchorName returns a sanitized anchor name for the given text. +// sanitizeAnchorName returns a sanitized anchor name for the given text. // Taken from https://github.com/shurcooL/sanitized_anchor_name/blob/master/main.go#L14:1 -func SanitizeAnchorName(text string) string { +func sanitizeAnchorName(text string) string { var anchorName []rune var futureDash = false for _, r := range text { @@ -303,7 +303,7 @@ func (p *Parser) prefixHeading(data []byte) int { } if end > i { if id == "" && p.extensions&AutoHeadingIDs != 0 { - id = SanitizeAnchorName(string(data[i:end])) + id = sanitizeAnchorName(string(data[i:end])) } block := &ast.Heading{ HeadingID: id, @@ -608,10 +608,10 @@ func (*Parser) isHRule(data []byte) bool { return n >= 3 } -// IsFenceLine checks if there's a fence line (e.g., ``` or ``` go) at the beginning of data, +// sFenceLine checks if there's a fence line (e.g., ``` or ``` go) at the beginning of data, // and returns the end index if so, or 0 otherwise. It also returns the marker found. // If syntax is not nil, it gets set to the syntax specified in the fence line. -func IsFenceLine(data []byte, syntax *string, oldmarker string) (end int, marker string) { +func sFenceLine(data []byte, syntax *string, oldmarker string) (end int, marker string) { i, size := 0, 0 n := len(data) @@ -712,7 +712,7 @@ func IsFenceLine(data []byte, syntax *string, oldmarker string) (end int, marker // If doRender is true, a final newline is mandatory to recognize the fenced code block. func (p *Parser) fencedCodeBlock(data []byte, doRender bool) int { var syntax string - beg, marker := IsFenceLine(data, &syntax, "") + beg, marker := sFenceLine(data, &syntax, "") if beg == 0 || beg >= len(data) { return 0 } @@ -725,7 +725,7 @@ func (p *Parser) fencedCodeBlock(data []byte, doRender bool) int { // safe to assume beg < len(data) // check for the end of the code block - fenceEnd, _ := IsFenceLine(data[beg:], nil, marker) + fenceEnd, _ := sFenceLine(data[beg:], nil, marker) if fenceEnd != 0 { beg += fenceEnd break @@ -1455,9 +1455,9 @@ func (p *Parser) paragraph(data []byte) int { // line: index of 1st char of current line // i: index of cursor/end of current line var prev, line, i int - tabSize := TabSizeDefault + tabSize := tabSizeDefault if p.extensions&TabSizeEight != 0 { - tabSize = TabSizeDouble + tabSize = tabSizeDouble } // keep going until we find something to mark the end of the paragraph for i < len(data) { @@ -1504,7 +1504,7 @@ func (p *Parser) paragraph(data []byte) int { id := "" if p.extensions&AutoHeadingIDs != 0 { - id = SanitizeAnchorName(string(data[prev:eol])) + id = sanitizeAnchorName(string(data[prev:eol])) } block := &ast.Heading{ diff --git a/parser/doc.go b/parser/doc.go deleted file mode 100644 index d7f8e91..0000000 --- a/parser/doc.go +++ /dev/null @@ -1,4 +0,0 @@ -/* -Package parser implements parsing of markdown document into AST tree. -*/ -package parser diff --git a/parser/parser.go b/parser/parser.go index 6793148..0c84aed 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -1,3 +1,6 @@ +/* +Package parser implements parser for markdown document generating AST (abstract syntax tree). +*/ package parser // Callback functions for inline parsing. One such function is defined @@ -10,7 +13,7 @@ import ( "github.com/gomarkdown/markdown/ast" ) -// Extensions is a bitmask of enabled Parser extensions. +// Extensions is a bitmask of enabled parser extensions. type Extensions int // Bit flags representing markdown parsing extensions. @@ -41,8 +44,8 @@ const ( // The size of a tab stop. const ( - TabSizeDefault = 4 - TabSizeDouble = 8 + tabSizeDefault = 4 + tabSizeDouble = 8 ) // for each character that triggers a response when parsing inline data. diff --git a/parser/parser_test.go b/parser/parser_test.go new file mode 100644 index 0000000..ce7f38d --- /dev/null +++ b/parser/parser_test.go @@ -0,0 +1,110 @@ +package parser + +import ( + "testing" +) + +func TestIsFenceLine(t *testing.T) { + t.Parallel() + tests := []struct { + data []byte + syntaxRequested bool + wantEnd int + wantMarker string + wantSyntax string + }{ + { + data: []byte("```"), + wantEnd: 3, + wantMarker: "```", + }, + { + data: []byte("```\nstuff here\n"), + wantEnd: 4, + wantMarker: "```", + }, + { + data: []byte("```\nstuff here\n"), + syntaxRequested: true, + wantEnd: 4, + wantMarker: "```", + }, + { + data: []byte("stuff here\n```\n"), + wantEnd: 0, + }, + { + data: []byte("```"), + syntaxRequested: true, + wantEnd: 3, + wantMarker: "```", + }, + { + data: []byte("``` go"), + syntaxRequested: true, + wantEnd: 6, + wantMarker: "```", + wantSyntax: "go", + }, + } + + for _, test := range tests { + var syntax *string + if test.syntaxRequested { + syntax = new(string) + } + end, marker := sFenceLine(test.data, syntax, "```") + if got, want := end, test.wantEnd; got != want { + t.Errorf("got end %v, want %v", got, want) + } + if got, want := marker, test.wantMarker; got != want { + t.Errorf("got marker %q, want %q", got, want) + } + if test.syntaxRequested { + if got, want := *syntax, test.wantSyntax; got != want { + t.Errorf("got syntax %q, want %q", got, want) + } + } + } +} + +func TestSanitizedAnchorName(t *testing.T) { + tests := []struct { + text string + want string + }{ + { + text: "This is a header", + want: "this-is-a-header", + }, + { + text: "This is also a header", + want: "this-is-also-a-header", + }, + { + text: "main.go", + want: "main-go", + }, + { + text: "Article 123", + want: "article-123", + }, + { + text: "<- Let's try this, shall we?", + want: "let-s-try-this-shall-we", + }, + { + text: " ", + want: "", + }, + { + text: "Hello, 世界", + want: "hello-世界", + }, + } + for _, test := range tests { + if got := sanitizeAnchorName(test.text); got != test.want { + t.Errorf("SanitizedAnchorName(%q):\ngot %q\nwant %q", test.text, got, test.want) + } + } +}