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