all repos — grayfriday @ 2336fd31093e8d1d9cab8642e0c08a82520a2abc

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	return 0
254}
255
256func smartNumberGeneric(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
257	if wordBoundary(previousChar) && len(text) >= 3 {
258		// is it of the form digits/digits(word boundary)?, i.e., \d+/\d+\b
259		// note: check for regular slash (/) or fraction slash (⁄, 0x2044, or 0xe2 81 84 in utf-8)
260		numEnd := 0
261		for len(text) > numEnd && isdigit(text[numEnd]) {
262			numEnd++
263		}
264		if numEnd == 0 {
265			out.WriteByte(text[0])
266			return 0
267		}
268		denStart := numEnd + 1
269		if len(text) > numEnd+3 && text[numEnd] == 0xe2 && text[numEnd+1] == 0x81 && text[numEnd+2] == 0x84 {
270			denStart = numEnd + 3
271		} else if len(text) < numEnd+2 || text[numEnd] != '/' {
272			out.WriteByte(text[0])
273			return 0
274		}
275		denEnd := denStart
276		for len(text) > denEnd && isdigit(text[denEnd]) {
277			denEnd++
278		}
279		if denEnd == denStart {
280			out.WriteByte(text[0])
281			return 0
282		}
283		if len(text) == denEnd || wordBoundary(text[denEnd]) {
284			out.WriteString("<sup>")
285			out.Write(text[:numEnd])
286			out.WriteString("</sup>&frasl;<sub>")
287			out.Write(text[denStart:denEnd])
288			out.WriteString("</sub>")
289			return denEnd - 1
290		}
291	}
292
293	out.WriteByte(text[0])
294	return 0
295}
296
297func smartNumber(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
298	if wordBoundary(previousChar) && len(text) >= 3 {
299		if text[0] == '1' && text[1] == '/' && text[2] == '2' {
300			if len(text) < 4 || wordBoundary(text[3]) {
301				out.WriteString("&frac12;")
302				return 2
303			}
304		}
305
306		if text[0] == '1' && text[1] == '/' && text[2] == '4' {
307			if len(text) < 4 || wordBoundary(text[3]) || (len(text) >= 5 && tolower(text[3]) == 't' && tolower(text[4]) == 'h') {
308				out.WriteString("&frac14;")
309				return 2
310			}
311		}
312
313		if text[0] == '3' && text[1] == '/' && text[2] == '4' {
314			if len(text) < 4 || wordBoundary(text[3]) || (len(text) >= 6 && tolower(text[3]) == 't' && tolower(text[4]) == 'h' && tolower(text[5]) == 's') {
315				out.WriteString("&frac34;")
316				return 2
317			}
318		}
319	}
320
321	out.WriteByte(text[0])
322	return 0
323}
324
325func smartDoubleQuote(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
326	nextChar := byte(0)
327	if len(text) > 1 {
328		nextChar = text[1]
329	}
330	if !smartQuoteHelper(out, previousChar, nextChar, 'd', &smrt.inDoubleQuote) {
331		out.WriteString("&quot;")
332	}
333
334	return 0
335}
336
337func smartLeftAngle(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
338	i := 0
339
340	for i < len(text) && text[i] != '>' {
341		i++
342	}
343
344	out.Write(text[:i+1])
345	return i
346}
347
348type smartCallback func(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int
349
350type smartypantsRenderer [256]smartCallback
351
352func smartypants(flags int) *smartypantsRenderer {
353	r := new(smartypantsRenderer)
354	r['"'] = smartDoubleQuote
355	r['&'] = smartAmp
356	r['\''] = smartSingleQuote
357	r['('] = smartParens
358	if flags&HTML_SMARTYPANTS_LATEX_DASHES == 0 {
359		r['-'] = smartDash
360	} else {
361		r['-'] = smartDashLatex
362	}
363	r['.'] = smartPeriod
364	if flags&HTML_SMARTYPANTS_FRACTIONS == 0 {
365		r['1'] = smartNumber
366		r['3'] = smartNumber
367	} else {
368		for ch := '1'; ch <= '9'; ch++ {
369			r[ch] = smartNumberGeneric
370		}
371	}
372	r['<'] = smartLeftAngle
373	r['`'] = smartBacktick
374	return r
375}