all repos — grayfriday @ 58c4ddd4312ed3590e02b837c785cef03554e530

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	EXTENSION_AUTO_HEADER_IDS                        // Create the header ID from the text
 45	EXTENSION_BACKSLASH_LINE_BREAK                   // translate trailing backslashes into line breaks
 46
 47	commonHtmlFlags = 0 |
 48		HTML_USE_XHTML |
 49		HTML_USE_SMARTYPANTS |
 50		HTML_SMARTYPANTS_FRACTIONS |
 51		HTML_SMARTYPANTS_LATEX_DASHES
 52
 53	commonExtensions = 0 |
 54		EXTENSION_NO_INTRA_EMPHASIS |
 55		EXTENSION_TABLES |
 56		EXTENSION_FENCED_CODE |
 57		EXTENSION_AUTOLINK |
 58		EXTENSION_STRIKETHROUGH |
 59		EXTENSION_SPACE_HEADERS |
 60		EXTENSION_HEADER_IDS |
 61		EXTENSION_BACKSLASH_LINE_BREAK
 62)
 63
 64// These are the possible flag values for the link renderer.
 65// Only a single one of these values will be used; they are not ORed together.
 66// These are mostly of interest if you are writing a new output format.
 67const (
 68	LINK_TYPE_NOT_AUTOLINK = iota
 69	LINK_TYPE_NORMAL
 70	LINK_TYPE_EMAIL
 71)
 72
 73// These are the possible flag values for the ListItem renderer.
 74// Multiple flag values may be ORed together.
 75// These are mostly of interest if you are writing a new output format.
 76const (
 77	LIST_TYPE_ORDERED = 1 << iota
 78	LIST_ITEM_CONTAINS_BLOCK
 79	LIST_ITEM_BEGINNING_OF_LIST
 80	LIST_ITEM_END_OF_LIST
 81)
 82
 83// These are the possible flag values for the table cell renderer.
 84// Only a single one of these values will be used; they are not ORed together.
 85// These are mostly of interest if you are writing a new output format.
 86const (
 87	TABLE_ALIGNMENT_LEFT = 1 << iota
 88	TABLE_ALIGNMENT_RIGHT
 89	TABLE_ALIGNMENT_CENTER = (TABLE_ALIGNMENT_LEFT | TABLE_ALIGNMENT_RIGHT)
 90)
 91
 92// The size of a tab stop.
 93const (
 94	TAB_SIZE_DEFAULT = 4
 95	TAB_SIZE_EIGHT   = 8
 96)
 97
 98// These are the tags that are recognized as HTML block tags.
 99// Any of these can be included in markdown text without special escaping.
