Skip to content

Commit

Permalink
internal/godoc/dochtml/internal/render: support unicode quotes
Browse files Browse the repository at this point in the history
convertQuotes is added, which converts quotes to their unicode
equivalent.

This fix was reappropriated from CL 150377.

Fixes golang/go#42666

Change-Id: I2b110ac67d08da66db6f50fb7698e0670af72693
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/274251
TryBot-Result: kokoro <[email protected]>
Trust: Julie Qiu <[email protected]>
Trust: Dmitri Shuralyov <[email protected]>
Run-TryBot: Julie Qiu <[email protected]>
Reviewed-by: Jonathan Amsterdam <[email protected]>
  • Loading branch information
julieqiu committed Feb 12, 2021
1 parent 1763954 commit f2cff40
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
13 changes: 13 additions & 0 deletions internal/godoc/dochtml/internal/render/linkify.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ func (r *Renderer) formatLineHTML(line string, idr *identifierResolver) safehtml
htmls = append(htmls, ExecuteToHTML(LinkTemplate, Link{Href: href, Text: text}))
}

line = convertQuotes(line)
for len(line) > 0 {
m0, m1 := len(line), len(line)
if m := matchRx.FindStringIndex(line); m != nil {
Expand Down Expand Up @@ -693,3 +694,15 @@ func generateAnchorLinks(idr *identifierResolver, decl ast.Decl) map[*ast.Ident]
})
return m
}

const (
ulquo = "“"
urquo = "”"
)

var unicodeQuoteReplacer = strings.NewReplacer("``", ulquo, "''", urquo)

// convertQuotes turns `` into “ and '' into ”.
func convertQuotes(text string) string {
return unicodeQuoteReplacer.Replace(text)
}
20 changes: 20 additions & 0 deletions internal/godoc/dochtml/internal/render/linkify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ TLSUnique contains the tls-unique channel binding value (see RFC
{Text: "title2", Href: "url2"},
},
},
{
name: "escape back ticks in quotes",
doc: "For more detail, run ``go help test'' and ``go help testflag''",
want: `<p>For more detail, run “go help test” and “go help testflag”` + "\n" + "</p>",
},
} {
t.Run(test.name, func(t *testing.T) {
extractLinks := test.extractLinks
Expand Down Expand Up @@ -518,3 +523,18 @@ func TestParseLink(t *testing.T) {
}
}
}

func TestCommentEscape(t *testing.T) {
commentTests := []struct {
in, out string
}{
{"typically invoked as ``go tool asm'',", `typically invoked as “go tool asm”,`},
{"For more detail, run ``go help test'' and ``go help testflag''", `For more detail, run “go help test” and “go help testflag”`},
}
for i, test := range commentTests {
out := convertQuotes(test.in)
if out != test.out {
t.Errorf("#%d: mismatch\nhave: %q\nwant: %q", i, out, test.out)
}
}
}

0 comments on commit f2cff40

Please sign in to comment.