all repos — grayfriday @ 10f1dc6358d4803229f5598839c0882148081f8b

blackfriday fork with a few changes

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("&rsquo;")
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("&rsquo;")
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("&copy;")
159			return 2
160		}
161
162		if t1 == 'r' && t2 == ')' {
163			out.WriteString("&reg;")
164			return 2
165		}
166
167		if len(text) >= 4 && t1 == 't' && t2 == 'm' && text[3] == ')' {
168			out.WriteString("&trade;")
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("&mdash;")
181			return 1
182		}
183
184		if wordBoundary(previousChar) && wordBoundary(text[1]) {
185			out.WriteString("&ndash;")
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("&mdash;")
197		return 2
198	}
199	if len(text) >= 2 && text[1] == '-' {
200		out.WriteString("&ndash;")
201		return 1
202	}
203
204	out.WriteByte(text[0])
205	return 0
206}
207
208func smartAmp(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
209	if bytes.HasPrefix(text, []byte("&quot;")) {
210		nextChar := byte(0)
211		if len(text) >= 7 {
212			nextChar = text[6]
213		}
214		if smartQuoteHelper(out, previousChar, nextChar, 'd', &smrt.inDoubleQuote) {
215			return 5
216		}
217	}
218
219	if bytes.HasPrefix(text, []byte("&#0;")) {
220		return 3
221	}
222
223	out.WriteByte('&')
224	return 0
225}
226
227func smartPeriod(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
228	if len(text) >= 3 && text[1] == '.' && text[2] == '.' {
229		out.WriteString("&hellip;")
230		return 2
231	}
232
233	if len(text) >= 5 && text[1] == ' ' && text[2] == '.' && text[3] == ' ' && text[4] == '.' {
234		out.WriteString("&hellip;")
235		return 4
236	}
237
238	out.WriteByte(text[0])
239	return 0
240}
241
242func smartBacktick(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
243	if len(text) >= 2 && text[1] == '`' {
244		nextChar := byte(0)
245		if len(text) >= 3 {
246			nextChar = text[2]
247		}
248		if smartQuoteHelper(out, previousChar, nextChar, 'd', &smrt.inDoubleQuote) {
249			return 1
250		}
251	}
252
253	out.WriteByte(text[0])
254	return 0
255}
256
257func smartNumberGeneric(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
258	if wordBoundary(previousChar) && len(text) >= 3 {
259		// is it of the form digits/digits(word boundary)?, i.e., \d+/\d+\b
260		// note: check for regular slash (/) or fraction slash (⁄, 0x2044, or 0xe2 81 84 in utf-8)
261		numEnd := 0
262		for len(text) > numEnd && isdigit(text[numEnd]) {
263			numEnd++
264		}
265		if numEnd == 0 {
266			out.WriteByte(text[0])
267			return 0
268		}
269		denStart := numEnd + 1
270		if len(text) > numEnd+3 && text[numEnd] == 0xe2 && text[numEnd+1] == 0x81 && text[numEnd+2] == 0x84 {
271			denStart = numEnd + 3
272		} else if len(text) < numEnd+2 || text[numEnd] != '/' {
273			out.WriteByte(text[0])
274			return 0
275		}
276		denEnd := denStart
277		for len(text) > denEnd && isdigit(text[denEnd]) {
278			denEnd++
279		}
280		if denEnd == denStart {
281			out.WriteByte(text[0])
282			return 0
283		}
284		if len(text) == denEnd || wordBoundary(text[denEnd]) {
285			out.WriteString("<sup>")
286			out.Write(text[:numEnd])
287			out.WriteString("</sup>&frasl;<sub>")
288			out.Write(text[denStart:denEnd])
289			out.WriteString("</sub>")
290			return denEnd - 1
291		}
292	}
293
294	out.WriteByte(text[0])
295	return 0
296}
297
298func smartNumber(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
299	if wordBoundary(previousChar) && len(text) >= 3 {
300		if text[0] == '1' && text[1] == '/' && text[2] == '2' {
301			if len(text) < 4 || wordBoundary(text[3]) {
302				out.WriteString("&frac12;")
303				return 2
304			}
305		}
306
307		if text[0] == '1' && text[1] == '/' && text[2] == '4' {
308			if len(text) < 4 || wordBoundary(text[3]) || (len(text) >= 5 && tolower(text[3]) == 't' && tolower(text[4]) == 'h') {
309				out.WriteString("&frac14;")
310				return 2
311			}
312		}
313
314		if text[0] == '3' && text[1] == '/' && text[2] == '4' {
315			if len(text) < 4 || wordBoundary(text[3]) || (len(text) >= 6 && tolower(text[3]) == 't' && tolower(text[4]) == 'h' && tolower(text[5]) == 's') {
316				out.WriteString("&frac34;")
317				return 2
318			}
319		}
320	}
321
322	out.WriteByte(text[0])
323	return 0
324}
325
326func smartDoubleQuote(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
327	nextChar := byte(0)
328	if len(text) > 1 {
329		nextChar = text[1]
330	}
331	if !smartQuoteHelper(out, previousChar, nextChar, 'd', &smrt.inDoubleQuote) {
332		out.WriteString("&quot;")
333	}
334
335	return 0
336}
337
338func smartLeftAngle(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
339	i := 0
340
341	for i < len(text) && text[i] != '>' {
342		i++
343	}
344
345	out.Write(text[:i+1])
346	return i
347}
348
349type smartCallback func(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int
350
351type smartypantsRenderer [256]smartCallback
352
353func smartypants(flags int) *smartypantsRenderer {
354	r := new(smartypantsRenderer)
355	r['"'] = smartDoubleQuote
356	r['&'] = smartAmp
357	r['\''] = smartSingleQuote
358	r['('] = smartParens
359	if flags&HTML_SMARTYPANTS_LATEX_DASHES == 0 {
360		r['-'] = smartDash
361	} else {
362		r['-'] = smartDashLatex
363	}
364	r['.'] = smartPeriod
365	if flags&HTML_SMARTYPANTS_FRACTIONS == 0 {
366		r['1'] = smartNumber
367		r['3'] = smartNumber
368	} else {
369		for ch := '1'; ch <= '9'; ch++ {
370			r[ch] = smartNumberGeneric
371		}
372	}
373	r['<'] = smartLeftAngle
374	r['`'] = smartBacktick
375	return r
376}