From 748be0c096914240b2c43fa603f600d3f3a06028 Mon Sep 17 00:00:00 2001 From: Cameron Moore Date: Fri, 29 Nov 2019 13:20:34 -0600 Subject: [PATCH 1/5] Fix shadow declarations --- ast/ast.go | 2 +- extension/table.go | 8 ++++---- parser/attribute.go | 7 +++---- parser/atx_heading.go | 20 ++++++++++---------- parser/code_span.go | 2 +- parser/parser.go | 3 +-- 6 files changed, 20 insertions(+), 22 deletions(-) diff --git a/ast/ast.go b/ast/ast.go index 025c599..c5edfe5 100644 --- a/ast/ast.go +++ b/ast/ast.go @@ -436,7 +436,7 @@ func Walk(n Node, walker Walker) error { } if status != WalkSkipChildren { for c := n.FirstChild(); c != nil; c = c.NextSibling() { - if err := Walk(c, walker); err != nil { + if err = Walk(c, walker); err != nil { return err } } diff --git a/extension/table.go b/extension/table.go index 8ae999b..e20327d 100644 --- a/extension/table.go +++ b/extension/table.go @@ -84,10 +84,10 @@ func (b *tableParagraphTransformer) parseRow(segment text.Segment, alignments [] closure = len(line[pos:]) } node := ast.NewTableCell() - segment := text.NewSegment(segment.Start+pos, segment.Start+pos+closure) - segment = segment.TrimLeftSpace(source) - segment = segment.TrimRightSpace(source) - node.Lines().Append(segment) + seg := text.NewSegment(segment.Start+pos, segment.Start+pos+closure) + seg = seg.TrimLeftSpace(source) + seg = seg.TrimRightSpace(source) + node.Lines().Append(seg) node.Alignment = alignment row.AppendChild(row, node) pos += closure + 1 diff --git a/parser/attribute.go b/parser/attribute.go index e5f0250..42b1753 100644 --- a/parser/attribute.go +++ b/parser/attribute.go @@ -2,9 +2,10 @@ package parser import ( "bytes" + "strconv" + "github.com/yuin/goldmark/text" "github.com/yuin/goldmark/util" - "strconv" ) var attrNameID = []byte("id") @@ -104,7 +105,7 @@ func parseAttribute(reader text.Reader) (Attribute, bool) { } i := 0 for ; i < len(line); i++ { - c := line[i] + c = line[i] if !((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' || c == ':' || c == '.' || c == '-') { @@ -125,7 +126,6 @@ func parseAttribute(reader text.Reader) (Attribute, bool) { return Attribute{}, false } return Attribute{Name: name, Value: value}, true - } func parseAttributeValue(reader text.Reader) (interface{}, bool) { @@ -153,7 +153,6 @@ func parseAttributeValue(reader text.Reader) (interface{}, bool) { return nil, false } return value, true - } func parseAttributeArray(reader text.Reader) ([]interface{}, bool) { diff --git a/parser/atx_heading.go b/parser/atx_heading.go index 67f55ac..c9843e3 100644 --- a/parser/atx_heading.go +++ b/parser/atx_heading.go @@ -108,19 +108,19 @@ func (b *atxHeadingParser) Open(parent ast.Node, reader text.Reader, pc Context) start-- closureClose := -1 closureOpen := -1 - for i := start; i < stop; { - c := line[i] - if util.IsEscapedPunctuation(line, i) { - i += 2 - } else if util.IsSpace(c) && i < stop-1 && line[i+1] == '#' { - closureOpen = i + 1 - j := i + 1 - for ; j < stop && line[j] == '#'; j++ { + for j := start; j < stop; { + c := line[j] + if util.IsEscapedPunctuation(line, j) { + j += 2 + } else if util.IsSpace(c) && j < stop-1 && line[j+1] == '#' { + closureOpen = j + 1 + k := j + 1 + for ; k < stop && line[k] == '#'; k++ { } - closureClose = j + closureClose = k break } else { - i++ + j++ } } if closureClose > 0 { diff --git a/parser/code_span.go b/parser/code_span.go index b815938..afb2b7f 100644 --- a/parser/code_span.go +++ b/parser/code_span.go @@ -43,7 +43,7 @@ func (s *codeSpanParser) Parse(parent ast.Node, block text.Reader, pc Context) a } closure := i - oldi if closure == opener && (i+1 >= len(line) || line[i+1] != '`') { - segment := segment.WithStop(segment.Start + i - closure) + segment = segment.WithStop(segment.Start + i - closure) if !segment.IsEmpty() { node.AppendChild(node, ast.NewRawTextSegment(segment)) } diff --git a/parser/parser.go b/parser/parser.go index 9eb0b8a..a06b643 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -903,7 +903,7 @@ retry: if w > 3 && !bp.CanAcceptIndentedLine() { continue } - lastBlock := pc.LastOpenedBlock() + lastBlock = pc.LastOpenedBlock() last := lastBlock.Node node, state := bp.Open(parent, reader, pc) if node != nil { @@ -1177,5 +1177,4 @@ func (p *parser) parseBlock(block text.BlockReader, parent ast.Node, pc Context) for _, ip := range p.closeBlockers { ip.CloseBlock(parent, block, pc) } - } From 2932dadfb3152230930f9a79b87499f6602046f2 Mon Sep 17 00:00:00 2001 From: Cameron Moore Date: Fri, 29 Nov 2019 13:22:11 -0600 Subject: [PATCH 2/5] Fix gofmt formatting --- commonmark_test.go | 2 +- options_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/commonmark_test.go b/commonmark_test.go index 4e15daa..e1a2d1b 100644 --- a/commonmark_test.go +++ b/commonmark_test.go @@ -6,8 +6,8 @@ import ( "testing" . "github.com/yuin/goldmark" - "github.com/yuin/goldmark/testutil" "github.com/yuin/goldmark/renderer/html" + "github.com/yuin/goldmark/testutil" ) type commonmarkSpecTestCase struct { diff --git a/options_test.go b/options_test.go index d024a52..4375b63 100644 --- a/options_test.go +++ b/options_test.go @@ -4,8 +4,8 @@ import ( "testing" . "github.com/yuin/goldmark" - "github.com/yuin/goldmark/testutil" "github.com/yuin/goldmark/parser" + "github.com/yuin/goldmark/testutil" ) func TestAttributeAndAutoHeadingID(t *testing.T) { From 3dc5ebdb17dbc71bb97f05dcfe58fb64e4f5e184 Mon Sep 17 00:00:00 2001 From: Cameron Moore Date: Fri, 29 Nov 2019 13:31:28 -0600 Subject: [PATCH 3/5] Fix golint issues --- fuzz/fuzz.go | 2 ++ parser/html_block.go | 9 +++++---- parser/thematic_break.go | 2 +- renderer/renderer.go | 5 ++--- testutil/testutil.go | 4 ++++ text/reader.go | 7 ++++--- util/util.go | 2 +- 7 files changed, 19 insertions(+), 12 deletions(-) diff --git a/fuzz/fuzz.go b/fuzz/fuzz.go index f56dec6..629f5cc 100644 --- a/fuzz/fuzz.go +++ b/fuzz/fuzz.go @@ -2,11 +2,13 @@ package fuzz import ( "bytes" + "github.com/yuin/goldmark" "github.com/yuin/goldmark/extension" "github.com/yuin/goldmark/renderer/html" ) +// Fuzz runs automated fuzzing against goldmark. func Fuzz(data []byte) int { markdown := goldmark.New( goldmark.WithRendererOptions( diff --git a/parser/html_block.go b/parser/html_block.go index 752ba08..c796354 100644 --- a/parser/html_block.go +++ b/parser/html_block.go @@ -2,11 +2,12 @@ package parser import ( "bytes" + "regexp" + "strings" + "github.com/yuin/goldmark/ast" "github.com/yuin/goldmark/text" "github.com/yuin/goldmark/util" - "regexp" - "strings" ) var allowedBlockTags = map[string]bool{ @@ -97,12 +98,12 @@ var htmlBlockType7Regexp = regexp.MustCompile(`^[ ]{0,3}<(/)?([a-zA-Z0-9]+)(` + type htmlBlockParser struct { } -var defaultHtmlBlockParser = &htmlBlockParser{} +var defaultHTMLBlockParser = &htmlBlockParser{} // NewHTMLBlockParser return a new BlockParser that can parse html // blocks. func NewHTMLBlockParser() BlockParser { - return defaultHtmlBlockParser + return defaultHTMLBlockParser } func (b *htmlBlockParser) Trigger() []byte { diff --git a/parser/thematic_break.go b/parser/thematic_break.go index 2f5c9fa..4d0d8da 100644 --- a/parser/thematic_break.go +++ b/parser/thematic_break.go @@ -11,7 +11,7 @@ type thematicBreakPraser struct { var defaultThematicBreakPraser = &thematicBreakPraser{} -// NewThematicBreakPraser returns a new BlockParser that +// NewThematicBreakParser returns a new BlockParser that // parses thematic breaks. func NewThematicBreakParser() BlockParser { return defaultThematicBreakPraser diff --git a/renderer/renderer.go b/renderer/renderer.go index 1681305..5c11ceb 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -1,14 +1,13 @@ -// Package renderer renders the given AST to certain formats. +// Package renderer renders the given AST to certain formats. package renderer import ( "bufio" "io" + "sync" "github.com/yuin/goldmark/ast" "github.com/yuin/goldmark/util" - - "sync" ) // A Config struct is a data structure that holds configuration of the Renderer. diff --git a/testutil/testutil.go b/testutil/testutil.go index 4a83a03..4b9cff6 100644 --- a/testutil/testutil.go +++ b/testutil/testutil.go @@ -21,6 +21,7 @@ type TestingT interface { FailNow() } +// MarkdownTestCase represents a test case. type MarkdownTestCase struct { No int Markdown string @@ -30,6 +31,7 @@ type MarkdownTestCase struct { const attributeSeparator = "//- - - - - - - - -//" const caseSeparator = "//= = = = = = = = = = = = = = = = = = = = = = = =//" +// DoTestCaseFile runs test cases in a given file. func DoTestCaseFile(m goldmark.Markdown, filename string, t TestingT) { fp, err := os.Open(filename) if err != nil { @@ -86,12 +88,14 @@ func DoTestCaseFile(m goldmark.Markdown, filename string, t TestingT) { DoTestCases(m, cases, t) } +// DoTestCases runs a set of test cases. func DoTestCases(m goldmark.Markdown, cases []MarkdownTestCase, t TestingT) { for _, testCase := range cases { DoTestCase(m, testCase, t) } } +// DoTestCase runs a test case. func DoTestCase(m goldmark.Markdown, testCase MarkdownTestCase, t TestingT) { var ok bool var out bytes.Buffer diff --git a/text/reader.go b/text/reader.go index 0e2c3f0..a8db23b 100644 --- a/text/reader.go +++ b/text/reader.go @@ -1,10 +1,11 @@ package text import ( - "github.com/yuin/goldmark/util" "io" "regexp" "unicode/utf8" + + "github.com/yuin/goldmark/util" ) const invalidValue = -1 @@ -138,7 +139,7 @@ func (r *reader) LineOffset() int { if r.source[i] == '\t' { v += util.TabWidth(v) } else { - v += 1 + v++ } } r.lineOffset = v - r.pos.Padding @@ -355,7 +356,7 @@ func (r *blockReader) LineOffset() int { if r.source[i] == '\t' { v += util.TabWidth(v) } else { - v += 1 + v++ } } r.lineOffset = v - r.pos.Padding diff --git a/util/util.go b/util/util.go index 57a2b84..74f991b 100644 --- a/util/util.go +++ b/util/util.go @@ -631,7 +631,7 @@ func URLEscape(v []byte, resolveReference bool) []byte { cob.Write(v[n:i]) stop := i + int(u8len) if stop > len(v) { - i += 1 + i++ n = i continue } From ff066ede8208590905537cf0879ff38e09339d82 Mon Sep 17 00:00:00 2001 From: Cameron Moore Date: Fri, 29 Nov 2019 13:39:42 -0600 Subject: [PATCH 4/5] Fix gofmt issues --- extension/definition_list_test.go | 2 +- extension/footnote_test.go | 2 +- extension/strikethrough_test.go | 2 +- extension/table_test.go | 2 +- extension/tasklist_test.go | 2 +- extension/typographer_test.go | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/extension/definition_list_test.go b/extension/definition_list_test.go index 2ee3c61..6280a64 100644 --- a/extension/definition_list_test.go +++ b/extension/definition_list_test.go @@ -4,8 +4,8 @@ import ( "testing" "github.com/yuin/goldmark" - "github.com/yuin/goldmark/testutil" "github.com/yuin/goldmark/renderer/html" + "github.com/yuin/goldmark/testutil" ) func TestDefinitionList(t *testing.T) { diff --git a/extension/footnote_test.go b/extension/footnote_test.go index 0f2e0b5..447d177 100644 --- a/extension/footnote_test.go +++ b/extension/footnote_test.go @@ -4,8 +4,8 @@ import ( "testing" "github.com/yuin/goldmark" - "github.com/yuin/goldmark/testutil" "github.com/yuin/goldmark/renderer/html" + "github.com/yuin/goldmark/testutil" ) func TestFootnote(t *testing.T) { diff --git a/extension/strikethrough_test.go b/extension/strikethrough_test.go index 05660ee..2569bff 100644 --- a/extension/strikethrough_test.go +++ b/extension/strikethrough_test.go @@ -4,8 +4,8 @@ import ( "testing" "github.com/yuin/goldmark" - "github.com/yuin/goldmark/testutil" "github.com/yuin/goldmark/renderer/html" + "github.com/yuin/goldmark/testutil" ) func TestStrikethrough(t *testing.T) { diff --git a/extension/table_test.go b/extension/table_test.go index 655f3fc..baacdf7 100644 --- a/extension/table_test.go +++ b/extension/table_test.go @@ -4,8 +4,8 @@ import ( "testing" "github.com/yuin/goldmark" - "github.com/yuin/goldmark/testutil" "github.com/yuin/goldmark/renderer/html" + "github.com/yuin/goldmark/testutil" ) func TestTable(t *testing.T) { diff --git a/extension/tasklist_test.go b/extension/tasklist_test.go index 594471f..6a04cba 100644 --- a/extension/tasklist_test.go +++ b/extension/tasklist_test.go @@ -4,8 +4,8 @@ import ( "testing" "github.com/yuin/goldmark" - "github.com/yuin/goldmark/testutil" "github.com/yuin/goldmark/renderer/html" + "github.com/yuin/goldmark/testutil" ) func TestTaskList(t *testing.T) { diff --git a/extension/typographer_test.go b/extension/typographer_test.go index bc1b21f..0b1b66f 100644 --- a/extension/typographer_test.go +++ b/extension/typographer_test.go @@ -4,8 +4,8 @@ import ( "testing" "github.com/yuin/goldmark" - "github.com/yuin/goldmark/testutil" "github.com/yuin/goldmark/renderer/html" + "github.com/yuin/goldmark/testutil" ) func TestTypographer(t *testing.T) { From 74e1374f5a42a90d2ce73c5e7563ac3f0c33cda8 Mon Sep 17 00:00:00 2001 From: Cameron Moore Date: Fri, 29 Nov 2019 13:42:55 -0600 Subject: [PATCH 5/5] Use io.ByteWriter --- parser/attribute.go | 3 ++- parser/html_block.go | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/parser/attribute.go b/parser/attribute.go index 42b1753..f417d2b 100644 --- a/parser/attribute.go +++ b/parser/attribute.go @@ -2,6 +2,7 @@ package parser import ( "bytes" + "io" "strconv" "github.com/yuin/goldmark/text" @@ -227,7 +228,7 @@ func parseAttributeString(reader text.Reader) ([]byte, bool) { return nil, false } -func scanAttributeDecimal(reader text.Reader, w *bytes.Buffer) { +func scanAttributeDecimal(reader text.Reader, w io.ByteWriter) { for { c := reader.Peek() if util.IsNumeric(c) { diff --git a/parser/html_block.go b/parser/html_block.go index c796354..a1f3338 100644 --- a/parser/html_block.go +++ b/parser/html_block.go @@ -134,7 +134,7 @@ func (b *htmlBlockParser) Open(parent ast.Node, reader text.Reader, pc Context) isCloseTag := match[2] > -1 && bytes.Equal(line[match[2]:match[3]], []byte("/")) hasAttr := match[6] != match[7] tagName = strings.ToLower(string(line[match[4]:match[5]])) - _, ok := allowedBlockTags[strings.ToLower(string(tagName))] + _, ok := allowedBlockTags[tagName] if ok { node = ast.NewHTMLBlock(ast.HTMLBlockType6) } else if tagName != "script" && tagName != "style" && tagName != "pre" && !ast.IsParagraph(last) && !(isCloseTag && hasAttr) { // type 7 can not interrupt paragraph