all repos — grayfriday @ 97235182ac5e768200740f8050b75e07bb97a43f

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