1
0
mirror of https://github.com/golang/go synced 2024-10-02 16:28:34 -06:00

go/doc: Fix URL linking in ToHTML.

Fixes issue: 2832

R=rsc, gri
CC=golang-dev
https://golang.org/cl/5616060
This commit is contained in:
Gary Burd 2012-02-02 17:02:05 -08:00 committed by Robert Griesemer
parent cce3de79f6
commit b9474de2be
2 changed files with 30 additions and 4 deletions

View File

@ -56,7 +56,7 @@ const (
filePart + `([:.,]` + filePart + `)*`
)
var matchRx = regexp.MustCompile(`(` + identRx + `)|(` + urlRx + `)`)
var matchRx = regexp.MustCompile(`(` + urlRx + `)|(` + identRx + `)`)
var (
html_a = []byte(`<a href="`)
@ -87,7 +87,7 @@ func emphasize(w io.Writer, line string, words map[string]string, nice bool) {
if m == nil {
break
}
// m >= 6 (two parenthesized sub-regexps in matchRx, 1st one is identRx)
// m >= 6 (two parenthesized sub-regexps in matchRx, 1st one is urlRx)
// write text before match
commentEscape(w, line[0:m[0]], nice)
@ -99,8 +99,8 @@ func emphasize(w io.Writer, line string, words map[string]string, nice bool) {
if words != nil {
url, italics = words[string(match)]
}
if m[2] < 0 {
// didn't match against first parenthesized sub-regexp; must be match against urlRx
if m[2] >= 0 {
// match against first parenthesized sub-regexp; must be match against urlRx
if !italics {
// no alternative URL in words list, use match instead
url = string(match)

View File

@ -5,6 +5,7 @@
package doc
import (
"bytes"
"reflect"
"testing"
)
@ -81,3 +82,28 @@ func TestBlocks(t *testing.T) {
}
}
}
var emphasizeTests = []struct {
in string
out string
}{
{"http://www.google.com/", `<a href="http://www.google.com/">http://www.google.com/</a>`},
{"https://www.google.com/", `<a href="https://www.google.com/">https://www.google.com/</a>`},
{"http://www.google.com/path.", `<a href="http://www.google.com/path">http://www.google.com/path</a>.`},
{"(http://www.google.com/)", `(<a href="http://www.google.com/">http://www.google.com/</a>)`},
{"Foo bar http://example.com/ quux!", `Foo bar <a href="http://example.com/">http://example.com/</a> quux!`},
{"Hello http://example.com/%2f/ /world.", `Hello <a href="http://example.com/%2f/">http://example.com/%2f/</a> /world.`},
{"Lorem http: ipsum //host/path", "Lorem http: ipsum //host/path"},
{"javascript://is/not/linked", "javascript://is/not/linked"},
}
func TestEmphasize(t *testing.T) {
for i, tt := range emphasizeTests {
var buf bytes.Buffer
emphasize(&buf, tt.in, nil, true)
out := buf.String()
if out != tt.out {
t.Errorf("#%d: mismatch\nhave: %v\nwant: %v", i, out, tt.out)
}
}
}