all repos — grayfriday @ 4bd8627b2ca34a46892d1f5bdf88daadf0609f06

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// Licensed 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(ob *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	ob.WriteByte('&')
100	if *isOpen {
101		ob.WriteByte('l')
102	} else {
103		ob.WriteByte('r')
104	}
105	ob.WriteByte(quote)
106	ob.WriteString("quo;")
107	return true
108}
109
110func smartSquote(ob *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(ob, 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			ob.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')) && (len(text) < 4 || wordBoundary(text[3])) {
133				ob.WriteString("&rsquo;")
134				return 0
135			}
136		}
137	}
138
139	nextChar := byte(0)
140	if len(text) > 1 {
141		nextChar = text[1]
142	}
143	if smartQuotesHelper(ob, previousChar, nextChar, 's', &smrt.inSingleQuote) {
144		return 0
145	}
146
147	ob.WriteByte(text[0])
148	return 0
149}
150
151func smartParens(ob *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
152	if len(text) >= 3 {
153		t1 := tolower(text[1])
154		t2 := tolower(text[2])
155
156		if t1 == 'c' && t2 == ')' {
157			ob.WriteString("&copy;")
158			return 2
159		}
160
161		if t1 == 'r' && t2 == ')' {
162			ob.WriteString("&reg;")
163			return 2
164		}
165
166		if len(text) >= 4 && t1 == 't' && t2 == 'm' && text[3] == ')' {
167			ob.WriteString("&trade;")
168			return 3
169		}
170	}
171
172	ob.WriteByte(text[0])
173	return 0
174}
175
176func smartDash(ob *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
177	if len(text) >= 2 {
178		if text[1] == '-' {
179			ob.WriteString("&mdash;")
180			return 1
181		}
182
183		if wordBoundary(previousChar) && wordBoundary(text[1]) {
184			ob.WriteString("&ndash;")
185			return 0
186		}
187	}
188
189	ob.WriteByte(text[0])
190	return 0
191}
192
193func smartDashLatex(ob *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
194	if len(text) >= 3 && text[1] == '-' && text[2] == '-' {
195		ob.WriteString("&mdash;")
196		return 2
197	}
198	if len(text) >= 2 && text[1] == '-' {
199		ob.WriteString("&ndash;")
200		return 1
201	}
202
203	ob.WriteByte(text[0])
204	return 0
205}
206
207func smartAmp(ob *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
208	if bytes.HasPrefix(text, []byte("&quot;")) {
209		nextChar := byte(0)
210		if len(text) >= 7 {
211			nextChar = text[6]
212		}
213		if smartQuotesHelper(ob, previousChar, nextChar, 'd', &smrt.inDoubleQuote) {
214			return 5
215		}
216	}
217
218	if bytes.HasPrefix(text, []byte("&#0;")) {
219		return 3
220	}
221
222	ob.WriteByte('&')
223	return 0
224}
225
226func smartPeriod(ob *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
227	if len(text) >= 3 && text[1] == '.' && text[2] == '.' {
228		ob.WriteString("&hellip;")
229		return 2
230	}
231
232	if len(text) >= 5 && text[1] == ' ' && text[2] == '.' && text[3] == ' ' && text[4] == '.' {
233		ob.WriteString("&hellip;")
234		return 4
235	}
236
237	ob.WriteByte(text[0])
238	return 0
239}
240
241func smartBacktick(ob *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
242	if len(text) >= 2 && text[1] == '`' {
243		nextChar := byte(0)
244		if len(text) >= 3 {
245			nextChar = text[2]
246		}
247		if smartQuotesHelper(ob, previousChar, nextChar, 'd', &smrt.inDoubleQuote) {
248			return 1
249		}
250	}
251
252	return 0
253}
254
255func smartNumberGeneric(ob *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
256	if wordBoundary(previousChar) && len(text) >= 3 {
257		// is it of the form digits/digits(word boundary)?, i.e., \d+/\d+\b
258		num_end := 0
259		for len(text) > num_end && isdigit(text[num_end]) {
260			num_end++
261		}
262		if num_end == 0 {
263			ob.WriteByte(text[0])
264			return 0
265		}
266		if len(text) < num_end+2 || text[num_end] != '/' {
267			ob.WriteByte(text[0])
268			return 0
269		}
270		den_end := num_end + 1
271		for len(text) > den_end && isdigit(text[den_end]) {
272			den_end++
273		}
274		if den_end == num_end+1 {
275			ob.WriteByte(text[0])
276			return 0
277		}
278		if len(text) == den_end || wordBoundary(text[den_end]) {
279			ob.WriteString("<sup>")
280			ob.Write(text[:num_end])
281			ob.WriteString("</sup>&frasl;<sub>")
282			ob.Write(text[num_end+1 : den_end])
283			ob.WriteString("</sub>")
284			return den_end - 1
285		}
286	}
287
288	ob.WriteByte(text[0])
289	return 0
290}
291
292func smartNumber(ob *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
293	if wordBoundary(previousChar) && len(text) >= 3 {
294		if text[0] == '1' && text[1] == '/' && text[2] == '2' {
295			if len(text) < 4 || wordBoundary(text[3]) {
296				ob.WriteString("&frac12;")
297				return 2
298			}
299		}
300
301		if text[0] == '1' && text[1] == '/' && text[2] == '4' {
302			if len(text) < 4 || wordBoundary(text[3]) || (len(text) >= 5 && tolower(text[3]) == 't' && tolower(text[4]) == 'h') {
303				ob.WriteString("&frac14;")
304				return 2
305			}
306		}
307
308		if text[0] == '3' && text[1] == '/' && text[2] == '4' {
309			if len(text) < 4 || wordBoundary(text[3]) || (len(text) >= 6 && tolower(text[3]) == 't' && tolower(text[4]) == 'h' && tolower(text[5]) == 's') {
310				ob.WriteString("&frac34;")
311				return 2
312			}
313		}
314	}
315
316	ob.WriteByte(text[0])
317	return 0
318}
319
320func smartDquote(ob *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
321	nextChar := byte(0)
322	if len(text) > 1 {
323		nextChar = text[1]
324	}
325	if !smartQuotesHelper(ob, previousChar, nextChar, 'd', &smrt.inDoubleQuote) {
326		ob.WriteString("&quot;")
327	}
328
329	return 0
330}
331
332func smartLtag(ob *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
333	i := 0
334
335	for i < len(text) && text[i] != '>' {
336		i++
337	}
338
339	ob.Write(text[:i+1])
340	return i
341}
342
343type smartCallback func(ob *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int
344
345type SmartypantsRenderer [256]smartCallback
346
347func Smartypants(flags int) *SmartypantsRenderer {
348	r := new(SmartypantsRenderer)
349	r['"'] = smartDquote
350	r['&'] = smartAmp
351	r['\''] = smartSquote
352	r['('] = smartParens
353	if flags&HTML_SMARTYPANTS_LATEX_DASHES == 0 {
354		r['-'] = smartDash
355	} else {
356		r['-'] = smartDashLatex
357	}
358	r['.'] = smartPeriod
359	if flags&HTML_SMARTYPANTS_FRACTIONS == 0 {
360		r['1'] = smartNumber
361		r['3'] = smartNumber
362	} else {
363		for ch := '1'; ch <= '9'; ch++ {
364			r[ch] = smartNumberGeneric
365		}
366	}
367	r['<'] = smartLtag
368	r['`'] = smartBacktick
369	return r
370}
371
372func htmlSmartypants(ob *bytes.Buffer, text []byte, opaque interface{}) {
373	options := opaque.(*htmlOptions)
374	smrt := smartypantsData{false, false}
375
376	// first do normal entity escaping
377	var escaped bytes.Buffer
378	attrEscape(&escaped, text)
379	text = escaped.Bytes()
380
381	mark := 0
382	for i := 0; i < len(text); i++ {
383		if action := options.smartypants[text[i]]; action != nil {
384			if i > mark {
385				ob.Write(text[mark:i])
386			}
387
388			previousChar := byte(0)
389			if i > 0 {
390				previousChar = text[i-1]
391			}
392			i += action(ob, &smrt, previousChar, text[i:])
393			mark = i + 1
394		}
395	}
396
397	if mark < len(text) {
398		ob.Write(text[mark:])
399	}
400}