all repos — grayfriday @ cb288d6b5d02f1a7be20cf6efbdb63d4164b8e67

blackfriday fork with a few changes

block.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// Functions to parse block-level elements.
  12//
  13
  14package blackfriday
  15
  16import (
  17	"bytes"
  18)
  19
  20// Parse block-level data.
  21// Note: this function and many that it calls assume that
  22// the input buffer ends with a newline.
  23func (p *parser) block(out *bytes.Buffer, data []byte) {
  24	if len(data) == 0 || data[len(data)-1] != '\n' {
  25		panic("block input is missing terminating newline")
  26	}
  27
  28	// this is called recursively: enforce a maximum depth
  29	if p.nesting >= p.maxNesting {
  30		return
  31	}
  32	p.nesting++
  33
  34	// parse out one block-level construct at a time
  35	for len(data) > 0 {
  36		// prefixed header:
  37		//
  38		// # Header 1
  39		// ## Header 2
  40		// ...
  41		// ###### Header 6
  42		if p.isPrefixHeader(data) {
  43			data = data[p.prefixHeader(out, data):]
  44			continue
  45		}
  46
  47		// block of preformatted HTML:
  48		//
  49		// <div>
  50		//     ...
  51		// </div>
  52		if data[0] == '<' {
  53			if i := p.html(out, data, true); i > 0 {
  54				data = data[i:]
  55				continue
  56			}
  57		}
  58
  59		// blank lines.  note: returns the # of bytes to skip
  60		if i := p.isEmpty(data); i > 0 {
  61			data = data[i:]
  62			continue
  63		}
  64
  65		// indented code block:
  66		//
  67		//     func max(a, b int) int {
  68		//         if a > b {
  69		//             return a
  70		//         }
  71		//         return b
  72		//      }
  73		if p.codePrefix(data) > 0 {
  74			data = data[p.code(out, data):]
  75			continue
  76		}
  77
  78		// fenced code block:
  79		//
  80		// ``` go
  81		// func fact(n int) int {
  82		//     if n <= 1 {
  83		//         return n
  84		//     }
  85		//     return n * fact(n-1)
  86		// }
  87		// ```
  88		if p.flags&EXTENSION_FENCED_CODE != 0 {
  89			if i := p.fencedCode(out, data, true); i > 0 {
  90				data = data[i:]
  91				continue
  92			}
  93		}
  94
  95		// horizontal rule:
  96		//
  97		// ------
  98		// or
  99		// ******
 100		// or
 101		// ______
 102		if p.isHRule(data) {
 103			p.r.HRule(out)
 104			var i int
 105			for i = 0; data[i] != '\n'; i++ {
 106			}
 107			data = data[i:]
 108			continue
 109		}
 110
 111		// block quote:
 112		//
 113		// > A big quote I found somewhere
 114		// > on the web
 115		if p.quotePrefix(data) > 0 {
 116			data = data[p.quote(out, data):]
 117			continue
 118		}
 119
 120		// table:
 121		//
 122		// Name  | Age | Phone
 123		// ------|-----|---------
 124		// Bob   | 31  | 555-1234
 125		// Alice | 27  | 555-4321
 126		if p.flags&EXTENSION_TABLES != 0 {
 127			if i := p.table(out, data); i > 0 {
 128				data = data[i:]
 129				continue
 130			}
 131		}
 132
 133		// an itemized/unordered list:
 134		//
 135		// * Item 1
 136		// * Item 2
 137		//
 138		// also works with + or -
 139		if p.uliPrefix(data) > 0 {
 140			data = data[p.list(out, data, 0):]
 141			continue
 142		}
 143
 144		// a numbered/ordered list:
 145		//
 146		// 1. Item 1
 147		// 2. Item 2
 148		if p.oliPrefix(data) > 0 {
 149			data = data[p.list(out, data, LIST_TYPE_ORDERED):]
 150			continue
 151		}
 152
 153		// anything else must look like a normal paragraph
 154		// note: this finds underlined headers, too
 155		data = data[p.paragraph(out, data):]
 156	}
 157
 158	p.nesting--
 159}
 160
 161func (p *parser) isPrefixHeader(data []byte) bool {
 162	if data[0] != '#' {
 163		return false
 164	}
 165
 166	if p.flags&EXTENSION_SPACE_HEADERS != 0 {
 167		level := 0
 168		for level < 6 && data[level] == '#' {
 169			level++
 170		}
 171		if data[level] != ' ' {
 172			return false
 173		}
 174	}
 175	return true
 176}
 177
 178func (p *parser) prefixHeader(out *bytes.Buffer, data []byte) int {
 179	level := 0
 180	for level < 6 && data[level] == '#' {
 181		level++
 182	}
 183	i, end := 0, 0
 184	for i = level; data[i] == ' '; i++ {
 185	}
 186	for end = i; data[end] != '\n'; end++ {
 187	}
 188	skip := end
 189	for end > 0 && data[end-1] == '#' {
 190		end--
 191	}
 192	for end > 0 && data[end-1] == ' ' {
 193		end--
 194	}
 195	if end > i {
 196		work := func() bool {
 197			p.inline(out, data[i:end])
 198			return true
 199		}
 200		p.r.Header(out, work, level)
 201	}
 202	return skip
 203}
 204
 205func (p *parser) isUnderlinedHeader(data []byte) int {
 206	// test of level 1 header
 207	if data[0] == '=' {
 208		i := 1
 209		for data[i] == '=' {
 210			i++
 211		}
 212		for data[i] == ' ' {
 213			i++
 214		}
 215		if data[i] == '\n' {
 216			return 1
 217		} else {
 218			return 0
 219		}
 220	}
 221
 222	// test of level 2 header
 223	if data[0] == '-' {
 224		i := 1
 225		for data[i] == '-' {
 226			i++
 227		}
 228		for data[i] == ' ' {
 229			i++
 230		}
 231		if data[i] == '\n' {
 232			return 2
 233		} else {
 234			return 0
 235		}
 236	}
 237
 238	return 0
 239}
 240
 241func (p *parser) html(out *bytes.Buffer, data []byte, doRender bool) int {
 242	var i, j int
 243
 244	// identify the opening tag
 245	if data[0] != '<' {
 246		return 0
 247	}
 248	curtag, tagfound := p.htmlFindTag(data[1:])
 249
 250	// handle special cases
 251	if !tagfound {
 252		// check for an HTML comment
 253		if size := p.htmlComment(out, data, doRender); size > 0 {
 254			return size
 255		}
 256
 257		// check for an <hr> tag
 258		if size := p.htmlHr(out, data, doRender); size > 0 {
 259			return size
 260		}
 261
 262		// no special case recognized
 263		return 0
 264	}
 265
 266	// look for an unindented matching closing tag
 267	// followed by a blank line
 268	found := false
 269	/*
 270		closetag := []byte("\n</" + curtag + ">")
 271		j = len(curtag) + 1
 272		for !found {
 273			// scan for a closing tag at the beginning of a line
 274			if skip := bytes.Index(data[j:], closetag); skip >= 0 {
 275				j += skip + len(closetag)
 276			} else {
 277				break
 278			}
 279
 280			// see if it is the only thing on the line
 281			if skip := p.isEmpty(data[j:]); skip > 0 {
 282				// see if it is followed by a blank line/eof
 283				j += skip
 284				if j >= len(data) {
 285					found = true
 286					i = j
 287				} else {
 288					if skip := p.isEmpty(data[j:]); skip > 0 {
 289						j += skip
 290						found = true
 291						i = j
 292					}
 293				}
 294			}
 295		}
 296	*/
 297
 298	// if not found, try a second pass looking for indented match
 299	// but not if tag is "ins" or "del" (following original Markdown.pl)
 300	if !found && curtag != "ins" && curtag != "del" {
 301		i = 1
 302		for i < len(data) {
 303			i++
 304			for i < len(data) && !(data[i-1] == '<' && data[i] == '/') {
 305				i++
 306			}
 307
 308			if i+2+len(curtag) >= len(data) {
 309				break
 310			}
 311
 312			j = p.htmlFindEnd(curtag, data[i-1:])
 313
 314			if j > 0 {
 315				i += j - 1
 316				found = true
 317				break
 318			}
 319		}
 320	}
 321
 322	if !found {
 323		return 0
 324	}
 325
 326	// the end of the block has been found
 327	if doRender {
 328		// trim newlines
 329		end := i
 330		for end > 0 && data[end-1] == '\n' {
 331			end--
 332		}
 333		p.r.BlockHtml(out, data[:end])
 334	}
 335
 336	return i
 337}
 338
 339// HTML comment, lax form
 340func (p *parser) htmlComment(out *bytes.Buffer, data []byte, doRender bool) int {
 341	if data[0] != '<' || data[1] != '!' || data[2] != '-' || data[3] != '-' {
 342		return 0
 343	}
 344
 345	i := 5
 346
 347	// scan for an end-of-comment marker, across lines if necessary
 348	for i < len(data) && !(data[i-2] == '-' && data[i-1] == '-' && data[i] == '>') {
 349		i++
 350	}
 351	i++
 352
 353	// no end-of-comment marker
 354	if i >= len(data) {
 355		return 0
 356	}
 357
 358	// needs to end with a blank line
 359	if j := p.isEmpty(data[i:]); j > 0 {
 360		size := i + j
 361		if doRender {
 362			// trim trailing newlines
 363			end := size
 364			for end > 0 && data[end-1] == '\n' {
 365				end--
 366			}
 367			p.r.BlockHtml(out, data[:end])
 368		}
 369		return size
 370	}
 371
 372	return 0
 373}
 374
 375// HR, which is the only self-closing block tag considered
 376func (p *parser) htmlHr(out *bytes.Buffer, data []byte, doRender bool) int {
 377	if data[0] != '<' || (data[1] != 'h' && data[1] != 'H') || (data[2] != 'r' && data[2] != 'R') {
 378		return 0
 379	}
 380	if data[3] != ' ' && data[3] != '/' && data[3] != '>' {
 381		// not an <hr> tag after all; at least not a valid one
 382		return 0
 383	}
 384
 385	i := 3
 386	for data[i] != '>' && data[i] != '\n' {
 387		i++
 388	}
 389
 390	if data[i] == '>' {
 391		i++
 392		if j := p.isEmpty(data[i:]); j > 0 {
 393			size := i + j
 394			if doRender {
 395				// trim newlines
 396				end := size
 397				for end > 0 && data[end-1] == '\n' {
 398					end--
 399				}
 400				p.r.BlockHtml(out, data[:end])
 401			}
 402			return size
 403		}
 404	}
 405
 406	return 0
 407}
 408
 409func (p *parser) htmlFindTag(data []byte) (string, bool) {
 410	i := 0
 411	for isalnum(data[i]) {
 412		i++
 413	}
 414	key := string(data[:i])
 415	if blockTags[key] {
 416		return key, true
 417	}
 418	return "", false
 419}
 420
 421func (p *parser) htmlFindEnd(tag string, data []byte) int {
 422	// assume data[0] == '<' && data[1] == '/' already tested
 423
 424	// check if tag is a match
 425	closetag := []byte("</" + tag + ">")
 426	if !bytes.HasPrefix(data, closetag) {
 427		return 0
 428	}
 429	i := len(closetag)
 430
 431	// check that the rest of the line is blank
 432	skip := 0
 433	if skip = p.isEmpty(data[i:]); skip == 0 {
 434		return 0
 435	}
 436	i += skip
 437	skip = 0
 438
 439	if i >= len(data) {
 440		return i
 441	}
 442
 443	if p.flags&EXTENSION_LAX_HTML_BLOCKS != 0 {
 444		return i
 445	}
 446	if skip = p.isEmpty(data[i:]); skip == 0 {
 447		// following line must be blank
 448		return 0
 449	}
 450
 451	return i + skip
 452}
 453
 454func (p *parser) isEmpty(data []byte) int {
 455	// it is okay to call isEmpty on an empty buffer
 456	if len(data) == 0 {
 457		return 0
 458	}
 459
 460	var i int
 461	for i = 0; i < len(data) && data[i] != '\n'; i++ {
 462		if data[i] != ' ' && data[i] != '\t' {
 463			return 0
 464		}
 465	}
 466	return i + 1
 467}
 468
 469func (p *parser) isHRule(data []byte) bool {
 470	i := 0
 471
 472	// skip up to three spaces
 473	for i < 3 && data[i] == ' ' {
 474		i++
 475	}
 476
 477	// look at the hrule char
 478	if data[i] != '*' && data[i] != '-' && data[i] != '_' {
 479		return false
 480	}
 481	c := data[i]
 482
 483	// the whole line must be the char or whitespace
 484	n := 0
 485	for data[i] != '\n' {
 486		switch {
 487		case data[i] == c:
 488			n++
 489		case data[i] != ' ':
 490			return false
 491		}
 492		i++
 493	}
 494
 495	return n >= 3
 496}
 497
 498func (p *parser) isFencedCode(data []byte, syntax **string, oldmarker string) (skip int, marker string) {
 499	i, size := 0, 0
 500	skip = 0
 501
 502	// skip up to three spaces
 503	for i < 3 && data[i] == ' ' {
 504		i++
 505	}
 506
 507	// check for the marker characters: ~ or `
 508	if data[i] != '~' && data[i] != '`' {
 509		return
 510	}
 511
 512	c := data[i]
 513
 514	// the whole line must be the same char or whitespace
 515	for data[i] == c {
 516		size++
 517		i++
 518	}
 519
 520	// the marker char must occur at least 3 times
 521	if size < 3 {
 522		return
 523	}
 524	marker = string(data[i-size : i])
 525
 526	// if this is the end marker, it must match the beginning marker
 527	if oldmarker != "" && marker != oldmarker {
 528		return
 529	}
 530
 531	if syntax != nil {
 532		syn := 0
 533
 534		for data[i] == ' ' {
 535			i++
 536		}
 537
 538		syntaxStart := i
 539
 540		if data[i] == '{' {
 541			i++
 542			syntaxStart++
 543
 544			for data[i] != '}' && data[i] != '\n' {
 545				syn++
 546				i++
 547			}
 548
 549			if data[i] != '}' {
 550				return
 551			}
 552
 553			// strip all whitespace at the beginning and the end
 554			// of the {} block
 555			for syn > 0 && isspace(data[syntaxStart]) {
 556				syntaxStart++
 557				syn--
 558			}
 559
 560			for syn > 0 && isspace(data[syntaxStart+syn-1]) {
 561				syn--
 562			}
 563
 564			i++
 565		} else {
 566			for !isspace(data[i]) {
 567				syn++
 568				i++
 569			}
 570		}
 571
 572		language := string(data[syntaxStart : syntaxStart+syn])
 573		*syntax = &language
 574	}
 575
 576	for data[i] == ' ' {
 577		i++
 578	}
 579	if data[i] != '\n' {
 580		return
 581	}
 582
 583	skip = i + 1
 584	return
 585}
 586
 587func (p *parser) fencedCode(out *bytes.Buffer, data []byte, doRender bool) int {
 588	var lang *string
 589	beg, marker := p.isFencedCode(data, &lang, "")
 590	if beg == 0 || beg >= len(data) {
 591		return 0
 592	}
 593
 594	var work bytes.Buffer
 595
 596	for {
 597		// safe to assume beg < len(data)
 598
 599		// check for the end of the code block
 600		fenceEnd, _ := p.isFencedCode(data[beg:], nil, marker)
 601		if fenceEnd != 0 {
 602			beg += fenceEnd
 603			break
 604		}
 605
 606		// copy the current line
 607		end := beg
 608		for data[end] != '\n' {
 609			end++
 610		}
 611		end++
 612
 613		// did we reach the end of the buffer without a closing marker?
 614		if end >= len(data) {
 615			return 0
 616		}
 617
 618		// verbatim copy to the working buffer
 619		if doRender {
 620			work.Write(data[beg:end])
 621		}
 622		beg = end
 623	}
 624
 625	syntax := ""
 626	if lang != nil {
 627		syntax = *lang
 628	}
 629
 630	if doRender {
 631		p.r.BlockCode(out, work.Bytes(), syntax)
 632	}
 633
 634	return beg
 635}
 636
 637func (p *parser) table(out *bytes.Buffer, data []byte) int {
 638	var header bytes.Buffer
 639	i, columns := p.tableHeader(&header, data)
 640	if i == 0 {
 641		return 0
 642	}
 643
 644	var body bytes.Buffer
 645
 646	for i < len(data) {
 647		pipes, rowStart := 0, i
 648		for ; data[i] != '\n'; i++ {
 649			if data[i] == '|' {
 650				pipes++
 651			}
 652		}
 653
 654		if pipes == 0 {
 655			i = rowStart
 656			break
 657		}
 658
 659		// include the newline in data sent to tableRow
 660		i++
 661		p.tableRow(&body, data[rowStart:i], columns, false)
 662	}
 663
 664	p.r.Table(out, header.Bytes(), body.Bytes(), columns)
 665
 666	return i
 667}
 668
 669// check if the specified position is preceeded by an odd number of backslashes
 670func isBackslashEscaped(data []byte, i int) bool {
 671	backslashes := 0
 672	for i-backslashes-1 >= 0 && data[i-backslashes-1] == '\\' {
 673		backslashes++
 674	}
 675	return backslashes&1 == 1
 676}
 677
 678func (p *parser) tableHeader(out *bytes.Buffer, data []byte) (size int, columns []int) {
 679	i := 0
 680	colCount := 1
 681	for i = 0; data[i] != '\n'; i++ {
 682		if data[i] == '|' && !isBackslashEscaped(data, i) {
 683			colCount++
 684		}
 685	}
 686
 687	// doesn't look like a table header
 688	if colCount == 1 {
 689		return
 690	}
 691
 692	// include the newline in the data sent to tableRow
 693	header := data[:i+1]
 694
 695	// column count ignores pipes at beginning or end of line
 696	if data[0] == '|' {
 697		colCount--
 698	}
 699	if i > 2 && data[i-1] == '|' && !isBackslashEscaped(data, i-1) {
 700		colCount--
 701	}
 702
 703	columns = make([]int, colCount)
 704
 705	// move on to the header underline
 706	i++
 707	if i >= len(data) {
 708		return
 709	}
 710
 711	if data[i] == '|' && !isBackslashEscaped(data, i) {
 712		i++
 713	}
 714	for data[i] == ' ' {
 715		i++
 716	}
 717
 718	// each column header is of form: / *:?-+:? *|/ with # dashes + # colons >= 3
 719	// and trailing | optional on last column
 720	col := 0
 721	for data[i] != '\n' {
 722		dashes := 0
 723
 724		if data[i] == ':' {
 725			i++
 726			columns[col] |= TABLE_ALIGNMENT_LEFT
 727			dashes++
 728		}
 729		for data[i] == '-' {
 730			i++
 731			dashes++
 732		}
 733		if data[i] == ':' {
 734			i++
 735			columns[col] |= TABLE_ALIGNMENT_RIGHT
 736			dashes++
 737		}
 738		for data[i] == ' ' {
 739			i++
 740		}
 741
 742		// end of column test is messy
 743		switch {
 744		case dashes < 3:
 745			// not a valid column
 746			return
 747
 748		case data[i] == '|' && !isBackslashEscaped(data, i):
 749			// marker found, now skip past trailing whitespace
 750			col++
 751			i++
 752			for data[i] == ' ' {
 753				i++
 754			}
 755
 756			// trailing junk found after last column
 757			if col >= colCount && data[i] != '\n' {
 758				return
 759			}
 760
 761		case (data[i] != '|' || isBackslashEscaped(data, i)) && col+1 < colCount:
 762			// something else found where marker was required
 763			return
 764
 765		case data[i] == '\n':
 766			// marker is optional for the last column
 767			col++
 768
 769		default:
 770			// trailing junk found after last column
 771			return
 772		}
 773	}
 774	if col != colCount {
 775		return
 776	}
 777
 778	p.tableRow(out, header, columns, true)
 779	size = i + 1
 780	return
 781}
 782
 783func (p *parser) tableRow(out *bytes.Buffer, data []byte, columns []int, header bool) {
 784	i, col := 0, 0
 785	var rowWork bytes.Buffer
 786
 787	if data[i] == '|' && !isBackslashEscaped(data, i) {
 788		i++
 789	}
 790
 791	for col = 0; col < len(columns) && i < len(data); col++ {
 792		for data[i] == ' ' {
 793			i++
 794		}
 795
 796		cellStart := i
 797
 798		for (data[i] != '|' || isBackslashEscaped(data, i)) && data[i] != '\n' {
 799			i++
 800		}
 801
 802		cellEnd := i
 803
 804		// skip the end-of-cell marker, possibly taking us past end of buffer
 805		i++
 806
 807		for cellEnd > cellStart && data[cellEnd-1] == ' ' {
 808			cellEnd--
 809		}
 810
 811		var cellWork bytes.Buffer
 812		p.inline(&cellWork, data[cellStart:cellEnd])
 813
 814		if header {
 815			p.r.TableHeaderCell(&rowWork, cellWork.Bytes(), columns[col])
 816		} else {
 817			p.r.TableCell(&rowWork, cellWork.Bytes(), columns[col])
 818		}
 819	}
 820
 821	// pad it out with empty columns to get the right number
 822	for ; col < len(columns); col++ {
 823		if header {
 824			p.r.TableHeaderCell(&rowWork, nil, columns[col])
 825		} else {
 826			p.r.TableCell(&rowWork, nil, columns[col])
 827		}
 828	}
 829
 830	// silently ignore rows with too many cells
 831
 832	p.r.TableRow(out, rowWork.Bytes())
 833}
 834
 835// returns blockquote prefix length
 836func (p *parser) quotePrefix(data []byte) int {
 837	i := 0
 838	for i < 3 && data[i] == ' ' {
 839		i++
 840	}
 841	if data[i] == '>' {
 842		if data[i+1] == ' ' {
 843			return i + 2
 844		}
 845		return i + 1
 846	}
 847	return 0
 848}
 849
 850// parse a blockquote fragment
 851func (p *parser) quote(out *bytes.Buffer, data []byte) int {
 852	var raw bytes.Buffer
 853	beg, end := 0, 0
 854	for beg < len(data) {
 855		end = beg
 856		for data[end] != '\n' {
 857			end++
 858		}
 859		end++
 860
 861		if pre := p.quotePrefix(data[beg:]); pre > 0 {
 862			// skip the prefix
 863			beg += pre
 864		} else if p.isEmpty(data[beg:]) > 0 &&
 865			(end >= len(data) ||
 866				(p.quotePrefix(data[end:]) == 0 && p.isEmpty(data[end:]) == 0)) {
 867			// blockquote ends with at least one blank line
 868			// followed by something without a blockquote prefix
 869			break
 870		}
 871
 872		// this line is part of the blockquote
 873		raw.Write(data[beg:end])
 874		beg = end
 875	}
 876
 877	var cooked bytes.Buffer
 878	p.block(&cooked, raw.Bytes())
 879	p.r.BlockQuote(out, cooked.Bytes())
 880	return end
 881}
 882
 883// returns prefix length for block code
 884func (p *parser) codePrefix(data []byte) int {
 885	if data[0] == ' ' && data[1] == ' ' && data[2] == ' ' && data[3] == ' ' {
 886		return 4
 887	}
 888	return 0
 889}
 890
 891func (p *parser) code(out *bytes.Buffer, data []byte) int {
 892	var work bytes.Buffer
 893
 894	i := 0
 895	for i < len(data) {
 896		beg := i
 897		for data[i] != '\n' {
 898			i++
 899		}
 900		i++
 901
 902		blankline := p.isEmpty(data[beg:i]) > 0
 903		if pre := p.codePrefix(data[beg:i]); pre > 0 {
 904			beg += pre
 905		} else if !blankline {
 906			// non-empty, non-prefixed line breaks the pre
 907			i = beg
 908			break
 909		}
 910
 911		// verbatim copy to the working buffeu
 912		if blankline {
 913			work.WriteByte('\n')
 914		} else {
 915			work.Write(data[beg:i])
 916		}
 917	}
 918
 919	// trim all the \n off the end of work
 920	workbytes := work.Bytes()
 921	eol := len(workbytes)
 922	for eol > 0 && workbytes[eol-1] == '\n' {
 923		eol--
 924	}
 925	if eol != len(workbytes) {
 926		work.Truncate(eol)
 927	}
 928
 929	work.WriteByte('\n')
 930
 931	p.r.BlockCode(out, work.Bytes(), "")
 932
 933	return i
 934}
 935
 936// returns unordered list item prefix
 937func (p *parser) uliPrefix(data []byte) int {
 938	i := 0
 939
 940	// start with up to 3 spaces
 941	for i < 3 && data[i] == ' ' {
 942		i++
 943	}
 944
 945	// need a *, +, or - followed by a space
 946	if (data[i] != '*' && data[i] != '+' && data[i] != '-') ||
 947		data[i+1] != ' ' {
 948		return 0
 949	}
 950	return i + 2
 951}
 952
 953// returns ordered list item prefix
 954func (p *parser) oliPrefix(data []byte) int {
 955	i := 0
 956
 957	// start with up to 3 spaces
 958	for i < 3 && data[i] == ' ' {
 959		i++
 960	}
 961
 962	// count the digits
 963	start := i
 964	for data[i] >= '0' && data[i] <= '9' {
 965		i++
 966	}
 967
 968	// we need >= 1 digits followed by a dot and a space
 969	if start == i || data[i] != '.' || data[i+1] != ' ' {
 970		return 0
 971	}
 972	return i + 2
 973}
 974
 975// parse ordered or unordered list block
 976func (p *parser) list(out *bytes.Buffer, data []byte, flags int) int {
 977	i := 0
 978	flags |= LIST_ITEM_BEGINNING_OF_LIST
 979	work := func() bool {
 980		for i < len(data) {
 981			skip := p.listItem(out, data[i:], &flags)
 982			i += skip
 983
 984			if skip == 0 || flags&LIST_ITEM_END_OF_LIST != 0 {
 985				break
 986			}
 987			flags &= ^LIST_ITEM_BEGINNING_OF_LIST
 988		}
 989		return true
 990	}
 991
 992	p.r.List(out, work, flags)
 993	return i
 994}
 995
 996// Parse a single list item.
 997// Assumes initial prefix is already removed if this is a sublist.
 998func (p *parser) listItem(out *bytes.Buffer, data []byte, flags *int) int {
 999	// keep track of the indentation of the first line
1000	itemIndent := 0
1001	for itemIndent < 3 && data[itemIndent] == ' ' {
1002		itemIndent++
1003	}
1004
1005	i := p.uliPrefix(data)
1006	if i == 0 {
1007		i = p.oliPrefix(data)
1008	}
1009	if i == 0 {
1010		return 0
1011	}
1012
1013	// skip leading whitespace on first line
1014	for data[i] == ' ' {
1015		i++
1016	}
1017
1018	// find the end of the line
1019	line := i
1020	for data[i-1] != '\n' {
1021		i++
1022	}
1023
1024	// get working buffer
1025	var raw bytes.Buffer
1026
1027	// put the first line into the working buffer
1028	raw.Write(data[line:i])
1029	line = i
1030
1031	// process the following lines
1032	containsBlankLine := false
1033	sublist := 0
1034
1035gatherlines:
1036	for line < len(data) {
1037		i++
1038
1039		// find the end of this line
1040		for data[i-1] != '\n' {
1041			i++
1042		}
1043
1044		// if it is an empty line, guess that it is part of this item
1045		// and move on to the next line
1046		if p.isEmpty(data[line:i]) > 0 {
1047			containsBlankLine = true
1048			line = i
1049			continue
1050		}
1051
1052		// calculate the indentation
1053		indent := 0
1054		for indent < 4 && line+indent < i && data[line+indent] == ' ' {
1055			indent++
1056		}
1057
1058		chunk := data[line+indent : i]
1059
1060		// evaluate how this line fits in
1061		switch {
1062		// is this a nested list item?
1063		case (p.uliPrefix(chunk) > 0 && !p.isHRule(chunk)) ||
1064			p.oliPrefix(chunk) > 0:
1065
1066			if containsBlankLine {
1067				*flags |= LIST_ITEM_CONTAINS_BLOCK
1068			}
1069
1070			// to be a nested list, it must be indented more
1071			// if not, it is the next item in the same list
1072			if indent <= itemIndent {
1073				break gatherlines
1074			}
1075
1076			// is this the first item in the the nested list?
1077			if sublist == 0 {
1078				sublist = raw.Len()
1079			}
1080
1081		// is this a nested prefix header?
1082		case p.isPrefixHeader(chunk):
1083			// if the header is not indented, it is not nested in the list
1084			// and thus ends the list
1085			if containsBlankLine && indent < 4 {
1086				*flags |= LIST_ITEM_END_OF_LIST
1087				break gatherlines
1088			}
1089			*flags |= LIST_ITEM_CONTAINS_BLOCK
1090
1091		// anything following an empty line is only part
1092		// of this item if it is indented 4 spaces
1093		// (regardless of the indentation of the beginning of the item)
1094		case containsBlankLine && indent < 4:
1095			*flags |= LIST_ITEM_END_OF_LIST
1096			break gatherlines
1097
1098		// a blank line means this should be parsed as a block
1099		case containsBlankLine:
1100			raw.WriteByte('\n')
1101			*flags |= LIST_ITEM_CONTAINS_BLOCK
1102		}
1103
1104		// if this line was preceeded by one or more blanks,
1105		// re-introduce the blank into the buffer
1106		if containsBlankLine {
1107			containsBlankLine = false
1108			raw.WriteByte('\n')
1109		}
1110
1111		// add the line into the working buffer without prefix
1112		raw.Write(data[line+indent : i])
1113
1114		line = i
1115	}
1116
1117	rawBytes := raw.Bytes()
1118
1119	// render the contents of the list item
1120	var cooked bytes.Buffer
1121	if *flags&LIST_ITEM_CONTAINS_BLOCK != 0 {
1122		// intermediate render of block li
1123		if sublist > 0 {
1124			p.block(&cooked, rawBytes[:sublist])
1125			p.block(&cooked, rawBytes[sublist:])
1126		} else {
1127			p.block(&cooked, rawBytes)
1128		}
1129	} else {
1130		// intermediate render of inline li
1131		if sublist > 0 {
1132			p.inline(&cooked, rawBytes[:sublist])
1133			p.block(&cooked, rawBytes[sublist:])
1134		} else {
1135			p.inline(&cooked, rawBytes)
1136		}
1137	}
1138
1139	// render the actual list item
1140	cookedBytes := cooked.Bytes()
1141	parsedEnd := len(cookedBytes)
1142
1143	// strip trailing newlines
1144	for parsedEnd > 0 && cookedBytes[parsedEnd-1] == '\n' {
1145		parsedEnd--
1146	}
1147	p.r.ListItem(out, cookedBytes[:parsedEnd], *flags)
1148
1149	return line
1150}
1151
1152// render a single paragraph that has already been parsed out
1153func (p *parser) renderParagraph(out *bytes.Buffer, data []byte) {
1154	if len(data) == 0 {
1155		return
1156	}
1157
1158	// trim leading spaces
1159	beg := 0
1160	for data[beg] == ' ' {
1161		beg++
1162	}
1163
1164	// trim trailing newline
1165	end := len(data) - 1
1166
1167	// trim trailing spaces
1168	for end > beg && data[end-1] == ' ' {
1169		end--
1170	}
1171
1172	work := func() bool {
1173		p.inline(out, data[beg:end])
1174		return true
1175	}
1176	p.r.Paragraph(out, work)
1177}
1178
1179func (p *parser) paragraph(out *bytes.Buffer, data []byte) int {
1180	// prev: index of 1st char of previous line
1181	// line: index of 1st char of current line
1182	// i: index of cursor/end of current line
1183	var prev, line, i int
1184
1185	// keep going until we find something to mark the end of the paragraph
1186	for i < len(data) {
1187		// mark the beginning of the current line
1188		prev = line
1189		current := data[i:]
1190		line = i
1191
1192		// did we find a blank line marking the end of the paragraph?
1193		if n := p.isEmpty(current); n > 0 {
1194			p.renderParagraph(out, data[:i])
1195			return i + n
1196		}
1197
1198		// an underline under some text marks a header, so our paragraph ended on prev line
1199		if i > 0 {
1200			if level := p.isUnderlinedHeader(current); level > 0 {
1201				// render the paragraph
1202				p.renderParagraph(out, data[:prev])
1203
1204				// ignore leading and trailing whitespace
1205				eol := i - 1
1206				for prev < eol && data[prev] == ' ' {
1207					prev++
1208				}
1209				for eol > prev && data[eol-1] == ' ' {
1210					eol--
1211				}
1212
1213				// render the header
1214				// this ugly double closure avoids forcing variables onto the heap
1215				work := func(o *bytes.Buffer, pp *parser, d []byte) func() bool {
1216					return func() bool {
1217						pp.inline(o, d)
1218						return true
1219					}
1220				}(out, p, data[prev:eol])
1221				p.r.Header(out, work, level)
1222
1223				// find the end of the underline
1224				for data[i] != '\n' {
1225					i++
1226				}
1227				return i
1228			}
1229		}
1230
1231		// if the next line starts a block of HTML, then the paragraph ends here
1232		if p.flags&EXTENSION_LAX_HTML_BLOCKS != 0 {
1233			if data[i] == '<' && p.html(out, current, false) > 0 {
1234				// rewind to before the HTML block
1235				p.renderParagraph(out, data[:i])
1236				return i
1237			}
1238		}
1239
1240		// if there's a prefixed header or a horizontal rule after this, paragraph is over
1241		if p.isPrefixHeader(current) || p.isHRule(current) {
1242			p.renderParagraph(out, data[:i])
1243			return i
1244		}
1245
1246		// if there's a list after this, paragraph is over
1247		if p.flags&EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK != 0 {
1248			if p.uliPrefix(current) != 0 ||
1249				p.oliPrefix(current) != 0 ||
1250				p.quotePrefix(current) != 0 ||
1251				p.codePrefix(current) != 0 {
1252				p.renderParagraph(out, data[:i])
1253				return i
1254			}
1255		}
1256
1257		// otherwise, scan to the beginning of the next line
1258		for data[i] != '\n' {
1259			i++
1260		}
1261		i++
1262	}
1263
1264	p.renderParagraph(out, data[:i])
1265	return i
1266}