smartypants.go (view raw)
1//
2// Blackfriday Markdown Processor
3// Available at http://github.com/russross/blackfriday
4//
5// Copyright © 2011 Russ Ross <russ@russross.com>.
6// Distributed under the Simplified BSD License.
7// See README.md for details.
8//
9
10//
11//
12// SmartyPants rendering
13//
14//
15
16package blackfriday
17
18import (
19 "bytes"
20)
21
22type smartypantsData struct {
23 inSingleQuote bool
24 inDoubleQuote bool
25}
26
27func wordBoundary(c byte) bool {
28 return c == 0 || isspace(c) || ispunct(c)
29}
30
31func tolower(c byte) byte {
32 if c >= 'A' && c <= 'Z' {
33 return c - 'A' + 'a'
34 }
35 return c
36}
37
38func isdigit(c byte) bool {
39 return c >= '0' && c <= '9'
40}
41
42func smartQuoteHelper(out *bytes.Buffer, previousChar byte, nextChar byte, quote byte, isOpen *bool) bool {
43 // edge of the buffer is likely to be a tag that we don't get to see,
44 // so we treat it like text sometimes
45
46 // enumerate all sixteen possibilities for (previousChar, nextChar)
47 // each can be one of {0, space, punct, other}
48 switch {
49 case previousChar == 0 && nextChar == 0:
50 // context is not any help here, so toggle
51 *isOpen = !*isOpen
52 case isspace(previousChar) && nextChar == 0:
53 // [ "] might be [ "<code>foo...]
54 *isOpen = true
55 case ispunct(previousChar) && nextChar == 0:
56 // [!"] hmm... could be [Run!"] or [("<code>...]
57 *isOpen = false
58 case /* isnormal(previousChar) && */ nextChar == 0:
59 // [a"] is probably a close
60 *isOpen = false
61 case previousChar == 0 && isspace(nextChar):
62 // [" ] might be [...foo</code>" ]
63 *isOpen = false
64 case isspace(previousChar) && isspace(nextChar):
65 // [ " ] context is not any help here, so toggle
66 *isOpen = !*isOpen
67 case ispunct(previousChar) && isspace(nextChar):
68 // [!" ] is probably a close
69 *isOpen = false
70 case /* isnormal(previousChar) && */ isspace(nextChar):
71 // [a" ] this is one of the easy cases
72 *isOpen = false
73 case previousChar == 0 && ispunct(nextChar):
74 // ["!] hmm... could be ["$1.95] or [</code>"!...]
75 *isOpen = false
76 case isspace(previousChar) && ispunct(nextChar):
77 // [ "!] looks more like [ "$1.95]
78 *isOpen = true
79 case ispunct(previousChar) && ispunct(nextChar):
80 // [!"!] context is not any help here, so toggle
81 *isOpen = !*isOpen
82 case /* isnormal(previousChar) && */ ispunct(nextChar):
83 // [a"!] is probably a close
84 *isOpen = false
85 case previousChar == 0 /* && isnormal(nextChar) */ :
86 // ["a] is probably an open
87 *isOpen = true
88 case isspace(previousChar) /* && isnormal(nextChar) */ :
89 // [ "a] this is one of the easy cases
90 *isOpen = true
91 case ispunct(previousChar) /* && isnormal(nextChar) */ :
92 // [!"a] is probably an open
93 *isOpen = true
94 default:
95 // [a'b] maybe a contraction?
96 *isOpen = false
97 }
98
99 out.WriteByte('&')
100 if *isOpen {
101 out.WriteByte('l')
102 } else {
103 out.WriteByte('r')
104 }
105 out.WriteByte(quote)
106 out.WriteString("quo;")
107 return true
108}
109
110func smartSingleQuote(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
111 if len(text) >= 2 {
112 t1 := tolower(text[1])
113
114 if t1 == '\'' {
115 nextChar := byte(0)
116 if len(text) >= 3 {
117 nextChar = text[2]
118 }
119 if smartQuoteHelper(out, previousChar, nextChar, 'd', &smrt.inDoubleQuote) {
120 return 1
121 }
122 }
123
124 if (t1 == 's' || t1 == 't' || t1 == 'm' || t1 == 'd') && (len(text) < 3 || wordBoundary(text[2])) {
125 out.WriteString("’")
126 return 0
127 }
128
129 if len(text) >= 3 {
130 t2 := tolower(text[2])
131
132 if ((t1 == 'r' && t2 == 'e') || (t1 == 'l' && t2 == 'l') || (t1 == 'v' && t2 == 'e')) &&
133 (len(text) < 4 || wordBoundary(text[3])) {
134 out.WriteString("’")
135 return 0
136 }
137 }
138 }
139
140 nextChar := byte(0)
141 if len(text) > 1 {
142 nextChar = text[1]
143 }
144 if smartQuoteHelper(out, previousChar, nextChar, 's', &smrt.inSingleQuote) {
145 return 0
146 }
147
148 out.WriteByte(text[0])
149 return 0
150}
151
152func smartParens(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
153 if len(text) >= 3 {
154 t1 := tolower(text[1])
155 t2 := tolower(text[2])
156
157 if t1 == 'c' && t2 == ')' {
158 out.WriteString("©")
159 return 2
160 }
161
162 if t1 == 'r' && t2 == ')' {
163 out.WriteString("®")
164 return 2
165 }
166
167 if len(text) >= 4 && t1 == 't' && t2 == 'm' && text[3] == ')' {
168 out.WriteString("™")
169 return 3
170 }
171 }
172
173 out.WriteByte(text[0])
174 return 0
175}
176
177func smartDash(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
178 if len(text) >= 2 {
179 if text[1] == '-' {
180 out.WriteString("—")
181 return 1
182 }
183
184 if wordBoundary(previousChar) && wordBoundary(text[1]) {
185 out.WriteString("–")
186 return 0
187 }
188 }
189
190 out.WriteByte(text[0])
191 return 0
192}
193
194func smartDashLatex(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
195 if len(text) >= 3 && text[1] == '-' && text[2] == '-' {
196 out.WriteString("—")
197 return 2
198 }
199 if len(text) >= 2 && text[1] == '-' {
200 out.WriteString("–")
201 return 1
202 }
203
204 out.WriteByte(text[0])
205 return 0
206}
207
208func smartAmpVariant(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte, quote byte) int {
209 if bytes.HasPrefix(text, []byte(""")) {
210 nextChar := byte(0)
211 if len(text) >= 7 {
212 nextChar = text[6]
213 }
214 if smartQuoteHelper(out, previousChar, nextChar, quote, &smrt.inDoubleQuote) {
215 return 5
216 }
217 }
218
219 if bytes.HasPrefix(text, []byte("�")) {
220 return 3
221 }
222
223 out.WriteByte('&')
224 return 0
225}
226
227func smartAmp(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
228 return smartAmpVariant(out, smrt, previousChar, text, 'd')
229}
230
231func smartAmpAngledQuote(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
232 return smartAmpVariant(out, smrt, previousChar, text, 'a')
233}
234
235func smartPeriod(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
236 if len(text) >= 3 && text[1] == '.' && text[2] == '.' {
237 out.WriteString("…")
238 return 2
239 }
240
241 if len(text) >= 5 && text[1] == ' ' && text[2] == '.' && text[3] == ' ' && text[4] == '.' {
242 out.WriteString("…")
243 return 4
244 }
245
246 out.WriteByte(text[0])
247 return 0
248}
249
250func smartBacktick(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
251 if len(text) >= 2 && text[1] == '`' {
252 nextChar := byte(0)
253 if len(text) >= 3 {
254 nextChar = text[2]
255 }
256 if smartQuoteHelper(out, previousChar, nextChar, 'd', &smrt.inDoubleQuote) {
257 return 1
258 }
259 }
260
261 out.WriteByte(text[0])
262 return 0
263}
264
265func smartNumberGeneric(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
266 if wordBoundary(previousChar) && len(text) >= 3 {
267 // is it of the form digits/digits(word boundary)?, i.e., \d+/\d+\b
268 // note: check for regular slash (/) or fraction slash (⁄, 0x2044, or 0xe2 81 84 in utf-8)
269 numEnd := 0
270 for len(text) > numEnd && isdigit(text[numEnd]) {
271 numEnd++
272 }
273 if numEnd == 0 {
274 out.WriteByte(text[0])
275 return 0
276 }
277 denStart := numEnd + 1
278 if len(text) > numEnd+3 && text[numEnd] == 0xe2 && text[numEnd+1] == 0x81 && text[numEnd+2] == 0x84 {
279 denStart = numEnd + 3
280 } else if len(text) < numEnd+2 || text[numEnd] != '/' {
281 out.WriteByte(text[0])
282 return 0
283 }
284 denEnd := denStart
285 for len(text) > denEnd && isdigit(text[denEnd]) {
286 denEnd++
287 }
288 if denEnd == denStart {
289 out.WriteByte(text[0])
290 return 0
291 }
292 if len(text) == denEnd || wordBoundary(text[denEnd]) {
293 out.WriteString("<sup>")
294 out.Write(text[:numEnd])
295 out.WriteString("</sup>⁄<sub>")
296 out.Write(text[denStart:denEnd])
297 out.WriteString("</sub>")
298 return denEnd - 1
299 }
300 }
301
302 out.WriteByte(text[0])
303 return 0
304}
305
306func smartNumber(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
307 if wordBoundary(previousChar) && len(text) >= 3 {
308 if text[0] == '1' && text[1] == '/' && text[2] == '2' {
309 if len(text) < 4 || wordBoundary(text[3]) {
310 out.WriteString("½")
311 return 2
312 }
313 }
314
315 if text[0] == '1' && text[1] == '/' && text[2] == '4' {
316 if len(text) < 4 || wordBoundary(text[3]) || (len(text) >= 5 && tolower(text[3]) == 't' && tolower(text[4]) == 'h') {
317 out.WriteString("¼")
318 return 2
319 }
320 }
321
322 if text[0] == '3' && text[1] == '/' && text[2] == '4' {
323 if len(text) < 4 || wordBoundary(text[3]) || (len(text) >= 6 && tolower(text[3]) == 't' && tolower(text[4]) == 'h' && tolower(text[5]) == 's') {
324 out.WriteString("¾")
325 return 2
326 }
327 }
328 }
329
330 out.WriteByte(text[0])
331 return 0
332}
333
334func smartDoubleQuoteVariant(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte, quote byte) int {
335 nextChar := byte(0)
336 if len(text) > 1 {
337 nextChar = text[1]
338 }
339 if !smartQuoteHelper(out, previousChar, nextChar, quote, &smrt.inDoubleQuote) {
340 out.WriteString(""")
341 }
342
343 return 0
344}
345
346func smartDoubleQuote(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
347 return smartDoubleQuoteVariant(out, smrt, previousChar, text, 'd')
348}
349
350func smartAngledDoubleQuote(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
351 return smartDoubleQuoteVariant(out, smrt, previousChar, text, 'a')
352}
353
354func smartLeftAngle(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
355 i := 0
356
357 for i < len(text) && text[i] != '>' {
358 i++
359 }
360
361 out.Write(text[:i+1])
362 return i
363}
364
365type smartCallback func(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int
366
367type smartypantsRenderer [256]smartCallback
368
369func smartypants(flags int) *smartypantsRenderer {
370 r := new(smartypantsRenderer)
371 if flags&HTML_SMARTYPANTS_ANGLED_QUOTES == 0 {
372 r['"'] = smartDoubleQuote
373 r['&'] = smartAmp
374 } else {
375 r['"'] = smartAngledDoubleQuote
376 r['&'] = smartAmpAngledQuote
377 }
378 r['\''] = smartSingleQuote
379 r['('] = smartParens
380 if flags&HTML_SMARTYPANTS_LATEX_DASHES == 0 {
381 r['-'] = smartDash
382 } else {
383 r['-'] = smartDashLatex
384 }
385 r['.'] = smartPeriod
386 if flags&HTML_SMARTYPANTS_FRACTIONS == 0 {
387 r['1'] = smartNumber
388 r['3'] = smartNumber
389 } else {
390 for ch := '1'; ch <= '9'; ch++ {
391 r[ch] = smartNumberGeneric
392 }
393 }
394 r['<'] = smartLeftAngle
395 r['`'] = smartBacktick
396 return r
397}