all repos — grayfriday @ 8cc40f8e07a23f2ddb9059ebf5c8b6f260013ee4

blackfriday fork with a few changes

markdown.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// Markdown parsing and processing
 13//
 14//
 15
 16// Blackfriday markdown processor.
 17//
 18// Translates plain text with simple formatting rules into HTML or LaTeX.
 19package blackfriday
 20
 21import (
 22	"bytes"
 23	"unicode/utf8"
 24)
 25
 26const VERSION = "1.1"
 27
 28// These are the supported markdown parsing extensions.
 29// OR these values together to select multiple extensions.
 30const (
 31	EXTENSION_NO_INTRA_EMPHASIS          = 1 << iota // ignore emphasis markers inside words
 32	EXTENSION_TABLES                                 // render tables
 33	EXTENSION_FENCED_CODE                            // render fenced code blocks
 34	EXTENSION_AUTOLINK                               // detect embedded URLs that are not explicitly marked
 35	EXTENSION_STRIKETHROUGH                          // strikethrough text using ~~test~~
 36	EXTENSION_LAX_HTML_BLOCKS                        // loosen up HTML block parsing rules
 37	EXTENSION_SPACE_HEADERS                          // be strict about prefix header rules
 38	EXTENSION_HARD_LINE_BREAK                        // translate newlines into line breaks
 39	EXTENSION_TAB_SIZE_EIGHT                         // expand tabs to eight spaces instead of four
 40	EXTENSION_FOOTNOTES                              // Pandoc-style footnotes
 41	EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK             // No need to insert an empty line to start a (code, quote, order list, unorder list)block
 42	EXTENSION_HEADER_IDS                             // specify header IDs  with {#id}
 43	EXTENSION_TITLEBLOCK                             // Titleblock ala pandoc
 44
 45	commonHtmlFlags = 0 |
 46		HTML_USE_XHTML |
 47		HTML_USE_SMARTYPANTS |
 48		HTML_SMARTYPANTS_FRACTIONS |
 49		HTML_SMARTYPANTS_LATEX_DASHES
 50
 51	commonExtensions = 0 |
 52		EXTENSION_NO_INTRA_EMPHASIS |
 53		EXTENSION_TABLES |
 54		EXTENSION_FENCED_CODE |
 55		EXTENSION_AUTOLINK |
 56		EXTENSION_STRIKETHROUGH |
 57		EXTENSION_SPACE_HEADERS |
 58		EXTENSION_HEADER_IDS
 59)
 60
 61// These are the possible flag values for the link renderer.
 62// Only a single one of these values will be used; they are not ORed together.
 63// These are mostly of interest if you are writing a new output format.
 64const (
 65	LINK_TYPE_NOT_AUTOLINK = iota
 66	LINK_TYPE_NORMAL
 67	LINK_TYPE_EMAIL
 68)
 69
 70// These are the possible flag values for the ListItem renderer.
 71// Multiple flag values may be ORed together.
 72// These are mostly of interest if you are writing a new output format.
 73const (
 74	LIST_TYPE_ORDERED = 1 << iota
 75	LIST_ITEM_CONTAINS_BLOCK
 76	LIST_ITEM_BEGINNING_OF_LIST
 77	LIST_ITEM_END_OF_LIST
 78)
 79
 80// These are the possible flag values for the table cell renderer.
 81// Only a single one of these values will be used; they are not ORed together.
 82// These are mostly of interest if you are writing a new output format.
 83const (
 84	TABLE_ALIGNMENT_LEFT = 1 << iota
 85	TABLE_ALIGNMENT_RIGHT
 86	TABLE_ALIGNMENT_CENTER = (TABLE_ALIGNMENT_LEFT | TABLE_ALIGNMENT_RIGHT)
 87)
 88
 89// The size of a tab stop.
 90const (
 91	TAB_SIZE_DEFAULT = 4
 92	TAB_SIZE_EIGHT   = 8
 93)
 94
 95// These are the tags that are recognized as HTML block tags.
 96// Any of these can be included in markdown text without special escaping.
 97var blockTags = map[string]bool{
 98	"p":          true,
 99	"dl":         true,
100	"h1":         true,
101	"h2":         true,
102	"h3":         true,
103	"h4":         true,
104	"h5":         true,
105	"h6":         true,
106	"ol":         true,
107	"ul":         true,
108	"del":        true,
109	"div":        true,
110	"ins":        true,
111	"pre":        true,
112	"form":       true,
113	"math":       true,
114	"table":      true,
115	"iframe":     true,
116	"script":     true,
117	"fieldset":   true,
118	"noscript":   true,
119	"blockquote": true,
120
121	// HTML5
122	"video":      true,
123	"aside":      true,
124	"canvas":     true,
125	"figure":     true,
126	"footer":     true,
127	"header":     true,
128	"hgroup":     true,
129	"output":     true,
130	"article":    true,
131	"section":    true,
132	"progress":   true,
133	"figcaption": true,
134}
135
136// Renderer is the rendering interface.
137// This is mostly of interest if you are implementing a new rendering format.
138//
139// When a byte slice is provided, it contains the (rendered) contents of the
140// element.
141//
142// When a callback is provided instead, it will write the contents of the
143// respective element directly to the output buffer and return true on success.
144// If the callback returns false, the rendering function should reset the
145// output buffer as though it had never been called.
146//
147// Currently Html and Latex implementations are provided
148type Renderer interface {
149	// block-level callbacks
150	BlockCode(out *bytes.Buffer, text []byte, lang string)
151	BlockQuote(out *bytes.Buffer, text []byte)
152	BlockHtml(out *bytes.Buffer, text []byte)
153	Header(out *bytes.Buffer, text func() bool, level int, id string)
154	HRule(out *bytes.Buffer)
155	List(out *bytes.Buffer, text func() bool, flags int)
156	ListItem(out *bytes.Buffer, text []byte, flags int)
157	Paragraph(out *bytes.Buffer, text func() bool)
158	Table(out *bytes.Buffer, header []byte, body []byte, columnData []int)
159	TableRow(out *bytes.Buffer, text []byte)
160	TableHeaderCell(out *bytes.Buffer, text []byte, flags int)
161	TableCell(out *bytes.Buffer, text []byte, flags int)
162	Footnotes(out *bytes.Buffer, text func() bool)
163	FootnoteItem(out *bytes.Buffer, name, text []byte, flags int)
164	TitleBlock(out *bytes.Buffer, text []byte)
165
166	// Span-level callbacks
167	AutoLink(out *bytes.Buffer, link []byte, kind int)
168	CodeSpan(out *bytes.Buffer, text []byte)
169	DoubleEmphasis(out *bytes.Buffer, text []byte)
170	Emphasis(out *bytes.Buffer, text []byte)
171	Image(out *bytes.Buffer, link []byte, title []byte, alt []byte)
172	LineBreak(out *bytes.Buffer)
173	Link(out *bytes.Buffer, link []byte, title []byte, content []byte)
174	RawHtmlTag(out *bytes.Buffer, tag []byte)
175	TripleEmphasis(out *bytes.Buffer, text []byte)
176	StrikeThrough(out *bytes.Buffer, text []byte)
177	FootnoteRef(out *bytes.Buffer, ref []byte, id int)
178
179	// Low-level callbacks
180	Entity(out *bytes.Buffer, entity []byte)
181	NormalText(out *bytes.Buffer, text []byte)
182
183	// Header and footer
184	DocumentHeader(out *bytes.Buffer)
185	DocumentFooter(out *bytes.Buffer)
186
187	GetFlags() int
188}
189
190// Callback functions for inline parsing. One such function is defined
191// for each character that triggers a response when parsing inline data.
192type inlineParser func(p *parser, out *bytes.Buffer, data []byte, offset int) int
193
194// Parser holds runtime state used by the parser.
195// This is constructed by the Markdown function.
196type parser struct {
197	r              Renderer
198	refs           map[string]*reference
199	inlineCallback [256]inlineParser
200	flags          int
201	nesting        int
202	maxNesting     int
203	insideLink     bool
204
205	// Footnotes need to be ordered as well as available to quickly check for
206	// presence. If a ref is also a footnote, it's stored both in refs and here
207	// in notes. Slice is nil if footnotes not enabled.
208	notes []*reference
209}
210
211//
212//
213// Public interface
214//
215//
216
217// MarkdownBasic is a convenience function for simple rendering.
218// It processes markdown input with no extensions enabled.
219func MarkdownBasic(input []byte) []byte {
220	// set up the HTML renderer
221	htmlFlags := HTML_USE_XHTML
222	renderer := HtmlRenderer(htmlFlags, "", "")
223
224	// set up the parser
225	extensions := 0
226
227	return Markdown(input, renderer, extensions)
228}
229
230// Call Markdown with most useful extensions enabled
231// MarkdownCommon is a convenience function for simple rendering.
232// It processes markdown input with common extensions enabled, including:
233//
234// * Smartypants processing with smart fractions and LaTeX dashes
235//
236// * Intra-word emphasis suppression
237//
238// * Tables
239//
240// * Fenced code blocks
241//
242// * Autolinking
243//
244// * Strikethrough support
245//
246// * Strict header parsing
247//
248// * Custom Header IDs
249func MarkdownCommon(input []byte) []byte {
250	// set up the HTML renderer
251	renderer := HtmlRenderer(commonHtmlFlags, "", "")
252	return Markdown(input, renderer, commonExtensions)
253}
254
255// Markdown is the main rendering function.
256// It parses and renders a block of markdown-encoded text.
257// The supplied Renderer is used to format the output, and extensions dictates
258// which non-standard extensions are enabled.
259//
260// To use the supplied Html or LaTeX renderers, see HtmlRenderer and
261// LatexRenderer, respectively.
262func Markdown(input []byte, renderer Renderer, extensions int) []byte {
263	// no point in parsing if we can't render
264	if renderer == nil {
265		return nil
266	}
267
268	// fill in the render structure
269	p := new(parser)
270	p.r = renderer
271	p.flags = extensions
272	p.refs = make(map[string]*reference)
273	p.maxNesting = 16
274	p.insideLink = false
275
276	// register inline parsers
277	p.inlineCallback['*'] = emphasis
278	p.inlineCallback['_'] = emphasis
279	if extensions&EXTENSION_STRIKETHROUGH != 0 {
280		p.inlineCallback['~'] = emphasis
281	}
282	p.inlineCallback['`'] = codeSpan
283	p.inlineCallback['\n'] = lineBreak
284	p.inlineCallback['['] = link
285	p.inlineCallback['<'] = leftAngle
286	p.inlineCallback['\\'] = escape
287	p.inlineCallback['&'] = entity
288
289	if extensions&EXTENSION_AUTOLINK != 0 {
290		p.inlineCallback[':'] = autoLink
291	}
292
293	if extensions&EXTENSION_FOOTNOTES != 0 {
294		p.notes = make([]*reference, 0)
295	}
296
297	first := firstPass(p, input)
298	second := secondPass(p, first)
299	return second
300}
301
302// first pass:
303// - extract references
304// - expand tabs
305// - normalize newlines
306// - copy everything else
307// - add missing newlines before fenced code blocks
308func firstPass(p *parser, input []byte) []byte {
309	var out bytes.Buffer
310	tabSize := TAB_SIZE_DEFAULT
311	if p.flags&EXTENSION_TAB_SIZE_EIGHT != 0 {
312		tabSize = TAB_SIZE_EIGHT
313	}
314	beg, end := 0, 0
315	lastLineWasBlank := false
316	lastFencedCodeBlockEnd := 0
317	for beg < len(input) { // iterate over lines
318		if end = isReference(p, input[beg:], tabSize); end > 0 {
319			beg += end
320		} else { // skip to the next line
321			end = beg
322			for end < len(input) && input[end] != '\n' && input[end] != '\r' {
323				end++
324			}
325
326			if p.flags&EXTENSION_FENCED_CODE != 0 {
327				// when last line was none blank and a fenced code block comes after
328				if beg >= lastFencedCodeBlockEnd {
329					// tmp var so we don't modify beyond bounds of `input`
330					var tmp = make([]byte, len(input[beg:]), len(input[beg:])+1)
331					copy(tmp, input[beg:])
332					if i := p.fencedCode(&out, append(tmp, '\n'), false); i > 0 {
333						if !lastLineWasBlank {
334							out.WriteByte('\n') // need to inject additional linebreak
335						}
336						lastFencedCodeBlockEnd = beg + i
337					}
338				}
339				lastLineWasBlank = end == beg
340			}
341
342			// add the line body if present
343			if end > beg {
344				if end < lastFencedCodeBlockEnd { // Do not expand tabs while inside fenced code blocks.
345					out.Write(input[beg:end])
346				} else {
347					expandTabs(&out, input[beg:end], tabSize)
348				}
349			}
350			out.WriteByte('\n')
351
352			if end < len(input) && input[end] == '\r' {
353				end++
354			}
355			if end < len(input) && input[end] == '\n' {
356				end++
357			}
358
359			beg = end
360		}
361	}
362
363	// empty input?
364	if out.Len() == 0 {
365		out.WriteByte('\n')
366	}
367
368	return out.Bytes()
369}
370
371// second pass: actual rendering
372func secondPass(p *parser, input []byte) []byte {
373	var output bytes.Buffer
374
375	p.r.DocumentHeader(&output)
376	p.block(&output, input)
377
378	if p.flags&EXTENSION_FOOTNOTES != 0 && len(p.notes) > 0 {
379		p.r.Footnotes(&output, func() bool {
380			flags := LIST_ITEM_BEGINNING_OF_LIST
381			for _, ref := range p.notes {
382				var buf bytes.Buffer
383				if ref.hasBlock {
384					flags |= LIST_ITEM_CONTAINS_BLOCK
385					p.block(&buf, ref.title)
386				} else {
387					p.inline(&buf, ref.title)
388				}
389				p.r.FootnoteItem(&output, ref.link, buf.Bytes(), flags)
390				flags &^= LIST_ITEM_BEGINNING_OF_LIST | LIST_ITEM_CONTAINS_BLOCK
391			}
392
393			return true
394		})
395	}
396
397	p.r.DocumentFooter(&output)
398
399	if p.nesting != 0 {
400		panic("Nesting level did not end at zero")
401	}
402
403	return output.Bytes()
404}
405
406//
407// Link references
408//
409// This section implements support for references that (usually) appear
410// as footnotes in a document, and can be referenced anywhere in the document.
411// The basic format is:
412//
413//    [1]: http://www.google.com/ "Google"
414//    [2]: http://www.github.com/ "Github"
415//
416// Anywhere in the document, the reference can be linked by referring to its
417// label, i.e., 1 and 2 in this example, as in:
418//
419//    This library is hosted on [Github][2], a git hosting site.
420//
421// Actual footnotes as specified in Pandoc and supported by some other Markdown
422// libraries such as php-markdown are also taken care of. They look like this:
423//
424//    This sentence needs a bit of further explanation.[^note]
425//
426//    [^note]: This is the explanation.
427//
428// Footnotes should be placed at the end of the document in an ordered list.
429// Inline footnotes such as:
430//
431//    Inline footnotes^[Not supported.] also exist.
432//
433// are not yet supported.
434
435// References are parsed and stored in this struct.
436type reference struct {
437	link     []byte
438	title    []byte
439	noteId   int // 0 if not a footnote ref
440	hasBlock bool
441}
442
443// Check whether or not data starts with a reference link.
444// If so, it is parsed and stored in the list of references
445// (in the render struct).
446// Returns the number of bytes to skip to move past it,
447// or zero if the first line is not a reference.
448func isReference(p *parser, data []byte, tabSize int) int {
449	// up to 3 optional leading spaces
450	if len(data) < 4 {
451		return 0
452	}
453	i := 0
454	for i < 3 && data[i] == ' ' {
455		i++
456	}
457
458	noteId := 0
459
460	// id part: anything but a newline between brackets
461	if data[i] != '[' {
462		return 0
463	}
464	i++
465	if p.flags&EXTENSION_FOOTNOTES != 0 {
466		if data[i] == '^' {
467			// we can set it to anything here because the proper noteIds will
468			// be assigned later during the second pass. It just has to be != 0
469			noteId = 1
470			i++
471		}
472	}
473	idOffset := i
474	for i < len(data) && data[i] != '\n' && data[i] != '\r' && data[i] != ']' {
475		i++
476	}
477	if i >= len(data) || data[i] != ']' {
478		return 0
479	}
480	idEnd := i
481
482	// spacer: colon (space | tab)* newline? (space | tab)*
483	i++
484	if i >= len(data) || data[i] != ':' {
485		return 0
486	}
487	i++
488	for i < len(data) && (data[i] == ' ' || data[i] == '\t') {
489		i++
490	}
491	if i < len(data) && (data[i] == '\n' || data[i] == '\r') {
492		i++
493		if i < len(data) && data[i] == '\n' && data[i-1] == '\r' {
494			i++
495		}
496	}
497	for i < len(data) && (data[i] == ' ' || data[i] == '\t') {
498		i++
499	}
500	if i >= len(data) {
501		return 0
502	}
503
504	var (
505		linkOffset, linkEnd   int
506		titleOffset, titleEnd int
507		lineEnd               int
508		raw                   []byte
509		hasBlock              bool
510	)
511
512	if p.flags&EXTENSION_FOOTNOTES != 0 && noteId != 0 {
513		linkOffset, linkEnd, raw, hasBlock = scanFootnote(p, data, i, tabSize)
514		lineEnd = linkEnd
515	} else {
516		linkOffset, linkEnd, titleOffset, titleEnd, lineEnd = scanLinkRef(p, data, i)
517	}
518	if lineEnd == 0 {
519		return 0
520	}
521
522	// a valid ref has been found
523
524	ref := &reference{
525		noteId:   noteId,
526		hasBlock: hasBlock,
527	}
528
529	if noteId > 0 {
530		// reusing the link field for the id since footnotes don't have links
531		ref.link = data[idOffset:idEnd]
532		// if footnote, it's not really a title, it's the contained text
533		ref.title = raw
534	} else {
535		ref.link = data[linkOffset:linkEnd]
536		ref.title = data[titleOffset:titleEnd]
537	}
538
539	// id matches are case-insensitive
540	id := string(bytes.ToLower(data[idOffset:idEnd]))
541
542	p.refs[id] = ref
543
544	return lineEnd
545}
546
547func scanLinkRef(p *parser, data []byte, i int) (linkOffset, linkEnd, titleOffset, titleEnd, lineEnd int) {
548	// link: whitespace-free sequence, optionally between angle brackets
549	if data[i] == '<' {
550		i++
551	}
552	linkOffset = i
553	for i < len(data) && data[i] != ' ' && data[i] != '\t' && data[i] != '\n' && data[i] != '\r' {
554		i++
555	}
556	linkEnd = i
557	if data[linkOffset] == '<' && data[linkEnd-1] == '>' {
558		linkOffset++
559		linkEnd--
560	}
561
562	// optional spacer: (space | tab)* (newline | '\'' | '"' | '(' )
563	for i < len(data) && (data[i] == ' ' || data[i] == '\t') {
564		i++
565	}
566	if i < len(data) && data[i] != '\n' && data[i] != '\r' && data[i] != '\'' && data[i] != '"' && data[i] != '(' {
567		return
568	}
569
570	// compute end-of-line
571	if i >= len(data) || data[i] == '\r' || data[i] == '\n' {
572		lineEnd = i
573	}
574	if i+1 < len(data) && data[i] == '\r' && data[i+1] == '\n' {
575		lineEnd++
576	}
577
578	// optional (space|tab)* spacer after a newline
579	if lineEnd > 0 {
580		i = lineEnd + 1
581		for i < len(data) && (data[i] == ' ' || data[i] == '\t') {
582			i++
583		}
584	}
585
586	// optional title: any non-newline sequence enclosed in '"() alone on its line
587	if i+1 < len(data) && (data[i] == '\'' || data[i] == '"' || data[i] == '(') {
588		i++
589		titleOffset = i
590
591		// look for EOL
592		for i < len(data) && data[i] != '\n' && data[i] != '\r' {
593			i++
594		}
595		if i+1 < len(data) && data[i] == '\n' && data[i+1] == '\r' {
596			titleEnd = i + 1
597		} else {
598			titleEnd = i
599		}
600
601		// step back
602		i--
603		for i > titleOffset && (data[i] == ' ' || data[i] == '\t') {
604			i--
605		}
606		if i > titleOffset && (data[i] == '\'' || data[i] == '"' || data[i] == ')') {
607			lineEnd = titleEnd
608			titleEnd = i
609		}
610	}
611
612	return
613}
614
615// The first bit of this logic is the same as (*parser).listItem, but the rest
616// is much simpler. This function simply finds the entire block and shifts it
617// over by one tab if it is indeed a block (just returns the line if it's not).
618// blockEnd is the end of the section in the input buffer, and contents is the
619// extracted text that was shifted over one tab. It will need to be rendered at
620// the end of the document.
621func scanFootnote(p *parser, data []byte, i, indentSize int) (blockStart, blockEnd int, contents []byte, hasBlock bool) {
622	if i == 0 || len(data) == 0 {
623		return
624	}
625
626	// skip leading whitespace on first line
627	for i < len(data) && data[i] == ' ' {
628		i++
629	}
630
631	blockStart = i
632
633	// find the end of the line
634	blockEnd = i
635	for i < len(data) && data[i-1] != '\n' {
636		i++
637	}
638
639	// get working buffer
640	var raw bytes.Buffer
641
642	// put the first line into the working buffer
643	raw.Write(data[blockEnd:i])
644	blockEnd = i
645
646	// process the following lines
647	containsBlankLine := false
648
649gatherLines:
650	for blockEnd < len(data) {
651		i++
652
653		// find the end of this line
654		for i < len(data) && data[i-1] != '\n' {
655			i++
656		}
657
658		// if it is an empty line, guess that it is part of this item
659		// and move on to the next line
660		if p.isEmpty(data[blockEnd:i]) > 0 {
661			containsBlankLine = true
662			blockEnd = i
663			continue
664		}
665
666		n := 0
667		if n = isIndented(data[blockEnd:i], indentSize); n == 0 {
668			// this is the end of the block.
669			// we don't want to include this last line in the index.
670			break gatherLines
671		}
672
673		// if there were blank lines before this one, insert a new one now
674		if containsBlankLine {
675			raw.WriteByte('\n')
676			containsBlankLine = false
677		}
678
679		// get rid of that first tab, write to buffer
680		raw.Write(data[blockEnd+n : i])
681		hasBlock = true
682
683		blockEnd = i
684	}
685
686	if data[blockEnd-1] != '\n' {
687		raw.WriteByte('\n')
688	}
689
690	contents = raw.Bytes()
691
692	return
693}
694
695//
696//
697// Miscellaneous helper functions
698//
699//
700
701// Test if a character is a punctuation symbol.
702// Taken from a private function in regexp in the stdlib.
703func ispunct(c byte) bool {
704	for _, r := range []byte("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~") {
705		if c == r {
706			return true
707		}
708	}
709	return false
710}
711
712// Test if a character is a whitespace character.
713func isspace(c byte) bool {
714	return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' || c == '\v'
715}
716
717// Test if a character is letter.
718func isletter(c byte) bool {
719	return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
720}
721
722// Test if a character is a letter or a digit.
723// TODO: check when this is looking for ASCII alnum and when it should use unicode
724func isalnum(c byte) bool {
725	return (c >= '0' && c <= '9') || isletter(c)
726}
727
728// Replace tab characters with spaces, aligning to the next TAB_SIZE column.
729// always ends output with a newline
730func expandTabs(out *bytes.Buffer, line []byte, tabSize int) {
731	// first, check for common cases: no tabs, or only tabs at beginning of line
732	i, prefix := 0, 0
733	slowcase := false
734	for i = 0; i < len(line); i++ {
735		if line[i] == '\t' {
736			if prefix == i {
737				prefix++
738			} else {
739				slowcase = true
740				break
741			}
742		}
743	}
744
745	// no need to decode runes if all tabs are at the beginning of the line
746	if !slowcase {
747		for i = 0; i < prefix*tabSize; i++ {
748			out.WriteByte(' ')
749		}
750		out.Write(line[prefix:])
751		return
752	}
753
754	// the slow case: we need to count runes to figure out how
755	// many spaces to insert for each tab
756	column := 0
757	i = 0
758	for i < len(line) {
759		start := i
760		for i < len(line) && line[i] != '\t' {
761			_, size := utf8.DecodeRune(line[i:])
762			i += size
763			column++
764		}
765
766		if i > start {
767			out.Write(line[start:i])
768		}
769
770		if i >= len(line) {
771			break
772		}
773
774		for {
775			out.WriteByte(' ')
776			column++
777			if column%tabSize == 0 {
778				break
779			}
780		}
781
782		i++
783	}
784}
785
786// Find if a line counts as indented or not.
787// Returns number of characters the indent is (0 = not indented).
788func isIndented(data []byte, indentSize int) int {
789	if len(data) == 0 {
790		return 0
791	}
792	if data[0] == '\t' {
793		return 1
794	}
795	if len(data) < indentSize {
796		return 0
797	}
798	for i := 0; i < indentSize; i++ {
799		if data[i] != ' ' {
800			return 0
801		}
802	}
803	return indentSize
804}
805
806// Create a url-safe slug for fragments
807func slugify(in []byte) []byte {
808	if len(in) == 0 {
809		return in
810	}
811	out := make([]byte, 0, len(in))
812	sym := false
813
814	for _, ch := range in {
815		if isalnum(ch) {
816			sym = false
817			out = append(out, ch)
818		} else if sym {
819			continue
820		} else {
821			out = append(out, '-')
822			sym = true
823		}
824	}
825	var a, b int
826	var ch byte
827	for a, ch = range out {
828		if ch != '-' {
829			break
830		}
831	}
832	for b = len(out) - 1; b > 0; b-- {
833		if out[b] != '-' {
834			break
835		}
836	}
837	return out[a : b+1]
838}