all repos — grayfriday @ f4655604b394cf7f941cbebea9bbf0e3ed9afd88

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