100var blockTags = map[string]bool{
101	"p":          true,
102	"dl":         true,
103	"h1":         true,
104	"h2":         true,
105	"h3":         true,
106	"h4":         true,
107	"h5":         true,
108	"h6":         true,
109	"ol":         true,
110	"ul":         true,
111	"del":        true,
112	"div":        true,
113	"ins":        true,
114	"pre":        true,
115	"form":       true,
116	"math":       true,
117	"table":      true,
118	"iframe":     true,
119	"script":     true,
120	"fieldset":   true,
121	"noscript":   true,
122	"blockquote": true,
123
124	// HTML5
125	"video":      true,
126	"aside":      true,
127	"canvas":     true,
128	"figure":     true,
129	"footer":     true,
130	"header":     true,
131	"hgroup":     true,
132	"output":     true,
133	"article":    true,
134	"section":    true,
135	"progress":   true,
136	"figcaption": true,
137}
138
139// Renderer is the rendering interface.
140// This is mostly of interest if you are implementing a new rendering format.
141//
142// When a byte slice is provided, it contains the (rendered) contents of the
143// element.
144//
145// When a callback is provided instead, it will write the contents of the
146// respective element directly to the output buffer and return true on success.
147// If the callback returns false, the rendering function should reset the
148// output buffer as though it had never been called.
149//
150// Currently Html and Latex implementations are provided
151type Renderer interface {
152	// block-level callbacks
153	BlockCode(out *bytes.Buffer, text []byte, lang string)
154	BlockQuote(out *bytes.Buffer, text []byte)
155	BlockHtml(out *bytes.Buffer, text []byte)
156	Header(out *bytes.Buffer, text func() bool, level int, id string)
157	HRule(out *bytes.Buffer)
158	List(out *bytes.Buffer, text func() bool, flags int)
159	ListItem(out *bytes.Buffer, text []byte, flags int)
160	Paragraph(out *bytes.Buffer, text func() bool)
161	Table(out *bytes.Buffer, header []byte, body []byte, columnData []int)
162	TableRow(out *bytes.Buffer, text []byte)
163	TableHeaderCell(out *bytes.Buffer, text []byte, flags int)
164	TableCell(out *bytes.Buffer, text []byte, flags int)
165	Footnotes(out *bytes.Buffer, text func() bool)
166	FootnoteItem(out *bytes.Buffer, name, text []byte, flags int)
167	TitleBlock(out *bytes.Buffer, text []byte)
168
169	// Span-level callbacks
170	AutoLink(out *bytes.Buffer, link []byte, kind int)
171	CodeSpan(out *bytes.Buffer, text []byte)
172	DoubleEmphasis(out *bytes.Buffer, text []byte)
173	Emphasis(out *bytes.Buffer, text []byte)
174	Image(out *bytes.Buffer, link []byte, title []byte, alt []byte)
175	LineBreak(out *bytes.Buffer)
176	Link(out *bytes.Buffer, link []byte, title []byte, content []byte)
177	RawHtmlTag(out *bytes.Buffer, tag []byte)
178	TripleEmphasis(out *bytes.Buffer, text []byte)
179	StrikeThrough(out *bytes.Buffer, text []byte)
180	FootnoteRef(out *bytes.Buffer, ref []byte, id int)
181
182	// Low-level callbacks
183	Entity(out *bytes.Buffer, entity []byte)
184	NormalText(out *bytes.Buffer, text []byte)
185
186	// Header and footer
187	DocumentHeader(out *bytes.Buffer)
188	DocumentFooter(out *bytes.Buffer)
189
190	GetFlags() int
191}
192
193// Callback functions for inline parsing. One such function is defined
194// for each character that triggers a response when parsing inline data.
195type inlineParser func(p *parser, out *bytes.Buffer, data []byte, offset int) int
196
197// Parser holds runtime state used by the parser.
198// This is constructed by the Markdown function.
199type parser struct {
200	r              Renderer
201	refs           map[string]*reference
202	inlineCallback [256]inlineParser
203	flags          int
204	nesting        int
205	maxNesting     int
206	insideLink     bool
207
208	// Footnotes need to be ordered as well as available to quickly check for
209	// presence. If a ref is also a footnote, it's stored both in refs and here
210	// in notes. Slice is nil if footnotes not enabled.
211	notes []*reference
212}
213
214//
215//
216// Public interface
217//
218//
219
220// MarkdownBasic is a convenience function for simple rendering.
221// It processes markdown input with no extensions enabled.
222func MarkdownBasic(input []byte) []byte {
223	// set up the HTML renderer
224	htmlFlags := HTML_USE_XHTML
225	renderer := HtmlRenderer(htmlFlags, "", "")
226
227	// set up the parser
228	extensions := 0
229
230	return Markdown(input, renderer, extensions)
231}
232
233// Call Markdown with most useful extensions enabled
234// MarkdownCommon is a convenience function for simple rendering.
235// It processes markdown input with common extensions enabled, including:
236//
237// * Smartypants processing with smart fractions and LaTeX dashes
238//
239// * Intra-word emphasis suppression
240//
241// * Tables
242//
243// * Fenced code blocks
244//
245// * Autolinking
246//
247// * Strikethrough support
248//
249// * Strict header parsing
250//
251// * Custom Header IDs
252func MarkdownCommon(input []byte) []byte {
253	// set up the HTML renderer
254	renderer := HtmlRenderer(commonHtmlFlags, "", "")
255	return Markdown(input, renderer, commonExtensions)
256}
257
258// Markdown is the main rendering function.
259// It parses and renders a block of markdown-encoded text.
260// The supplied Renderer is used to format the output, and extensions dictates
261// which non-standard extensions are enabled.
262//
263// To use the supplied Html or LaTeX renderers, see HtmlRenderer and
264// LatexRenderer, respectively.
265func Markdown(input []byte, renderer Renderer, extensions int) []byte {
266	// no point in parsing if we can't render
267	if renderer == nil {
268		return nil
269	}
270
271	// fill in the render structure
272	p := new(parser)
273	p.r = renderer
274	p.flags = extensions
275	p.refs = make(map[string]*reference)
276	p.maxNesting = 16
277	p.insideLink = false
278
279	// register inline parsers
280	p.inlineCallback['*'] = emphasis
281	p.inlineCallback['_'] = emphasis
282	if extensions&EXTENSION_STRIKETHROUGH != 0 {
283		p.inlineCallback['~'] = emphasis
284	}
285	p.inlineCallback['`'] = codeSpan
286	p.inlineCallback['\n'] = lineBreak
287	p.inlineCallback['['] = link
288	p.inlineCallback['<'] = leftAngle
289	p.inlineCallback['\\'] = escape
290	p.inlineCallback['&'] = entity
291
292	if extensions&EXTENSION_AUTOLINK != 0 {
293		p.inlineCallback[':'] = autoLink
294	}
295
296	if extensions&EXTENSION_FOOTNOTES != 0 {
297		p.notes = make([]*reference, 0)
298	}
299
300	first := firstPass(p, input)
301	second := secondPass(p, first)
302	return second
303}
304
305// first pass:
306// - extract references
307// - expand tabs
308// - normalize newlines
309// - copy everything else
310// - add missing newlines before fenced code blocks
311func firstPass(p *parser, input []byte) []byte {
312	var out bytes.Buffer
313	tabSize := TAB_SIZE_DEFAULT
314	if p.flags&EXTENSION_TAB_SIZE_EIGHT != 0 {
315		tabSize = TAB_SIZE_EIGHT
316	}
317	beg, end := 0, 0
318	lastLineWasBlank := false
319	lastFencedCodeBlockEnd := 0
320	for beg < len(input) { // iterate over lines
321		if end = isReference(p, input[beg:], tabSize); end > 0 {
322			beg += end
323		} else { // skip to the next line
324			end = beg
325			for end < len(input) && input[end] != '\n' && input[end] != '\r' {
326				end++
327			}
328
329			if p.flags&EXTENSION_FENCED_CODE != 0 {
330				// when last line was none blank and a fenced code block comes after
331				if beg >= lastFencedCodeBlockEnd {
332					if i := p.fencedCode(&out, input[beg:], 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}