Skip to content
This repository was archived by the owner on Jul 29, 2021. It is now read-only.

Commit 543cd3c

Browse files
Task: Fix linting issues
Remove unused variables, constants, and parameters. Explicitly ignore return values when calling `Write`. Regenerate examples to verify the only changes are due to updates of the specific packages.
1 parent e7bb543 commit 543cd3c

File tree

6 files changed

+117
-127
lines changed

6 files changed

+117
-127
lines changed

comment.go

Lines changed: 32 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,6 @@ import (
1515
"unicode/utf8"
1616
)
1717

18-
var (
19-
ldquo = []byte("“")
20-
rdquo = []byte("”")
21-
)
22-
23-
// Escape comment text for HTML. If nice is set,
24-
// also turn `` into “ and '' into ”.
25-
func commentEscape(w io.Writer, text string, nice bool) {
26-
w.Write([]byte(text))
27-
return
28-
}
29-
3018
const (
3119
// Regexp for Go identifiers
3220
identRx = `[a-zA-Z_][a-zA-Z_0-9]*` // TODO(gri) ASCII only for now - fix this
@@ -43,35 +31,17 @@ const (
4331
var matchRx = regexp.MustCompile(`(` + urlRx + `)|(` + identRx + `)`)
4432

4533
var (
46-
html_a = []byte(`<a href="`)
47-
html_aq = []byte(`">`)
48-
html_enda = []byte("</a>")
49-
html_i = []byte("<i>")
50-
html_endi = []byte("</i>")
51-
html_p = []byte("\n")
52-
html_endp = []byte("\n")
53-
html_pre = []byte("<pre>")
54-
html_endpre = []byte("</pre>\n")
55-
html_h = []byte(`<h3 id="`)
56-
html_hq = []byte(`">`)
57-
html_endh = []byte("</h3>\n")
58-
59-
md_pre = []byte("\t")
60-
md_newline = []byte("\n")
61-
md_h1 = []byte("# ")
62-
md_h2 = []byte("## ")
63-
md_h3 = []byte("### ")
34+
htmlA = []byte(`<a href="`)
35+
htmlAq = []byte(`">`)
36+
htmlEnda = []byte("</a>")
37+
38+
mdPre = []byte("\t")
39+
mdNewline = []byte("\n")
40+
mdH3 = []byte("### ")
6441
)
6542

66-
// Emphasize and escape a line of text for HTML. URLs are converted into links;
67-
// if the URL also appears in the words map, the link is taken from the map (if
68-
// the corresponding map value is the empty string, the URL is not converted
69-
// into a link). Go identifiers that appear in the words map are italicized; if
70-
// the corresponding map value is not the empty string, it is considered a URL
71-
// and the word is converted into a link. If nice is set, the remaining text's
72-
// appearance is improved where it makes sense (e.g., `` is turned into &ldquo;
73-
// and '' into &rdquo;).
74-
func emphasize(w io.Writer, line string, words map[string]string, nice bool) {
43+
// Emphasize and escape a line of text for HTML. URLs are converted into links.
44+
func emphasize(w io.Writer, line string) {
7545
for {
7646
m := matchRx.FindStringSubmatchIndex(line)
7747
if m == nil {
@@ -80,45 +50,29 @@ func emphasize(w io.Writer, line string, words map[string]string, nice bool) {
8050
// m >= 6 (two parenthesized sub-regexps in matchRx, 1st one is urlRx)
8151

8252
// write text before match
83-
commentEscape(w, line[0:m[0]], nice)
53+
_, _ = w.Write([]byte(line[0:m[0]]))
8454

8555
// analyze match
8656
match := line[m[0]:m[1]]
87-
url := ""
88-
italics := false
89-
if words != nil {
90-
url, italics = words[string(match)]
91-
}
57+
58+
// if URL then write as link
9259
if m[2] >= 0 {
93-
// match against first parenthesized sub-regexp; must be match against urlRx
94-
if !italics {
95-
// no alternative URL in words list, use match instead
96-
url = string(match)
97-
}
98-
italics = false // don't italicize URLs
60+
_, _ = w.Write(htmlA)
61+
template.HTMLEscape(w, []byte(match))
62+
_, _ = w.Write(htmlAq)
9963
}
10064

10165
// write match
102-
if len(url) > 0 {
103-
w.Write(html_a)
104-
template.HTMLEscape(w, []byte(url))
105-
w.Write(html_aq)
106-
}
107-
if italics {
108-
w.Write(html_i)
109-
}
110-
commentEscape(w, match, nice)
111-
if italics {
112-
w.Write(html_endi)
113-
}
114-
if len(url) > 0 {
115-
w.Write(html_enda)
66+
_, _ = w.Write([]byte(match))
67+
68+
if m[2] >= 0 {
69+
_, _ = w.Write(htmlEnda)
11670
}
11771

11872
// advance
11973
line = line[m[1]:]
12074
}
121-
commentEscape(w, line, nice)
75+
_, _ = w.Write([]byte(line))
12276
}
12377

12478
func indentLen(s string) int {
@@ -184,7 +138,7 @@ func heading(line string) string {
184138
}
185139

186140
// exclude lines with illegal characters
187-
if strings.IndexAny(line, ",.;:!?+*/=()[]{}_^°&§~%#@<\">\\") >= 0 {
141+
if strings.ContainsAny(line, ",.;:!?+*/=()[]{}_^°&§~%#@<\">\\") {
188142
return ""
189143
}
190144

@@ -238,38 +192,32 @@ func anchorID(line string) string {
238192
// A span of indented lines is converted into a <pre> block,
239193
// with the common indent prefix removed.
240194
//
241-
// URLs in the comment text are converted into links; if the URL also appears
242-
// in the words map, the link is taken from the map (if the corresponding map
243-
// value is the empty string, the URL is not converted into a link).
244-
//
245-
// Go identifiers that appear in the words map are italicized; if the corresponding
246-
// map value is not the empty string, it is considered a URL and the word is converted
247-
// into a link.
248-
func ToMD(w io.Writer, text string, words map[string]string) {
195+
// URLs in the comment text are converted into links.
196+
func ToMD(w io.Writer, text string) {
249197
for _, b := range blocks(text) {
250198
switch b.op {
251199
case opPara:
252200
for _, line := range b.lines {
253-
emphasize(w, line, words, true)
201+
emphasize(w, line)
254202
}
255-
w.Write(md_newline) // trailing newline to emulate </p>
203+
_, _ = w.Write(mdNewline) // trailing newline to emulate </p>
256204
case opHead:
257-
w.Write(md_h3)
205+
_, _ = w.Write(mdH3)
258206
id := ""
259207
for _, line := range b.lines {
260208
if id == "" {
261209
id = anchorID(line)
262210
}
263-
commentEscape(w, line, true)
211+
_, _ = w.Write([]byte(line))
264212
}
265-
w.Write(md_newline)
213+
_, _ = w.Write(mdNewline)
266214
case opPre:
267-
w.Write(md_newline)
215+
_, _ = w.Write(mdNewline)
268216
for _, line := range b.lines {
269-
w.Write(md_pre)
270-
emphasize(w, line, nil, false)
217+
_, _ = w.Write(mdPre)
218+
emphasize(w, line)
271219
}
272-
w.Write(md_newline)
220+
_, _ = w.Write(mdNewline)
273221
}
274222
}
275223
}

0 commit comments

Comments
 (0)