Skip to content

Commit

Permalink
internal/godoc/dochtml/internal/render: don't smart quote in <pre> bl…
Browse files Browse the repository at this point in the history
…ocks

Fixes golang/go#51807.

Change-Id: I8ab6c9362ef79a286c0480d14a4905f8fe4c0c84
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/394034
Trust: Jamal Carvalho <[email protected]>
Reviewed-by: Jamal Carvalho <[email protected]>
Reviewed-by: Jamal Carvalho <[email protected]>
Reviewed-by: Jonathan Amsterdam <[email protected]>
  • Loading branch information
kortschak authored and jba committed Apr 7, 2022
1 parent 81a1781 commit a5c2c4e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
17 changes: 10 additions & 7 deletions internal/godoc/dochtml/internal/render/linkify.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,15 @@ func (r *Renderer) formatDocHTML(doc string, extractLinks bool) safehtml.HTML {
if inLinks {
r.links = append(r.links, parseLinks(blk.lines)...)
} else {
el.Body = r.linesToHTML(blk.lines)
el.Body = r.linesToHTML(blk.lines, false)
els = append(els, el)
}
case *preformat:
if inLinks {
r.links = append(r.links, parseLinks(blk.lines)...)
} else {
el.IsPreformat = true
el.Body = r.linesToHTML(blk.lines)
el.Body = r.linesToHTML(blk.lines, true)
els = append(els, el)
}
case *heading:
Expand Down Expand Up @@ -159,11 +159,11 @@ func parseLink(line string) *Link {
}
}

func (r *Renderer) linesToHTML(lines []string) safehtml.HTML {
func (r *Renderer) linesToHTML(lines []string, pre bool) safehtml.HTML {
newline := safehtml.HTMLEscaped("\n")
htmls := make([]safehtml.HTML, 0, 2*len(lines))
for _, l := range lines {
htmls = append(htmls, r.formatLineHTML(l))
htmls = append(htmls, r.formatLineHTML(l, pre))
htmls = append(htmls, newline)
}
return safehtml.HTMLConcat(htmls...)
Expand Down Expand Up @@ -264,15 +264,18 @@ scan:

// formatLineHTML formats the line as HTML-annotated text.
// URLs and Go identifiers are linked to corresponding declarations.
func (r *Renderer) formatLineHTML(line string) safehtml.HTML {
// If pre is true no conversion of `` or '' to “ and ” is performed.
func (r *Renderer) formatLineHTML(line string, pre bool) safehtml.HTML {
var htmls []safehtml.HTML
var numQuotes int

addLink := func(href, text string) {
htmls = append(htmls, ExecuteToHTML(LinkTemplate, Link{Href: href, Text: text}))
}

line = convertQuotes(line)
if !pre {
line = convertQuotes(line)
}
for len(line) > 0 {
m0, m1 := len(line), len(line)
if m := matchRx.FindStringIndex(line); m != nil {
Expand Down Expand Up @@ -425,7 +428,7 @@ scan:
tokType = commentType
htmlLines[line] = append(htmlLines[line],
template.MustParseAndExecuteToHTML(`<span class="comment">`),
r.formatLineHTML(lit),
r.formatLineHTML(lit, false),
template.MustParseAndExecuteToHTML(`</span>`))
lastOffset += len(lit)
case token.IDENT:
Expand Down
6 changes: 6 additions & 0 deletions internal/godoc/dochtml/internal/render/linkify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ TLSUnique contains the tls-unique channel binding value (see RFC
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>",
},
{
name: "single quotes in pre block",
doc: `Join
[].join() // returns ''`,
want: `<p>Join` + "\n" + `</p><pre>[].join() // returns &#39;&#39;` + "\n" + `</pre>`,
},
} {
t.Run(test.name, func(t *testing.T) {
extractLinks := test.extractLinks
Expand Down

0 comments on commit a5c2c4e

Please sign in to comment.