@@ -15,18 +15,6 @@ import (
15
15
"unicode/utf8"
16
16
)
17
17
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
-
30
18
const (
31
19
// Regexp for Go identifiers
32
20
identRx = `[a-zA-Z_][a-zA-Z_0-9]*` // TODO(gri) ASCII only for now - fix this
@@ -43,35 +31,17 @@ const (
43
31
var matchRx = regexp .MustCompile (`(` + urlRx + `)|(` + identRx + `)` )
44
32
45
33
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 ("### " )
64
41
)
65
42
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 “
73
- // and '' into ”).
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 ) {
75
45
for {
76
46
m := matchRx .FindStringSubmatchIndex (line )
77
47
if m == nil {
@@ -80,45 +50,29 @@ func emphasize(w io.Writer, line string, words map[string]string, nice bool) {
80
50
// m >= 6 (two parenthesized sub-regexps in matchRx, 1st one is urlRx)
81
51
82
52
// write text before match
83
- commentEscape ( w , line [0 :m [0 ]], nice )
53
+ _ , _ = w . Write ([] byte ( line [0 :m [0 ]]) )
84
54
85
55
// analyze match
86
56
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
92
59
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 )
99
63
}
100
64
101
65
// 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 )
116
70
}
117
71
118
72
// advance
119
73
line = line [m [1 ]:]
120
74
}
121
- commentEscape ( w , line , nice )
75
+ _ , _ = w . Write ([] byte ( line ) )
122
76
}
123
77
124
78
func indentLen (s string ) int {
@@ -184,7 +138,7 @@ func heading(line string) string {
184
138
}
185
139
186
140
// exclude lines with illegal characters
187
- if strings .IndexAny (line , ",.;:!?+*/=()[]{}_^°&§~%#@<\" >\\ " ) >= 0 {
141
+ if strings .ContainsAny (line , ",.;:!?+*/=()[]{}_^°&§~%#@<\" >\\ " ) {
188
142
return ""
189
143
}
190
144
@@ -238,38 +192,32 @@ func anchorID(line string) string {
238
192
// A span of indented lines is converted into a <pre> block,
239
193
// with the common indent prefix removed.
240
194
//
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 ) {
249
197
for _ , b := range blocks (text ) {
250
198
switch b .op {
251
199
case opPara :
252
200
for _ , line := range b .lines {
253
- emphasize (w , line , words , true )
201
+ emphasize (w , line )
254
202
}
255
- w .Write (md_newline ) // trailing newline to emulate </p>
203
+ _ , _ = w .Write (mdNewline ) // trailing newline to emulate </p>
256
204
case opHead :
257
- w .Write (md_h3 )
205
+ _ , _ = w .Write (mdH3 )
258
206
id := ""
259
207
for _ , line := range b .lines {
260
208
if id == "" {
261
209
id = anchorID (line )
262
210
}
263
- commentEscape ( w , line , true )
211
+ _ , _ = w . Write ([] byte ( line ) )
264
212
}
265
- w .Write (md_newline )
213
+ _ , _ = w .Write (mdNewline )
266
214
case opPre :
267
- w .Write (md_newline )
215
+ _ , _ = w .Write (mdNewline )
268
216
for _ , line := range b .lines {
269
- w .Write (md_pre )
270
- emphasize (w , line , nil , false )
217
+ _ , _ = w .Write (mdPre )
218
+ emphasize (w , line )
271
219
}
272
- w .Write (md_newline )
220
+ _ , _ = w .Write (mdNewline )
273
221
}
274
222
}
275
223
}
0 commit comments