all repos — grayfriday @ 643477a0516ad3565177aa8ac8371e0eb4c824f9

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