all repos — grayfriday @ 43ba539936f4225f156c648585840b7c9b0866c2

blackfriday fork with a few changes

inline.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 inline elements.
  12//
  13
  14package blackfriday
  15
  16import (
  17	"bytes"
  18	"regexp"
  19	"strconv"
  20)
  21
  22var (
  23	urlRe    = `((https?|ftp):\/\/|\/)[-A-Za-z0-9+&@#\/%?=~_|!:,.;\(\)]+`
  24	anchorRe = regexp.MustCompile(`^(<a\shref="` + urlRe + `"(\stitle="[^"<>]+")?\s?>` + urlRe + `<\/a>)`)
  25
  26	// TODO: improve this regexp to catch all possible entities:
  27	htmlEntityRe = regexp.MustCompile(`&[a-z]{2,5};`)
  28)
  29
  30// Functions to parse text within a block
  31// Each function returns the number of chars taken care of
  32// data is the complete block being rendered
  33// offset is the number of valid chars before the current cursor
  34
  35func (p *parser) inline(currBlock *Node, data []byte) {
  36	// this is called recursively: enforce a maximum depth
  37	if p.nesting >= p.maxNesting {
  38		return
  39	}
  40	p.nesting++
  41
  42	i, end := 0, 0
  43	for i < len(data) {
  44		// Stop at EOL
  45		if data[i] == '\n' && i+1 == len(data) {
  46			break
  47		}
  48
  49		for ; end < len(data); end++ {
  50			if p.inlineCallback[data[end]] != nil {
  51				break
  52			}
  53		}
  54
  55		if end >= len(data) {
  56			if data[end-1] == '\n' {
  57				currBlock.AppendChild(text(data[i : end-1]))
  58			} else {
  59				currBlock.AppendChild(text(data[i:end]))
  60			}
  61			break
  62		}
  63
  64		// call the trigger
  65		handler := p.inlineCallback[data[end]]
  66		if consumed, node := handler(p, data, end); consumed == 0 {
  67			// No action from the callback.
  68			end++
  69		} else {
  70			// Copy inactive chars into the output.
  71			currBlock.AppendChild(text(data[i:end]))
  72			if node != nil {
  73				currBlock.AppendChild(node)
  74			}
  75			// Skip past whatever the callback used.
  76			i = end + consumed
  77			end = i
  78		}
  79	}
  80
  81	p.nesting--
  82}
  83
  84// single and double emphasis parsing
  85func emphasis(p *parser, data []byte, offset int) (int, *Node) {
  86	data = data[offset:]
  87	c := data[0]
  88
  89	if len(data) > 2 && data[1] != c {
  90		// whitespace cannot follow an opening emphasis;
  91		// strikethrough only takes two characters '~~'
  92		if c == '~' || isspace(data[1]) {
  93			return 0, nil
  94		}
  95		ret, node := helperEmphasis(p, data[1:], c)
  96		if ret == 0 {
  97			return 0, nil
  98		}
  99
 100		return ret + 1, node
 101	}
 102
 103	if len(data) > 3 && data[1] == c && data[2] != c {
 104		if isspace(data[2]) {
 105			return 0, nil
 106		}
 107		ret, node := helperDoubleEmphasis(p, data[2:], c)
 108		if ret == 0 {
 109			return 0, nil
 110		}
 111
 112		return ret + 2, node
 113	}
 114
 115	if len(data) > 4 && data[1] == c && data[2] == c && data[3] != c {
 116		if c == '~' || isspace(data[3]) {
 117			return 0, nil
 118		}
 119		ret, node := helperTripleEmphasis(p, data, 3, c)
 120		if ret == 0 {
 121			return 0, nil
 122		}
 123
 124		return ret + 3, node
 125	}
 126
 127	return 0, nil
 128}
 129
 130func codeSpan(p *parser, data []byte, offset int) (int, *Node) {
 131	data = data[offset:]
 132
 133	nb := 0
 134
 135	// count the number of backticks in the delimiter
 136	for nb < len(data) && data[nb] == '`' {
 137		nb++
 138	}
 139
 140	// find the next delimiter
 141	i, end := 0, 0
 142	for end = nb; end < len(data) && i < nb; end++ {
 143		if data[end] == '`' {
 144			i++
 145		} else {
 146			i = 0
 147		}
 148	}
 149
 150	// no matching delimiter?
 151	if i < nb && end >= len(data) {
 152		return 0, nil
 153	}
 154
 155	// trim outside whitespace
 156	fBegin := nb
 157	for fBegin < end && data[fBegin] == ' ' {
 158		fBegin++
 159	}
 160
 161	fEnd := end - nb
 162	for fEnd > fBegin && data[fEnd-1] == ' ' {
 163		fEnd--
 164	}
 165
 166	// render the code span
 167	if fBegin != fEnd {
 168		code := NewNode(Code)
 169		code.Literal = data[fBegin:fEnd]
 170		return end, code
 171	}
 172
 173	return end, nil
 174}
 175
 176// newline preceded by two spaces becomes <br>
 177func maybeLineBreak(p *parser, data []byte, offset int) (int, *Node) {
 178	origOffset := offset
 179	for offset < len(data) && data[offset] == ' ' {
 180		offset++
 181	}
 182
 183	if offset < len(data) && data[offset] == '\n' {
 184		if offset-origOffset >= 2 {
 185			return offset - origOffset + 1, NewNode(Hardbreak)
 186		}
 187		return offset - origOffset, nil
 188	}
 189	return 0, nil
 190}
 191
 192// newline without two spaces works when HardLineBreak is enabled
 193func lineBreak(p *parser, data []byte, offset int) (int, *Node) {
 194	if p.flags&HardLineBreak != 0 {
 195		return 1, NewNode(Hardbreak)
 196	}
 197	return 0, nil
 198}
 199
 200type linkType int
 201
 202const (
 203	linkNormal linkType = iota
 204	linkImg
 205	linkDeferredFootnote
 206	linkInlineFootnote
 207)
 208
 209func isReferenceStyleLink(data []byte, pos int, t linkType) bool {
 210	if t == linkDeferredFootnote {
 211		return false
 212	}
 213	return pos < len(data)-1 && data[pos] == '[' && data[pos+1] != '^'
 214}
 215
 216func maybeImage(p *parser, data []byte, offset int) (int, *Node) {
 217	if offset < len(data)-1 && data[offset+1] == '[' {
 218		return link(p, data, offset)
 219	}
 220	return 0, nil
 221}
 222
 223func maybeInlineFootnote(p *parser, data []byte, offset int) (int, *Node) {
 224	if offset < len(data)-1 && data[offset+1] == '[' {
 225		return link(p, data, offset)
 226	}
 227	return 0, nil
 228}
 229
 230// '[': parse a link or an image or a footnote
 231func link(p *parser, data []byte, offset int) (int, *Node) {
 232	// no links allowed inside regular links, footnote, and deferred footnotes
 233	if p.insideLink && (offset > 0 && data[offset-1] == '[' || len(data)-1 > offset && data[offset+1] == '^') {
 234		return 0, nil
 235	}
 236
 237	var t linkType
 238	switch {
 239	// special case: ![^text] == deferred footnote (that follows something with
 240	// an exclamation point)
 241	case p.flags&Footnotes != 0 && len(data)-1 > offset && data[offset+1] == '^':
 242		t = linkDeferredFootnote
 243	// ![alt] == image
 244	case offset >= 0 && data[offset] == '!':
 245		t = linkImg
 246		offset++
 247	// ^[text] == inline footnote
 248	// [^refId] == deferred footnote
 249	case p.flags&Footnotes != 0:
 250		if offset >= 0 && data[offset] == '^' {
 251			t = linkInlineFootnote
 252			offset++
 253		} else if len(data)-1 > offset && data[offset+1] == '^' {
 254			t = linkDeferredFootnote
 255		}
 256	// [text] == regular link
 257	default:
 258		t = linkNormal
 259	}
 260
 261	data = data[offset:]
 262
 263	var (
 264		i                       = 1
 265		noteID                  int
 266		title, link, altContent []byte
 267		textHasNl               = false
 268	)
 269
 270	if t == linkDeferredFootnote {
 271		i++
 272	}
 273
 274	// look for the matching closing bracket
 275	for level := 1; level > 0 && i < len(data); i++ {
 276		switch {
 277		case data[i] == '\n':
 278			textHasNl = true
 279
 280		case data[i-1] == '\\':
 281			continue
 282
 283		case data[i] == '[':
 284			level++
 285
 286		case data[i] == ']':
 287			level--
 288			if level <= 0 {
 289				i-- // compensate for extra i++ in for loop
 290			}
 291		}
 292	}
 293
 294	if i >= len(data) {
 295		return 0, nil
 296	}
 297
 298	txtE := i
 299	i++
 300
 301	// skip any amount of whitespace or newline
 302	// (this is much more lax than original markdown syntax)
 303	for i < len(data) && isspace(data[i]) {
 304		i++
 305	}
 306
 307	// inline style link
 308	switch {
 309	case i < len(data) && data[i] == '(':
 310		// skip initial whitespace
 311		i++
 312
 313		for i < len(data) && isspace(data[i]) {
 314			i++
 315		}
 316
 317		linkB := i
 318
 319		// look for link end: ' " )
 320	findlinkend:
 321		for i < len(data) {
 322			switch {
 323			case data[i] == '\\':
 324				i += 2
 325
 326			case data[i] == ')' || data[i] == '\'' || data[i] == '"':
 327				break findlinkend
 328
 329			default:
 330				i++
 331			}
 332		}
 333
 334		if i >= len(data) {
 335			return 0, nil
 336		}
 337		linkE := i
 338
 339		// look for title end if present
 340		titleB, titleE := 0, 0
 341		if data[i] == '\'' || data[i] == '"' {
 342			i++
 343			titleB = i
 344
 345		findtitleend:
 346			for i < len(data) {
 347				switch {
 348				case data[i] == '\\':
 349					i += 2
 350
 351				case data[i] == ')':
 352					break findtitleend
 353
 354				default:
 355					i++
 356				}
 357			}
 358
 359			if i >= len(data) {
 360				return 0, nil
 361			}
 362
 363			// skip whitespace after title
 364			titleE = i - 1
 365			for titleE > titleB && isspace(data[titleE]) {
 366				titleE--
 367			}
 368
 369			// check for closing quote presence
 370			if data[titleE] != '\'' && data[titleE] != '"' {
 371				titleB, titleE = 0, 0
 372				linkE = i
 373			}
 374		}
 375
 376		// remove whitespace at the end of the link
 377		for linkE > linkB && isspace(data[linkE-1]) {
 378			linkE--
 379		}
 380
 381		// remove optional angle brackets around the link
 382		if data[linkB] == '<' {
 383			linkB++
 384		}
 385		if data[linkE-1] == '>' {
 386			linkE--
 387		}
 388
 389		// build escaped link and title
 390		if linkE > linkB {
 391			link = data[linkB:linkE]
 392		}
 393
 394		if titleE > titleB {
 395			title = data[titleB:titleE]
 396		}
 397
 398		i++
 399
 400	// reference style link
 401	case isReferenceStyleLink(data, i, t):
 402		var id []byte
 403		altContentConsidered := false
 404
 405		// look for the id
 406		i++
 407		linkB := i
 408		for i < len(data) && data[i] != ']' {
 409			i++
 410		}
 411		if i >= len(data) {
 412			return 0, nil
 413		}
 414		linkE := i
 415
 416		// find the reference
 417		if linkB == linkE {
 418			if textHasNl {
 419				var b bytes.Buffer
 420
 421				for j := 1; j < txtE; j++ {
 422					switch {
 423					case data[j] != '\n':
 424						b.WriteByte(data[j])
 425					case data[j-1] != ' ':
 426						b.WriteByte(' ')
 427					}
 428				}
 429
 430				id = b.Bytes()
 431			} else {
 432				id = data[1:txtE]
 433				altContentConsidered = true
 434			}
 435		} else {
 436			id = data[linkB:linkE]
 437		}
 438
 439		// find the reference with matching id
 440		lr, ok := p.getRef(string(id))
 441		if !ok {
 442			return 0, nil
 443		}
 444
 445		// keep link and title from reference
 446		link = lr.link
 447		title = lr.title
 448		if altContentConsidered {
 449			altContent = lr.text
 450		}
 451		i++
 452
 453	// shortcut reference style link or reference or inline footnote
 454	default:
 455		var id []byte
 456
 457		// craft the id
 458		if textHasNl {
 459			var b bytes.Buffer
 460
 461			for j := 1; j < txtE; j++ {
 462				switch {
 463				case data[j] != '\n':
 464					b.WriteByte(data[j])
 465				case data[j-1] != ' ':
 466					b.WriteByte(' ')
 467				}
 468			}
 469
 470			id = b.Bytes()
 471		} else {
 472			if t == linkDeferredFootnote {
 473				id = data[2:txtE] // get rid of the ^
 474			} else {
 475				id = data[1:txtE]
 476			}
 477		}
 478
 479		if t == linkInlineFootnote {
 480			// create a new reference
 481			noteID = len(p.notes) + 1
 482
 483			var fragment []byte
 484			if len(id) > 0 {
 485				if len(id) < 16 {
 486					fragment = make([]byte, len(id))
 487				} else {
 488					fragment = make([]byte, 16)
 489				}
 490				copy(fragment, slugify(id))
 491			} else {
 492				fragment = append([]byte("footnote-"), []byte(strconv.Itoa(noteID))...)
 493			}
 494
 495			ref := &reference{
 496				noteID:   noteID,
 497				hasBlock: false,
 498				link:     fragment,
 499				title:    id,
 500			}
 501
 502			p.notes = append(p.notes, ref)
 503
 504			link = ref.link
 505			title = ref.title
 506		} else {
 507			// find the reference with matching id
 508			lr, ok := p.getRef(string(id))
 509			if !ok {
 510				return 0, nil
 511			}
 512
 513			if t == linkDeferredFootnote {
 514				lr.noteID = len(p.notes) + 1
 515				p.notes = append(p.notes, lr)
 516			}
 517
 518			// keep link and title from reference
 519			link = lr.link
 520			// if inline footnote, title == footnote contents
 521			title = lr.title
 522			noteID = lr.noteID
 523		}
 524
 525		// rewind the whitespace
 526		i = txtE + 1
 527	}
 528
 529	var uLink []byte
 530	if t == linkNormal || t == linkImg {
 531		if len(link) > 0 {
 532			var uLinkBuf bytes.Buffer
 533			unescapeText(&uLinkBuf, link)
 534			uLink = uLinkBuf.Bytes()
 535		}
 536
 537		// links need something to click on and somewhere to go
 538		if len(uLink) == 0 || (t == linkNormal && txtE <= 1) {
 539			return 0, nil
 540		}
 541	}
 542
 543	// call the relevant rendering function
 544	var linkNode *Node
 545	switch t {
 546	case linkNormal:
 547		linkNode = NewNode(Link)
 548		linkNode.Destination = normalizeURI(uLink)
 549		linkNode.Title = title
 550		if len(altContent) > 0 {
 551			linkNode.AppendChild(text(altContent))
 552		} else {
 553			// links cannot contain other links, so turn off link parsing
 554			// temporarily and recurse
 555			insideLink := p.insideLink
 556			p.insideLink = true
 557			p.inline(linkNode, data[1:txtE])
 558			p.insideLink = insideLink
 559		}
 560
 561	case linkImg:
 562		linkNode = NewNode(Image)
 563		linkNode.Destination = uLink
 564		linkNode.Title = title
 565		linkNode.AppendChild(text(data[1:txtE]))
 566		i++
 567
 568	case linkInlineFootnote, linkDeferredFootnote:
 569		linkNode = NewNode(Link)
 570		linkNode.Destination = link
 571		linkNode.Title = title
 572		linkNode.NoteID = noteID
 573		if t == linkInlineFootnote {
 574			i++
 575		}
 576
 577	default:
 578		return 0, nil
 579	}
 580
 581	return i, linkNode
 582}
 583
 584func (p *parser) inlineHTMLComment(data []byte) int {
 585	if len(data) < 5 {
 586		return 0
 587	}
 588	if data[0] != '<' || data[1] != '!' || data[2] != '-' || data[3] != '-' {
 589		return 0
 590	}
 591	i := 5
 592	// scan for an end-of-comment marker, across lines if necessary
 593	for i < len(data) && !(data[i-2] == '-' && data[i-1] == '-' && data[i] == '>') {
 594		i++
 595	}
 596	// no end-of-comment marker
 597	if i >= len(data) {
 598		return 0
 599	}
 600	return i + 1
 601}
 602
 603func stripMailto(link []byte) []byte {
 604	if bytes.HasPrefix(link, []byte("mailto://")) {
 605		return link[9:]
 606	} else if bytes.HasPrefix(link, []byte("mailto:")) {
 607		return link[7:]
 608	} else {
 609		return link
 610	}
 611}
 612
 613// autolinkType specifies a kind of autolink that gets detected.
 614type autolinkType int
 615
 616// These are the possible flag values for the autolink renderer.
 617const (
 618	notAutolink autolinkType = iota
 619	normalAutolink
 620	emailAutolink
 621)
 622
 623// '<' when tags or autolinks are allowed
 624func leftAngle(p *parser, data []byte, offset int) (int, *Node) {
 625	data = data[offset:]
 626	altype, end := tagLength(data)
 627	if size := p.inlineHTMLComment(data); size > 0 {
 628		end = size
 629	}
 630	if end > 2 {
 631		if altype != notAutolink {
 632			var uLink bytes.Buffer
 633			unescapeText(&uLink, data[1:end+1-2])
 634			if uLink.Len() > 0 {
 635				link := uLink.Bytes()
 636				node := NewNode(Link)
 637				node.Destination = link
 638				if altype == emailAutolink {
 639					node.Destination = append([]byte("mailto:"), link...)
 640				}
 641				node.AppendChild(text(stripMailto(link)))
 642				return end, node
 643			}
 644		} else {
 645			htmlTag := NewNode(HTMLSpan)
 646			htmlTag.Literal = data[:end]
 647			return end, htmlTag
 648		}
 649	}
 650
 651	return end, nil
 652}
 653
 654// '\\' backslash escape
 655var escapeChars = []byte("\\`*_{}[]()#+-.!:|&<>~")
 656
 657func escape(p *parser, data []byte, offset int) (int, *Node) {
 658	data = data[offset:]
 659
 660	if len(data) > 1 {
 661		if p.flags&BackslashLineBreak != 0 && data[1] == '\n' {
 662			return 2, NewNode(Hardbreak)
 663		}
 664		if bytes.IndexByte(escapeChars, data[1]) < 0 {
 665			return 0, nil
 666		}
 667
 668		return 2, text(data[1:2])
 669	}
 670
 671	return 2, nil
 672}
 673
 674func unescapeText(ob *bytes.Buffer, src []byte) {
 675	i := 0
 676	for i < len(src) {
 677		org := i
 678		for i < len(src) && src[i] != '\\' {
 679			i++
 680		}
 681
 682		if i > org {
 683			ob.Write(src[org:i])
 684		}
 685
 686		if i+1 >= len(src) {
 687			break
 688		}
 689
 690		ob.WriteByte(src[i+1])
 691		i += 2
 692	}
 693}
 694
 695// '&' escaped when it doesn't belong to an entity
 696// valid entities are assumed to be anything matching &#?[A-Za-z0-9]+;
 697func entity(p *parser, data []byte, offset int) (int, *Node) {
 698	data = data[offset:]
 699
 700	end := 1
 701
 702	if end < len(data) && data[end] == '#' {
 703		end++
 704	}
 705
 706	for end < len(data) && isalnum(data[end]) {
 707		end++
 708	}
 709
 710	if end < len(data) && data[end] == ';' {
 711		end++ // real entity
 712	} else {
 713		return 0, nil // lone '&'
 714	}
 715
 716	ent := data[:end]
 717	// undo &amp; escaping or it will be converted to &amp;amp; by another
 718	// escaper in the renderer
 719	if bytes.Equal(ent, []byte("&amp;")) {
 720		ent = []byte{'&'}
 721	}
 722
 723	return end, text(ent)
 724}
 725
 726func linkEndsWithEntity(data []byte, linkEnd int) bool {
 727	entityRanges := htmlEntityRe.FindAllIndex(data[:linkEnd], -1)
 728	return entityRanges != nil && entityRanges[len(entityRanges)-1][1] == linkEnd
 729}
 730
 731func maybeAutoLink(p *parser, data []byte, offset int) (int, *Node) {
 732	// quick check to rule out most false hits
 733	if p.insideLink || len(data) < offset+6 { // 6 is the len() of the shortest prefix below
 734		return 0, nil
 735	}
 736	prefixes := []string{
 737		"http://",
 738		"https://",
 739		"ftp://",
 740		"file://",
 741		"mailto:",
 742	}
 743	for _, prefix := range prefixes {
 744		endOfHead := offset + 8 // 8 is the len() of the longest prefix
 745		if endOfHead > len(data) {
 746			endOfHead = len(data)
 747		}
 748		head := bytes.ToLower(data[offset:endOfHead])
 749		if bytes.HasPrefix(head, []byte(prefix)) {
 750			return autoLink(p, data, offset)
 751		}
 752	}
 753	return 0, nil
 754}
 755
 756func autoLink(p *parser, data []byte, offset int) (int, *Node) {
 757	// Now a more expensive check to see if we're not inside an anchor element
 758	anchorStart := offset
 759	offsetFromAnchor := 0
 760	for anchorStart > 0 && data[anchorStart] != '<' {
 761		anchorStart--
 762		offsetFromAnchor++
 763	}
 764
 765	anchorStr := anchorRe.Find(data[anchorStart:])
 766	if anchorStr != nil {
 767		anchorClose := NewNode(HTMLSpan)
 768		anchorClose.Literal = anchorStr[offsetFromAnchor:]
 769		return len(anchorStr) - offsetFromAnchor, anchorClose
 770	}
 771
 772	// scan backward for a word boundary
 773	rewind := 0
 774	for offset-rewind > 0 && rewind <= 7 && isletter(data[offset-rewind-1]) {
 775		rewind++
 776	}
 777	if rewind > 6 { // longest supported protocol is "mailto" which has 6 letters
 778		return 0, nil
 779	}
 780
 781	origData := data
 782	data = data[offset-rewind:]
 783
 784	if !isSafeLink(data) {
 785		return 0, nil
 786	}
 787
 788	linkEnd := 0
 789	for linkEnd < len(data) && !isEndOfLink(data[linkEnd]) {
 790		linkEnd++
 791	}
 792
 793	// Skip punctuation at the end of the link
 794	if (data[linkEnd-1] == '.' || data[linkEnd-1] == ',') && data[linkEnd-2] != '\\' {
 795		linkEnd--
 796	}
 797
 798	// But don't skip semicolon if it's a part of escaped entity:
 799	if data[linkEnd-1] == ';' && data[linkEnd-2] != '\\' && !linkEndsWithEntity(data, linkEnd) {
 800		linkEnd--
 801	}
 802
 803	// See if the link finishes with a punctuation sign that can be closed.
 804	var copen byte
 805	switch data[linkEnd-1] {
 806	case '"':
 807		copen = '"'
 808	case '\'':
 809		copen = '\''
 810	case ')':
 811		copen = '('
 812	case ']':
 813		copen = '['
 814	case '}':
 815		copen = '{'
 816	default:
 817		copen = 0
 818	}
 819
 820	if copen != 0 {
 821		bufEnd := offset - rewind + linkEnd - 2
 822
 823		openDelim := 1
 824
 825		/* Try to close the final punctuation sign in this same line;
 826		 * if we managed to close it outside of the URL, that means that it's
 827		 * not part of the URL. If it closes inside the URL, that means it
 828		 * is part of the URL.
 829		 *
 830		 * Examples:
 831		 *
 832		 *      foo http://www.pokemon.com/Pikachu_(Electric) bar
 833		 *              => http://www.pokemon.com/Pikachu_(Electric)
 834		 *
 835		 *      foo (http://www.pokemon.com/Pikachu_(Electric)) bar
 836		 *              => http://www.pokemon.com/Pikachu_(Electric)
 837		 *
 838		 *      foo http://www.pokemon.com/Pikachu_(Electric)) bar
 839		 *              => http://www.pokemon.com/Pikachu_(Electric))
 840		 *
 841		 *      (foo http://www.pokemon.com/Pikachu_(Electric)) bar
 842		 *              => foo http://www.pokemon.com/Pikachu_(Electric)
 843		 */
 844
 845		for bufEnd >= 0 && origData[bufEnd] != '\n' && openDelim != 0 {
 846			if origData[bufEnd] == data[linkEnd-1] {
 847				openDelim++
 848			}
 849
 850			if origData[bufEnd] == copen {
 851				openDelim--
 852			}
 853
 854			bufEnd--
 855		}
 856
 857		if openDelim == 0 {
 858			linkEnd--
 859		}
 860	}
 861
 862	var uLink bytes.Buffer
 863	unescapeText(&uLink, data[:linkEnd])
 864
 865	if uLink.Len() > 0 {
 866		node := NewNode(Link)
 867		node.Destination = uLink.Bytes()
 868		node.AppendChild(text(uLink.Bytes()))
 869		return linkEnd, node
 870	}
 871
 872	return linkEnd, nil
 873}
 874
 875func isEndOfLink(char byte) bool {
 876	return isspace(char) || char == '<'
 877}
 878
 879var validUris = [][]byte{[]byte("http://"), []byte("https://"), []byte("ftp://"), []byte("mailto://")}
 880var validPaths = [][]byte{[]byte("/"), []byte("./"), []byte("../")}
 881
 882func isSafeLink(link []byte) bool {
 883	for _, path := range validPaths {
 884		if len(link) >= len(path) && bytes.Equal(link[:len(path)], path) {
 885			if len(link) == len(path) {
 886				return true
 887			} else if isalnum(link[len(path)]) {
 888				return true
 889			}
 890		}
 891	}
 892
 893	for _, prefix := range validUris {
 894		// TODO: handle unicode here
 895		// case-insensitive prefix test
 896		if len(link) > len(prefix) && bytes.Equal(bytes.ToLower(link[:len(prefix)]), prefix) && isalnum(link[len(prefix)]) {
 897			return true
 898		}
 899	}
 900
 901	return false
 902}
 903
 904// return the length of the given tag, or 0 is it's not valid
 905func tagLength(data []byte) (autolink autolinkType, end int) {
 906	var i, j int
 907
 908	// a valid tag can't be shorter than 3 chars
 909	if len(data) < 3 {
 910		return notAutolink, 0
 911	}
 912
 913	// begins with a '<' optionally followed by '/', followed by letter or number
 914	if data[0] != '<' {
 915		return notAutolink, 0
 916	}
 917	if data[1] == '/' {
 918		i = 2
 919	} else {
 920		i = 1
 921	}
 922
 923	if !isalnum(data[i]) {
 924		return notAutolink, 0
 925	}
 926
 927	// scheme test
 928	autolink = notAutolink
 929
 930	// try to find the beginning of an URI
 931	for i < len(data) && (isalnum(data[i]) || data[i] == '.' || data[i] == '+' || data[i] == '-') {
 932		i++
 933	}
 934
 935	if i > 1 && i < len(data) && data[i] == '@' {
 936		if j = isMailtoAutoLink(data[i:]); j != 0 {
 937			return emailAutolink, i + j
 938		}
 939	}
 940
 941	if i > 2 && i < len(data) && data[i] == ':' {
 942		autolink = normalAutolink
 943		i++
 944	}
 945
 946	// complete autolink test: no whitespace or ' or "
 947	switch {
 948	case i >= len(data):
 949		autolink = notAutolink
 950	case autolink != notAutolink:
 951		j = i
 952
 953		for i < len(data) {
 954			if data[i] == '\\' {
 955				i += 2
 956			} else if data[i] == '>' || data[i] == '\'' || data[i] == '"' || isspace(data[i]) {
 957				break
 958			} else {
 959				i++
 960			}
 961
 962		}
 963
 964		if i >= len(data) {
 965			return autolink, 0
 966		}
 967		if i > j && data[i] == '>' {
 968			return autolink, i + 1
 969		}
 970
 971		// one of the forbidden chars has been found
 972		autolink = notAutolink
 973	}
 974	i += bytes.IndexByte(data[i:], '>')
 975	if i < 0 {
 976		return autolink, 0
 977	}
 978	return autolink, i + 1
 979}
 980
 981// look for the address part of a mail autolink and '>'
 982// this is less strict than the original markdown e-mail address matching
 983func isMailtoAutoLink(data []byte) int {
 984	nb := 0
 985
 986	// address is assumed to be: [-@._a-zA-Z0-9]+ with exactly one '@'
 987	for i := 0; i < len(data); i++ {
 988		if isalnum(data[i]) {
 989			continue
 990		}
 991
 992		switch data[i] {
 993		case '@':
 994			nb++
 995
 996		case '-', '.', '_':
 997			break
 998
 999		case '>':
1000			if nb == 1 {
1001				return i + 1
1002			}
1003			return 0
1004		default:
1005			return 0
1006		}
1007	}
1008
1009	return 0
1010}
1011
1012// look for the next emph char, skipping other constructs
1013func helperFindEmphChar(data []byte, c byte) int {
1014	i := 0
1015
1016	for i < len(data) {
1017		for i < len(data) && data[i] != c && data[i] != '`' && data[i] != '[' {
1018			i++
1019		}
1020		if i >= len(data) {
1021			return 0
1022		}
1023		// do not count escaped chars
1024		if i != 0 && data[i-1] == '\\' {
1025			i++
1026			continue
1027		}
1028		if data[i] == c {
1029			return i
1030		}
1031
1032		if data[i] == '`' {
1033			// skip a code span
1034			tmpI := 0
1035			i++
1036			for i < len(data) && data[i] != '`' {
1037				if tmpI == 0 && data[i] == c {
1038					tmpI = i
1039				}
1040				i++
1041			}
1042			if i >= len(data) {
1043				return tmpI
1044			}
1045			i++
1046		} else if data[i] == '[' {
1047			// skip a link
1048			tmpI := 0
1049			i++
1050			for i < len(data) && data[i] != ']' {
1051				if tmpI == 0 && data[i] == c {
1052					tmpI = i
1053				}
1054				i++
1055			}
1056			i++
1057			for i < len(data) && (data[i] == ' ' || data[i] == '\n') {
1058				i++
1059			}
1060			if i >= len(data) {
1061				return tmpI
1062			}
1063			if data[i] != '[' && data[i] != '(' { // not a link
1064				if tmpI > 0 {
1065					return tmpI
1066				}
1067				continue
1068			}
1069			cc := data[i]
1070			i++
1071			for i < len(data) && data[i] != cc {
1072				if tmpI == 0 && data[i] == c {
1073					return i
1074				}
1075				i++
1076			}
1077			if i >= len(data) {
1078				return tmpI
1079			}
1080			i++
1081		}
1082	}
1083	return 0
1084}
1085
1086func helperEmphasis(p *parser, data []byte, c byte) (int, *Node) {
1087	i := 0
1088
1089	// skip one symbol if coming from emph3
1090	if len(data) > 1 && data[0] == c && data[1] == c {
1091		i = 1
1092	}
1093
1094	for i < len(data) {
1095		length := helperFindEmphChar(data[i:], c)
1096		if length == 0 {
1097			return 0, nil
1098		}
1099		i += length
1100		if i >= len(data) {
1101			return 0, nil
1102		}
1103
1104		if i+1 < len(data) && data[i+1] == c {
1105			i++
1106			continue
1107		}
1108
1109		if data[i] == c && !isspace(data[i-1]) {
1110
1111			if p.flags&NoIntraEmphasis != 0 {
1112				if !(i+1 == len(data) || isspace(data[i+1]) || ispunct(data[i+1])) {
1113					continue
1114				}
1115			}
1116
1117			emph := NewNode(Emph)
1118			p.inline(emph, data[:i])
1119			return i + 1, emph
1120		}
1121	}
1122
1123	return 0, nil
1124}
1125
1126func helperDoubleEmphasis(p *parser, data []byte, c byte) (int, *Node) {
1127	i := 0
1128
1129	for i < len(data) {
1130		length := helperFindEmphChar(data[i:], c)
1131		if length == 0 {
1132			return 0, nil
1133		}
1134		i += length
1135
1136		if i+1 < len(data) && data[i] == c && data[i+1] == c && i > 0 && !isspace(data[i-1]) {
1137			nodeType := Strong
1138			if c == '~' {
1139				nodeType = Del
1140			}
1141			node := NewNode(nodeType)
1142			p.inline(node, data[:i])
1143			return i + 2, node
1144		}
1145		i++
1146	}
1147	return 0, nil
1148}
1149
1150func helperTripleEmphasis(p *parser, data []byte, offset int, c byte) (int, *Node) {
1151	i := 0
1152	origData := data
1153	data = data[offset:]
1154
1155	for i < len(data) {
1156		length := helperFindEmphChar(data[i:], c)
1157		if length == 0 {
1158			return 0, nil
1159		}
1160		i += length
1161
1162		// skip whitespace preceded symbols
1163		if data[i] != c || isspace(data[i-1]) {
1164			continue
1165		}
1166
1167		switch {
1168		case i+2 < len(data) && data[i+1] == c && data[i+2] == c:
1169			// triple symbol found
1170			strong := NewNode(Strong)
1171			em := NewNode(Emph)
1172			strong.AppendChild(em)
1173			p.inline(em, data[:i])
1174			return i + 3, strong
1175		case (i+1 < len(data) && data[i+1] == c):
1176			// double symbol found, hand over to emph1
1177			length, node := helperEmphasis(p, origData[offset-2:], c)
1178			if length == 0 {
1179				return 0, nil
1180			}
1181			return length - 2, node
1182		default:
1183			// single symbol found, hand over to emph2
1184			length, node := helperDoubleEmphasis(p, origData[offset-1:], c)
1185			if length == 0 {
1186				return 0, nil
1187			}
1188			return length - 1, node
1189		}
1190	}
1191	return 0, nil
1192}
1193
1194func text(s []byte) *Node {
1195	node := NewNode(Text)
1196	node.Literal = s
1197	return node
1198}
1199
1200func normalizeURI(s []byte) []byte {
1201	return s // TODO: implement
1202}