From a9baf845f1de9a949422834449e8eaaaed7480fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytautas=20=C5=A0altenis?= Date: Fri, 29 Jul 2016 08:01:31 +0300 Subject: [PATCH] Unpublish and rename LinkType constants (#285) * Unpublish and rename LinkType constants The constants are only used in the parsing phase, they are not recorded in the AST directly, so make them private. Improve their names along the way. Fix tagLength to return two values instead of taking an output parameter. * autoLinkType -> autolinkType And remove unnecessary comment. --- inline.go | 54 ++++++++++++++++++++++++++++------------------------- latex.go | 4 ++-- markdown.go | 12 ------------ 3 files changed, 31 insertions(+), 39 deletions(-) diff --git a/inline.go b/inline.go index d051ead..d32d75e 100644 --- a/inline.go +++ b/inline.go @@ -638,23 +638,32 @@ func stripMailto(link []byte) []byte { } } +// autolinkType specifies a kind of autolink that gets detected. +type autolinkType int + +// These are the possible flag values for the autolink renderer. +const ( + notAutolink autolinkType = iota + normalAutolink + emailAutolink +) + // '<' when tags or autolinks are allowed func leftAngle(p *parser, data []byte, offset int) int { data = data[offset:] - altype := LinkTypeNotAutolink - end := tagLength(data, &altype) + altype, end := tagLength(data) if size := p.inlineHTMLComment(data); size > 0 { end = size } if end > 2 { - if altype != LinkTypeNotAutolink { + if altype != notAutolink { var uLink bytes.Buffer unescapeText(&uLink, data[1:end+1-2]) if uLink.Len() > 0 { link := uLink.Bytes() node := NewNode(Link) node.Destination = link - if altype == LinkTypeEmail { + if altype == emailAutolink { node.Destination = append([]byte("mailto:"), link...) } p.currBlock.appendChild(node) @@ -924,17 +933,17 @@ func isSafeLink(link []byte) bool { } // return the length of the given tag, or 0 is it's not valid -func tagLength(data []byte, autolink *LinkType) int { +func tagLength(data []byte) (autolink autolinkType, end int) { var i, j int // a valid tag can't be shorter than 3 chars if len(data) < 3 { - return 0 + return notAutolink, 0 } // begins with a '<' optionally followed by '/', followed by letter or number if data[0] != '<' { - return 0 + return notAutolink, 0 } if data[1] == '/' { i = 2 @@ -943,11 +952,11 @@ func tagLength(data []byte, autolink *LinkType) int { } if !isalnum(data[i]) { - return 0 + return notAutolink, 0 } // scheme test - *autolink = LinkTypeNotAutolink + autolink = notAutolink // try to find the beginning of an URI for i < len(data) && (isalnum(data[i]) || data[i] == '.' || data[i] == '+' || data[i] == '-') { @@ -956,21 +965,20 @@ func tagLength(data []byte, autolink *LinkType) int { if i > 1 && i < len(data) && data[i] == '@' { if j = isMailtoAutoLink(data[i:]); j != 0 { - *autolink = LinkTypeEmail - return i + j + return emailAutolink, i + j } } if i > 2 && i < len(data) && data[i] == ':' { - *autolink = LinkTypeNormal + autolink = normalAutolink i++ } // complete autolink test: no whitespace or ' or " switch { case i >= len(data): - *autolink = LinkTypeNotAutolink - case *autolink != 0: + autolink = notAutolink + case autolink != notAutolink: j = i for i < len(data) { @@ -985,24 +993,20 @@ func tagLength(data []byte, autolink *LinkType) int { } if i >= len(data) { - return 0 + return autolink, 0 } if i > j && data[i] == '>' { - return i + 1 + return autolink, i + 1 } // one of the forbidden chars has been found - *autolink = LinkTypeNotAutolink + autolink = notAutolink } - - // look for something looking like a tag end - for i < len(data) && data[i] != '>' { - i++ + i += bytes.IndexByte(data[i:], '>') + if i < 0 { + return autolink, 0 } - if i >= len(data) { - return 0 - } - return i + 1 + return autolink, i + 1 } // look for the address part of a mail autolink and '>' diff --git a/latex.go b/latex.go index 5b341ae..900abf0 100644 --- a/latex.go +++ b/latex.go @@ -177,9 +177,9 @@ func (r *Latex) FootnoteItem(name, text []byte, flags ListType) { } -func (r *Latex) AutoLink(link []byte, kind LinkType) { +func (r *Latex) AutoLink(link []byte, kind autolinkType) { r.w.WriteString("\\href{") - if kind == LinkTypeEmail { + if kind == emailAutolink { r.w.WriteString("mailto:") } r.w.Write(link) diff --git a/markdown.go b/markdown.go index 2efab1e..904dfb6 100644 --- a/markdown.go +++ b/markdown.go @@ -75,18 +75,6 @@ var DefaultOptions = Options{ Extensions: CommonExtensions, } -// TODO: this should probably be unexported. Or moved to node.go -type LinkType int - -// These are the possible flag values for the link renderer. -// Only a single one of these values will be used; they are not ORed together. -// These are mostly of interest if you are writing a new output format. -const ( - LinkTypeNotAutolink LinkType = iota - LinkTypeNormal - LinkTypeEmail -) - // ListType contains bitwise or'ed flags for list and list item objects. type ListType int