all repos — grayfriday @ 386ef80f18233ea97960e855a54382ec446c6637

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