all repos — grayfriday @ 6712f32cfd36fbe8ef4fc7088eb01a5b368ab07d

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(out *bytes.Buffer, 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(out, 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(out, 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&EXTENSION_TITLEBLOCK != 0 {
  67			if data[0] == '%' {
  68				if i := p.titleBlock(out, 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(out, 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&EXTENSION_FENCED_CODE != 0 {
 105			if i := p.fencedCode(out, 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(out)
 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(out, 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&EXTENSION_TABLES != 0 {
 143			if i := p.table(out, 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(out, 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(out, data, LIST_TYPE_ORDERED):]
 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&EXTENSION_DEFINITION_LISTS != 0 {
 178			if p.dliPrefix(data) > 0 {
 179				data = data[p.list(out, data, LIST_TYPE_DEFINITION):]
 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(out, 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&EXTENSION_SPACE_HEADERS != 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(out *bytes.Buffer, 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&EXTENSION_HEADER_IDS != 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&EXTENSION_AUTO_HEADER_IDS != 0 {
 246			id = sanitized_anchor_name.Create(string(data[i:end]))
 247		}
 248		work := func() bool {
 249			p.inline(out, data[i:end])
 250			return true
 251		}
 252		p.r.Header(out, work, level, id)
 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(out *bytes.Buffer, 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(out, data)
 298
 299	return len(data)
 300}
 301
 302func (p *parser) html(out *bytes.Buffer, 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(out, data, doRender); size > 0 {
 315			return size
 316		}
 317
 318		// check for an <hr> tag
 319		if size := p.htmlHr(out, 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(out, data[:end])
 395	}
 396
 397	return i
 398}
 399
 400// HTML comment, lax form
 401func (p *parser) htmlComment(out *bytes.Buffer, data []byte, doRender bool) int {
 402	i := p.inlineHtmlComment(out, 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(out, 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(out *bytes.Buffer, 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(out, 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 blockTags[key] {
 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&EXTENSION_LAX_HTML_BLOCKS != 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(out *bytes.Buffer, 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(out, work.Bytes(), syntax)
 678	}
 679
 680	return beg
 681}
 682
 683func (p *parser) table(out *bytes.Buffer, 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(out, 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] |= TABLE_ALIGNMENT_LEFT
 771			dashes++
 772		}
 773		for data[i] == '-' {
 774			i++
 775			dashes++
 776		}
 777		if data[i] == ':' {
 778			i++
 779			columns[col] |= TABLE_ALIGNMENT_RIGHT
 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		var cellWork bytes.Buffer
 856		p.inline(&cellWork, data[cellStart:cellEnd])
 857
 858		if header {
 859			p.r.TableHeaderCell(&rowWork, cellWork.Bytes(), columns[col])
 860		} else {
 861			p.r.TableCell(&rowWork, cellWork.Bytes(), columns[col])
 862		}
 863	}
 864
 865	// pad it out with empty columns to get the right number
 866	for ; col < len(columns); col++ {
 867		if header {
 868			p.r.TableHeaderCell(&rowWork, nil, columns[col])
 869		} else {
 870			p.r.TableCell(&rowWork, nil, columns[col])
 871		}
 872	}
 873
 874	// silently ignore rows with too many cells
 875
 876	p.r.TableRow(out, rowWork.Bytes())
 877}
 878
 879// returns blockquote prefix length
 880func (p *parser) quotePrefix(data []byte) int {
 881	i := 0
 882	for i < 3 && data[i] == ' ' {
 883		i++
 884	}
 885	if data[i] == '>' {
 886		if data[i+1] == ' ' {
 887			return i + 2
 888		}
 889		return i + 1
 890	}
 891	return 0
 892}
 893
 894// parse a blockquote fragment
 895func (p *parser) quote(out *bytes.Buffer, data []byte) int {
 896	var raw bytes.Buffer
 897	beg, end := 0, 0
 898	for beg < len(data) {
 899		end = beg
 900		for data[end] != '\n' {
 901			end++
 902		}
 903		end++
 904
 905		if pre := p.quotePrefix(data[beg:]); pre > 0 {
 906			// skip the prefix
 907			beg += pre
 908		} else if p.isEmpty(data[beg:]) > 0 &&
 909			(end >= len(data) ||
 910				(p.quotePrefix(data[end:]) == 0 && p.isEmpty(data[end:]) == 0)) {
 911			// blockquote ends with at least one blank line
 912			// followed by something without a blockquote prefix
 913			break
 914		}
 915
 916		// this line is part of the blockquote
 917		raw.Write(data[beg:end])
 918		beg = end
 919	}
 920
 921	var cooked bytes.Buffer
 922	p.block(&cooked, raw.Bytes())
 923	p.r.BlockQuote(out, cooked.Bytes())
 924	return end
 925}
 926
 927// returns prefix length for block code
 928func (p *parser) codePrefix(data []byte) int {
 929	if data[0] == ' ' && data[1] == ' ' && data[2] == ' ' && data[3] == ' ' {
 930		return 4
 931	}
 932	return 0
 933}
 934
 935func (p *parser) code(out *bytes.Buffer, data []byte) int {
 936	var work bytes.Buffer
 937
 938	i := 0
 939	for i < len(data) {
 940		beg := i
 941		for data[i] != '\n' {
 942			i++
 943		}
 944		i++
 945
 946		blankline := p.isEmpty(data[beg:i]) > 0
 947		if pre := p.codePrefix(data[beg:i]); pre > 0 {
 948			beg += pre
 949		} else if !blankline {
 950			// non-empty, non-prefixed line breaks the pre
 951			i = beg
 952			break
 953		}
 954
 955		// verbatim copy to the working buffeu
 956		if blankline {
 957			work.WriteByte('\n')
 958		} else {
 959			work.Write(data[beg:i])
 960		}
 961	}
 962
 963	// trim all the \n off the end of work
 964	workbytes := work.Bytes()
 965	eol := len(workbytes)
 966	for eol > 0 && workbytes[eol-1] == '\n' {
 967		eol--
 968	}
 969	if eol != len(workbytes) {
 970		work.Truncate(eol)
 971	}
 972
 973	work.WriteByte('\n')
 974
 975	p.r.BlockCode(out, work.Bytes(), "")
 976
 977	return i
 978}
 979
 980// returns unordered list item prefix
 981func (p *parser) uliPrefix(data []byte) int {
 982	i := 0
 983
 984	// start with up to 3 spaces
 985	for i < 3 && data[i] == ' ' {
 986		i++
 987	}
 988
 989	// need a *, +, or - followed by a space
 990	if (data[i] != '*' && data[i] != '+' && data[i] != '-') ||
 991		data[i+1] != ' ' {
 992		return 0
 993	}
 994	return i + 2
 995}
 996
 997// returns ordered list item prefix
 998func (p *parser) oliPrefix(data []byte) int {
 999	i := 0
1000
1001	// start with up to 3 spaces
1002	for i < 3 && data[i] == ' ' {
1003		i++
1004	}
1005
1006	// count the digits
1007	start := i
1008	for data[i] >= '0' && data[i] <= '9' {
1009		i++
1010	}
1011
1012	// we need >= 1 digits followed by a dot and a space
1013	if start == i || data[i] != '.' || data[i+1] != ' ' {
1014		return 0
1015	}
1016	return i + 2
1017}
1018
1019// returns definition list item prefix
1020func (p *parser) dliPrefix(data []byte) int {
1021	i := 0
1022
1023	// need a : followed by a spaces
1024	if data[i] != ':' || data[i+1] != ' ' {
1025		return 0
1026	}
1027	for data[i] == ' ' {
1028		i++
1029	}
1030	return i + 2
1031}
1032
1033// parse ordered or unordered list block
1034func (p *parser) list(out *bytes.Buffer, data []byte, flags int) int {
1035	i := 0
1036	flags |= LIST_ITEM_BEGINNING_OF_LIST
1037	work := func() bool {
1038		for i < len(data) {
1039			skip := p.listItem(out, data[i:], &flags)
1040			i += skip
1041
1042			if skip == 0 || flags&LIST_ITEM_END_OF_LIST != 0 {
1043				break
1044			}
1045			flags &= ^LIST_ITEM_BEGINNING_OF_LIST
1046		}
1047		return true
1048	}
1049
1050	p.r.List(out, work, flags)
1051	return i
1052}
1053
1054// Parse a single list item.
1055// Assumes initial prefix is already removed if this is a sublist.
1056func (p *parser) listItem(out *bytes.Buffer, data []byte, flags *int) int {
1057	// keep track of the indentation of the first line
1058	itemIndent := 0
1059	for itemIndent < 3 && data[itemIndent] == ' ' {
1060		itemIndent++
1061	}
1062
1063	i := p.uliPrefix(data)
1064	if i == 0 {
1065		i = p.oliPrefix(data)
1066	}
1067	if i == 0 {
1068		i = p.dliPrefix(data)
1069		// reset definition term flag
1070		if i > 0 {
1071			*flags &= ^LIST_TYPE_TERM
1072		}
1073	}
1074	if i == 0 {
1075		// if in defnition list, set term flag and continue
1076		if *flags&LIST_TYPE_DEFINITION != 0 {
1077			*flags |= LIST_TYPE_TERM
1078		} else {
1079			return 0
1080		}
1081	}
1082
1083	// skip leading whitespace on first line
1084	for data[i] == ' ' {
1085		i++
1086	}
1087
1088	// find the end of the line
1089	line := i
1090	for i > 0 && data[i-1] != '\n' {
1091		i++
1092	}
1093
1094	// get working buffer
1095	var raw bytes.Buffer
1096
1097	// put the first line into the working buffer
1098	raw.Write(data[line:i])
1099	line = i
1100
1101	// process the following lines
1102	containsBlankLine := false
1103	sublist := 0
1104
1105gatherlines:
1106	for line < len(data) {
1107		i++
1108
1109		// find the end of this line
1110		for data[i-1] != '\n' {
1111			i++
1112		}
1113
1114		// if it is an empty line, guess that it is part of this item
1115		// and move on to the next line
1116		if p.isEmpty(data[line:i]) > 0 {
1117			containsBlankLine = true
1118			line = i
1119			continue
1120		}
1121
1122		// calculate the indentation
1123		indent := 0
1124		for indent < 4 && line+indent < i && data[line+indent] == ' ' {
1125			indent++
1126		}
1127
1128		chunk := data[line+indent : i]
1129
1130		// evaluate how this line fits in
1131		switch {
1132		// is this a nested list item?
1133		case (p.uliPrefix(chunk) > 0 && !p.isHRule(chunk)) ||
1134			p.oliPrefix(chunk) > 0 ||
1135			p.dliPrefix(chunk) > 0:
1136
1137			if containsBlankLine {
1138				*flags |= LIST_ITEM_CONTAINS_BLOCK
1139			}
1140
1141			// to be a nested list, it must be indented more
1142			// if not, it is the next item in the same list
1143			if indent <= itemIndent {
1144				break gatherlines
1145			}
1146
1147			// is this the first item in the nested list?
1148			if sublist == 0 {
1149				sublist = raw.Len()
1150			}
1151
1152		// is this a nested prefix header?
1153		case p.isPrefixHeader(chunk):
1154			// if the header is not indented, it is not nested in the list
1155			// and thus ends the list
1156			if containsBlankLine && indent < 4 {
1157				*flags |= LIST_ITEM_END_OF_LIST
1158				break gatherlines
1159			}
1160			*flags |= LIST_ITEM_CONTAINS_BLOCK
1161
1162		// anything following an empty line is only part
1163		// of this item if it is indented 4 spaces
1164		// (regardless of the indentation of the beginning of the item)
1165		case containsBlankLine && indent < 4:
1166			if *flags&LIST_TYPE_DEFINITION != 0 && i < len(data)-1 {
1167				// is the next item still a part of this list?
1168				next := i
1169				for data[next] != '\n' {
1170					next++
1171				}
1172				for next < len(data)-1 && data[next] == '\n' {
1173					next++
1174				}
1175				if i < len(data)-1 && data[i] != ':' && data[next] != ':' {
1176					*flags |= LIST_ITEM_END_OF_LIST
1177				}
1178			} else {
1179				*flags |= LIST_ITEM_END_OF_LIST
1180			}
1181			break gatherlines
1182
1183		// a blank line means this should be parsed as a block
1184		case containsBlankLine:
1185			raw.WriteByte('\n')
1186			*flags |= LIST_ITEM_CONTAINS_BLOCK
1187		}
1188
1189		// if this line was preceeded by one or more blanks,
1190		// re-introduce the blank into the buffer
1191		if containsBlankLine {
1192			containsBlankLine = false
1193			raw.WriteByte('\n')
1194
1195		}
1196
1197		// add the line into the working buffer without prefix
1198		raw.Write(data[line+indent : i])
1199
1200		line = i
1201	}
1202
1203	rawBytes := raw.Bytes()
1204
1205	// render the contents of the list item
1206	var cooked bytes.Buffer
1207	if *flags&LIST_ITEM_CONTAINS_BLOCK != 0 && *flags&LIST_TYPE_TERM == 0 {
1208		// intermediate render of block item, except for definition term
1209		if sublist > 0 {
1210			p.block(&cooked, rawBytes[:sublist])
1211			p.block(&cooked, rawBytes[sublist:])
1212		} else {
1213			p.block(&cooked, rawBytes)
1214		}
1215	} else {
1216		// intermediate render of inline item
1217		if sublist > 0 {
1218			p.inline(&cooked, rawBytes[:sublist])
1219			p.block(&cooked, rawBytes[sublist:])
1220		} else {
1221			p.inline(&cooked, rawBytes)
1222		}
1223	}
1224
1225	// render the actual list item
1226	cookedBytes := cooked.Bytes()
1227	parsedEnd := len(cookedBytes)
1228
1229	// strip trailing newlines
1230	for parsedEnd > 0 && cookedBytes[parsedEnd-1] == '\n' {
1231		parsedEnd--
1232	}
1233	p.r.ListItem(out, cookedBytes[:parsedEnd], *flags)
1234
1235	return line
1236}
1237
1238// render a single paragraph that has already been parsed out
1239func (p *parser) renderParagraph(out *bytes.Buffer, data []byte) {
1240	if len(data) == 0 {
1241		return
1242	}
1243
1244	// trim leading spaces
1245	beg := 0
1246	for data[beg] == ' ' {
1247		beg++
1248	}
1249
1250	// trim trailing newline
1251	end := len(data) - 1
1252
1253	// trim trailing spaces
1254	for end > beg && data[end-1] == ' ' {
1255		end--
1256	}
1257
1258	work := func() bool {
1259		p.inline(out, data[beg:end])
1260		return true
1261	}
1262	p.r.Paragraph(out, work)
1263}
1264
1265func (p *parser) paragraph(out *bytes.Buffer, data []byte) int {
1266	// prev: index of 1st char of previous line
1267	// line: index of 1st char of current line
1268	// i: index of cursor/end of current line
1269	var prev, line, i int
1270
1271	// keep going until we find something to mark the end of the paragraph
1272	for i < len(data) {
1273		// mark the beginning of the current line
1274		prev = line
1275		current := data[i:]
1276		line = i
1277
1278		// did we find a blank line marking the end of the paragraph?
1279		if n := p.isEmpty(current); n > 0 {
1280			// did this blank line followed by a definition list item?
1281			if p.flags&EXTENSION_DEFINITION_LISTS != 0 {
1282				if i < len(data)-1 && data[i+1] == ':' {
1283					return p.list(out, data[prev:], LIST_TYPE_DEFINITION)
1284				}
1285			}
1286
1287			p.renderParagraph(out, data[:i])
1288			return i + n
1289		}
1290
1291		// an underline under some text marks a header, so our paragraph ended on prev line
1292		if i > 0 {
1293			if level := p.isUnderlinedHeader(current); level > 0 {
1294				// render the paragraph
1295				p.renderParagraph(out, data[:prev])
1296
1297				// ignore leading and trailing whitespace
1298				eol := i - 1
1299				for prev < eol && data[prev] == ' ' {
1300					prev++
1301				}
1302				for eol > prev && data[eol-1] == ' ' {
1303					eol--
1304				}
1305
1306				// render the header
1307				// this ugly double closure avoids forcing variables onto the heap
1308				work := func(o *bytes.Buffer, pp *parser, d []byte) func() bool {
1309					return func() bool {
1310						pp.inline(o, d)
1311						return true
1312					}
1313				}(out, p, data[prev:eol])
1314
1315				id := ""
1316				if p.flags&EXTENSION_AUTO_HEADER_IDS != 0 {
1317					id = sanitized_anchor_name.Create(string(data[prev:eol]))
1318				}
1319
1320				p.r.Header(out, work, level, id)
1321
1322				// find the end of the underline
1323				for data[i] != '\n' {
1324					i++
1325				}
1326				return i
1327			}
1328		}
1329
1330		// if the next line starts a block of HTML, then the paragraph ends here
1331		if p.flags&EXTENSION_LAX_HTML_BLOCKS != 0 {
1332			if data[i] == '<' && p.html(out, current, false) > 0 {
1333				// rewind to before the HTML block
1334				p.renderParagraph(out, data[:i])
1335				return i
1336			}
1337		}
1338
1339		// if there's a prefixed header or a horizontal rule after this, paragraph is over
1340		if p.isPrefixHeader(current) || p.isHRule(current) {
1341			p.renderParagraph(out, data[:i])
1342			return i
1343		}
1344
1345		// if there's a definition list item, prev line is a definition term
1346		if p.flags&EXTENSION_DEFINITION_LISTS != 0 {
1347			if p.dliPrefix(current) != 0 {
1348				return p.list(out, data[prev:], LIST_TYPE_DEFINITION)
1349			}
1350		}
1351
1352		// if there's a list after this, paragraph is over
1353		if p.flags&EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK != 0 {
1354			if p.uliPrefix(current) != 0 ||
1355				p.oliPrefix(current) != 0 ||
1356				p.quotePrefix(current) != 0 ||
1357				p.codePrefix(current) != 0 {
1358				p.renderParagraph(out, data[:i])
1359				return i
1360			}
1361		}
1362
1363		// otherwise, scan to the beginning of the next line
1364		for data[i] != '\n' {
1365			i++
1366		}
1367		i++
1368	}
1369
1370	p.renderParagraph(out, data[:i])
1371	return i
1372}