all repos — grayfriday @ 8b9cd447d79fee1450f0ad18063edbc375d4225b

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