From 6d2e5fddae38200451d6f7da589cadc4ab4ca58e Mon Sep 17 00:00:00 2001 From: yuin Date: Mon, 16 Dec 2019 11:08:45 +0900 Subject: [PATCH] Fix #64 and some lint warnings --- extension/_test/linkify.txt | 7 +++++++ extension/linkify.go | 2 +- parser/delimiter.go | 6 +++--- parser/html_block.go | 6 ++---- parser/parser.go | 1 + util/util.go | 1 + 6 files changed, 15 insertions(+), 8 deletions(-) diff --git a/extension/_test/linkify.txt b/extension/_test/linkify.txt index 01fd308..eef889d 100644 --- a/extension/_test/linkify.txt +++ b/extension/_test/linkify.txt @@ -130,3 +130,10 @@ https://github.com/sunday's //- - - - - - - - -//

https://github.com/sunday's

//= = = = = = = = = = = = = = = = = = = = = = = =// + +13 +//- - - - - - - - -// +https://github.com?q=stars:>1 +//- - - - - - - - -// +

https://github.com?q=stars:>1

+//= = = = = = = = = = = = = = = = = = = = = = = =// diff --git a/extension/linkify.go b/extension/linkify.go index b7bd9db..454abf8 100644 --- a/extension/linkify.go +++ b/extension/linkify.go @@ -12,7 +12,7 @@ import ( var wwwURLRegxp = regexp.MustCompile(`^www\.[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b(?:[-a-zA-Z0-9@:%_\+.~#?&//=\(\);,'"]*)`) -var urlRegexp = regexp.MustCompile(`^(?:http|https|ftp):\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=\(\);,'"]*)`) +var urlRegexp = regexp.MustCompile(`^(?:http|https|ftp):\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+\.~#$!?&//=\(\);,'"<>\^{}\[\]` + "`" + `]*)`) type linkifyParser struct { } diff --git a/parser/delimiter.go b/parser/delimiter.go index 888e21a..0cdaab3 100644 --- a/parser/delimiter.go +++ b/parser/delimiter.go @@ -127,15 +127,15 @@ func ScanDelimiter(line []byte, before rune, min int, processor DelimiterProcess after = util.ToRune(line, j) } - isLeft, isRight, canOpen, canClose := false, false, false, false + canOpen, canClose := false, false beforeIsPunctuation := unicode.IsPunct(before) beforeIsWhitespace := unicode.IsSpace(before) afterIsPunctuation := unicode.IsPunct(after) afterIsWhitespace := unicode.IsSpace(after) - isLeft = !afterIsWhitespace && + isLeft := !afterIsWhitespace && (!afterIsPunctuation || beforeIsWhitespace || beforeIsPunctuation) - isRight = !beforeIsWhitespace && + isRight := !beforeIsWhitespace && (!beforeIsPunctuation || afterIsWhitespace || afterIsPunctuation) if line[i] == '_' { diff --git a/parser/html_block.go b/parser/html_block.go index a1f3338..2f04aa5 100644 --- a/parser/html_block.go +++ b/parser/html_block.go @@ -118,9 +118,7 @@ func (b *htmlBlockParser) Open(parent ast.Node, reader text.Reader, pc Context) return nil, NoChildren } - tagName := "" if m := htmlBlockType1OpenRegexp.FindSubmatchIndex(line); m != nil { - tagName = string(line[m[2]:m[3]]) node = ast.NewHTMLBlock(ast.HTMLBlockType1) } else if htmlBlockType2OpenRegexp.Match(line) { node = ast.NewHTMLBlock(ast.HTMLBlockType2) @@ -133,7 +131,7 @@ func (b *htmlBlockParser) Open(parent ast.Node, reader text.Reader, pc Context) } else if match := htmlBlockType7Regexp.FindSubmatchIndex(line); match != nil { 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]])) + tagName := strings.ToLower(string(line[match[4]:match[5]])) _, ok := allowedBlockTags[tagName] if ok { node = ast.NewHTMLBlock(ast.HTMLBlockType6) @@ -143,7 +141,7 @@ func (b *htmlBlockParser) Open(parent ast.Node, reader text.Reader, pc Context) } if node == nil { if match := htmlBlockType6Regexp.FindSubmatchIndex(line); match != nil { - tagName = string(line[match[2]:match[3]]) + tagName := string(line[match[2]:match[3]]) _, ok := allowedBlockTags[strings.ToLower(tagName)] if ok { node = ast.NewHTMLBlock(ast.HTMLBlockType6) diff --git a/parser/parser.go b/parser/parser.go index 2a5538a..a154454 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -206,6 +206,7 @@ type ContextConfig struct { // An ContextOption is a functional option type for the Context. type ContextOption func(*ContextConfig) +// WithIDs is a functional option for the Context. func WithIDs(ids IDs) ContextOption { return func(c *ContextConfig) { c.IDs = ids diff --git a/util/util.go b/util/util.go index 479a9fa..dd3a7a0 100644 --- a/util/util.go +++ b/util/util.go @@ -826,6 +826,7 @@ type bytesFilter struct { slots [][][]byte } +// NewBytesFilter returns a new BytesFilter. func NewBytesFilter(elements ...[]byte) BytesFilter { s := &bytesFilter{ threshold: 3,