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.
Vytautas Ĺ altenis vytas@rtfb.lt
Fri, 29 Jul 2016 08:01:31 +0300
3 files changed,
31 insertions(+),
39 deletions(-)
M
inline.go
→
inline.go
@@ -638,23 +638,32 @@ return link
} } +// 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 @@ return false
} // 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 @@ i = 1
} 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 @@ }
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 @@
} 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 - } - - // look for something looking like a tag end - for i < len(data) && data[i] != '>' { - i++ + autolink = notAutolink } - if i >= len(data) { - return 0 + i += bytes.IndexByte(data[i:], '>') + if i < 0 { + return autolink, 0 } - return i + 1 + return autolink, i + 1 } // look for the address part of a mail autolink and '>'
M
latex.go
→
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)
M
markdown.go
→
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