| 1 | package markdown |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "unicode" |
| 6 | "unicode/utf8" |
| 7 | ) |
| 8 | |
| 9 | // Inline content is parsed into a tree of nodes held in linked lists, so |
| 10 | // that wrapping a run of nodes in emphasis or a link takes constant time. |
| 11 | |
| 12 | type ikind uint8 |
| 13 | |
| 14 | const ( |
| 15 | itext ikind = iota |
| 16 | icode |
| 17 | isoft |
| 18 | ihard |
| 19 | iemph |
| 20 | istrong |
| 21 | idel |
| 22 | ilink |
| 23 | iimage |
| 24 | iautolink |
| 25 | ) |
| 26 | |
| 27 | type node struct { |
| 28 | // text is text, code, an autolink's text, or the delimiters of |
| 29 | // emphasis, strong emphasis or strikethrough. |
| 30 | text string |
| 31 | // link is a link's or an image's destination and title: a reference's |
| 32 | // definition, or one of its own. |
| 33 | link *linkRef |
| 34 | first, last *node |
| 35 | prev, next *node |
| 36 | kind ikind |
| 37 | // brk is the line break, isoft or ihard, that follows a text node, |
| 38 | // so that a break after text takes no node of its own. |
| 39 | brk ikind |
| 40 | delimiter, opener bool // a text node that holds a delimiter run or a bracket |
| 41 | mail bool // an autolink to an email address |
| 42 | } |
| 43 | |
| 44 | // dest is where an autolink goes. |
| 45 | func (n *node) dest() string { |
| 46 | if n.mail { |
| 47 | return "mailto:" + n.text |
| 48 | } |
| 49 | return n.text |
| 50 | } |
| 51 | |
| 52 | // delim is a run of *, _ or ~ that may open or close emphasis. |
| 53 | type delim struct { |
| 54 | n *node |
| 55 | c byte |
| 56 | count, orig int |
| 57 | canOpen, canClose bool |
| 58 | prev, next *delim |
| 59 | } |
| 60 | |
| 61 | // bracket is a [ or ![ that may open a link or an image. |
| 62 | type bracket struct { |
| 63 | n *node |
| 64 | image bool |
| 65 | pos int // where its text starts |
| 66 | delim *delim |
| 67 | } |
| 68 | |
| 69 | type inliner struct { |
| 70 | r *renderer |
| 71 | s string |
| 72 | root node |
| 73 | delims *delim // the last delimiter |
| 74 | brackets []bracket |
| 75 | floor int // brackets below floor may open no link |
| 76 | // The maximal runs of backticks, by length, and how far each length's |
| 77 | // list has been searched. |
| 78 | ticks map[int][]int |
| 79 | cursor map[int]int |
| 80 | } |
| 81 | |
| 82 | var special = func() (t [256]bool) { |
| 83 | for _, c := range []byte("\n\\`*_~[]!<&h") { |
| 84 | t[c] = true |
| 85 | } |
| 86 | return t |
| 87 | }() |
| 88 | |
| 89 | // parseInline parses s, or gives nil when the document's inline content |
| 90 | // takes more than MaxInlineNodes nodes with s's, or rendering stops. |
| 91 | func (r *renderer) parseInline(s string) *node { |
| 92 | if r.full { |
| 93 | return nil |
| 94 | } |
| 95 | in := &inliner{r: r, s: s} |
| 96 | in.run() |
| 97 | if r.stopped || r.full { |
| 98 | return nil |
| 99 | } |
| 100 | in.emphasis(nil) |
| 101 | if r.full { |
| 102 | return nil |
| 103 | } |
| 104 | return &in.root |
| 105 | } |
| 106 | |
| 107 | // count counts a new node against MaxInlineNodes. |
| 108 | func (in *inliner) count() { |
| 109 | if in.r.nodes++; in.r.nodes > MaxInlineNodes { |
| 110 | in.r.full = true |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | func (in *inliner) append(n *node) *node { |
| 115 | in.count() |
| 116 | if in.root.last == nil { |
| 117 | in.root.first = n |
| 118 | } else { |
| 119 | in.root.last.next = n |
| 120 | n.prev = in.root.last |
| 121 | } |
| 122 | in.root.last = n |
| 123 | return n |
| 124 | } |
| 125 | |
| 126 | func (in *inliner) remove(n *node) { |
| 127 | if n.prev != nil { |
| 128 | n.prev.next = n.next |
| 129 | } else { |
| 130 | in.root.first = n.next |
| 131 | } |
| 132 | if n.next != nil { |
| 133 | n.next.prev = n.prev |
| 134 | } else { |
| 135 | in.root.last = n.prev |
| 136 | } |
| 137 | n.prev, n.next = nil, nil |
| 138 | } |
| 139 | |
| 140 | // plainLast is the last node when it is text that no delimiter, bracket |
| 141 | // or break ends, and nil when it is not. |
| 142 | func (in *inliner) plainLast() *node { |
| 143 | if l := in.root.last; l != nil && l.kind == itext && !l.delimiter && !l.opener && l.brk == 0 { |
| 144 | return l |
| 145 | } |
| 146 | return nil |
| 147 | } |
| 148 | |
| 149 | // addBreak adds a line break: to the last node, when that is plain text, |
| 150 | // or as a node of its own. |
| 151 | func (in *inliner) addBreak(brk ikind) { |
| 152 | if l := in.plainLast(); l != nil { |
| 153 | l.brk = brk |
| 154 | return |
| 155 | } |
| 156 | in.append(&node{kind: brk}) |
| 157 | } |
| 158 | |
| 159 | func (in *inliner) addText(t string) { |
| 160 | if t == "" { |
| 161 | return |
| 162 | } |
| 163 | in.append(&node{kind: itext, text: t}) // never joined to the last, which would copy it again |
| 164 | |
| 165 | } |
| 166 | |
| 167 | func (in *inliner) run() { |
| 168 | s := in.s |
| 169 | for i := 0; i < len(s); { |
| 170 | if in.r.done() || in.r.full { |
| 171 | return |
| 172 | } |
| 173 | c := s[i] |
| 174 | if !special[c] { |
| 175 | j := i + 1 |
| 176 | for j < len(s) && !special[s[j]] { |
| 177 | j++ |
| 178 | } |
| 179 | in.addText(s[i:j]) |
| 180 | i = j |
| 181 | continue |
| 182 | } |
| 183 | switch c { |
| 184 | case '\n': |
| 185 | brk := isoft |
| 186 | if l := in.plainLast(); l != nil { |
| 187 | t := strings.TrimRight(l.text, " ") |
| 188 | if len(l.text)-len(t) >= 2 { |
| 189 | brk = ihard |
| 190 | } |
| 191 | l.text = t |
| 192 | } |
| 193 | in.addBreak(brk) |
| 194 | i++ |
| 195 | for i < len(s) && s[i] == ' ' { |
| 196 | i++ |
| 197 | } |
| 198 | case '\\': |
| 199 | switch { |
| 200 | case i+1 < len(s) && s[i+1] == '\n': |
| 201 | in.addBreak(ihard) |
| 202 | i += 2 |
| 203 | for i < len(s) && s[i] == ' ' { |
| 204 | i++ |
| 205 | } |
| 206 | case i+1 < len(s) && isPunct(s[i+1]): |
| 207 | in.addText(s[i+1 : i+2]) |
| 208 | i += 2 |
| 209 | default: |
| 210 | in.addText("\\") |
| 211 | i++ |
| 212 | } |
| 213 | case '`': |
| 214 | i = in.code(i) |
| 215 | case '*', '_', '~': |
| 216 | i = in.delimiter(i) |
| 217 | case '[': |
| 218 | // A [ past maxBrackets open is text. |
| 219 | if len(in.brackets) < maxBrackets { |
| 220 | in.open(bracket{n: in.append(&node{kind: itext, text: "[", opener: true}), pos: i + 1, delim: in.delims}) |
| 221 | } else { |
| 222 | in.addText("[") |
| 223 | } |
| 224 | i++ |
| 225 | case '!': |
| 226 | if i+1 < len(s) && s[i+1] == '[' && len(in.brackets) < maxBrackets { |
| 227 | in.open(bracket{n: in.append(&node{kind: itext, text: "![", opener: true}), image: true, pos: i + 2, delim: in.delims}) |
| 228 | i += 2 |
| 229 | } else { |
| 230 | in.addText("!") |
| 231 | i++ |
| 232 | } |
| 233 | case ']': |
| 234 | i = in.closeBracket(i) |
| 235 | case '<': |
| 236 | if n, end := autolink(s, i); end > 0 { |
| 237 | in.append(n) |
| 238 | i = end |
| 239 | } else { |
| 240 | in.addText("<") |
| 241 | i++ |
| 242 | } |
| 243 | case '&': |
| 244 | if t, n := entity(s[i:]); n > 0 { |
| 245 | in.addText(t) |
| 246 | i += n |
| 247 | } else { |
| 248 | in.addText("&") |
| 249 | i++ |
| 250 | } |
| 251 | case 'h': |
| 252 | if n, end := bareLink(s, i); end > 0 { |
| 253 | in.append(n) |
| 254 | i = end |
| 255 | } else { |
| 256 | in.addText("h") |
| 257 | i++ |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | // open adds a bracket that waits for its ]. |
| 264 | func (in *inliner) open(b bracket) { |
| 265 | in.brackets = append(in.brackets, b) |
| 266 | in.r.brackets = max(in.r.brackets, len(in.brackets)) |
| 267 | } |
| 268 | |
| 269 | // code reads a code span, or a run of backticks that opens none, at i. |
| 270 | func (in *inliner) code(i int) int { |
| 271 | s := in.s |
| 272 | n := run(s[i:], '`') |
| 273 | end := i + n |
| 274 | if in.ticks == nil { |
| 275 | in.ticks, in.cursor = map[int][]int{}, map[int]int{} |
| 276 | for j := 0; j < len(s); { |
| 277 | if s[j] != '`' { |
| 278 | j++ |
| 279 | continue |
| 280 | } |
| 281 | k := run(s[j:], '`') |
| 282 | in.ticks[k] = append(in.ticks[k], j) |
| 283 | j += k |
| 284 | } |
| 285 | } |
| 286 | list := in.ticks[n] |
| 287 | k := in.cursor[n] |
| 288 | for k < len(list) && list[k] < end { |
| 289 | k++ |
| 290 | } |
| 291 | in.cursor[n] = k |
| 292 | if k == len(list) { |
| 293 | in.addText(s[i:end]) |
| 294 | return end |
| 295 | } |
| 296 | at := list[k] |
| 297 | text := strings.ReplaceAll(s[end:at], "\n", " ") |
| 298 | if len(text) >= 2 && text[0] == ' ' && text[len(text)-1] == ' ' && strings.Trim(text, " ") != "" { |
| 299 | text = text[1 : len(text)-1] |
| 300 | } |
| 301 | in.append(&node{kind: icode, text: text}) |
| 302 | return at + n |
| 303 | } |
| 304 | |
| 305 | func (in *inliner) delimiter(i int) int { |
| 306 | s := in.s |
| 307 | c := s[i] |
| 308 | n := run(s[i:], c) |
| 309 | if c == '~' && n > 2 { |
| 310 | in.addText(s[i : i+n]) |
| 311 | return i + n |
| 312 | } |
| 313 | before, after := '\n', '\n' |
| 314 | if i > 0 { |
| 315 | before, _ = utf8.DecodeLastRuneInString(s[:i]) |
| 316 | } |
| 317 | if i+n < len(s) { |
| 318 | after, _ = utf8.DecodeRuneInString(s[i+n:]) |
| 319 | } |
| 320 | left := !isSpace(after) && (!isPunctRune(after) || isSpace(before) || isPunctRune(before)) |
| 321 | right := !isSpace(before) && (!isPunctRune(before) || isSpace(after) || isPunctRune(after)) |
| 322 | canOpen, canClose := left, right |
| 323 | if c == '_' { |
| 324 | canOpen = left && (!right || isPunctRune(before)) |
| 325 | canClose = right && (!left || isPunctRune(after)) |
| 326 | } |
| 327 | nd := in.append(&node{kind: itext, text: s[i : i+n], delimiter: true}) |
| 328 | if canOpen || canClose { |
| 329 | d := &delim{n: nd, c: c, count: n, orig: n, canOpen: canOpen, canClose: canClose, prev: in.delims} |
| 330 | if in.delims != nil { |
| 331 | in.delims.next = d |
| 332 | } |
| 333 | in.delims = d |
| 334 | } |
| 335 | return i + n |
| 336 | } |
| 337 | |
| 338 | func isSpace(r rune) bool { |
| 339 | return r == ' ' || r == '\t' || r == '\n' || r == '\r' || r == '\f' || unicode.Is(unicode.Zs, r) |
| 340 | } |
| 341 | |
| 342 | func isPunctRune(r rune) bool { |
| 343 | if r < 0x80 { |
| 344 | return isPunct(byte(r)) |
| 345 | } |
| 346 | return unicode.IsPunct(r) || unicode.IsSymbol(r) |
| 347 | } |
| 348 | |
| 349 | func (in *inliner) removeDelim(d *delim) { |
| 350 | if d.prev != nil { |
| 351 | d.prev.next = d.next |
| 352 | } |
| 353 | if d.next != nil { |
| 354 | d.next.prev = d.prev |
| 355 | } else { |
| 356 | in.delims = d.prev |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | // emphasis matches the delimiters above bottom, as CommonMark's process |
| 361 | // emphasis does. Each search for an opener starts no lower than the |
| 362 | // last search for the same kind of closer ended, so the whole takes time |
| 363 | // linear in the delimiters. |
| 364 | func (in *inliner) emphasis(bottom *delim) { |
| 365 | var openersBottom [3][2][3]*delim |
| 366 | slot := func(c byte) int { |
| 367 | switch c { |
| 368 | case '*': |
| 369 | return 0 |
| 370 | case '_': |
| 371 | return 1 |
| 372 | } |
| 373 | return 2 |
| 374 | } |
| 375 | for a := range openersBottom { |
| 376 | for b := range openersBottom[a] { |
| 377 | for k := range openersBottom[a][b] { |
| 378 | openersBottom[a][b][k] = bottom |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | // The first delimiter above bottom. |
| 383 | var closer *delim |
| 384 | for d := in.delims; d != nil && d != bottom; d = d.prev { |
| 385 | closer = d |
| 386 | } |
| 387 | for closer != nil && !in.r.full { |
| 388 | if !closer.canClose { |
| 389 | closer = closer.next |
| 390 | continue |
| 391 | } |
| 392 | open := 0 |
| 393 | if closer.canOpen { |
| 394 | open = 1 |
| 395 | } |
| 396 | mod := closer.orig % 3 |
| 397 | if closer.c == '~' { |
| 398 | mod = closer.orig |
| 399 | } |
| 400 | ob := &openersBottom[slot(closer.c)][open][mod] |
| 401 | var opener *delim |
| 402 | for o := closer.prev; o != nil && o != bottom && o != *ob; o = o.prev { |
| 403 | if o.c != closer.c || !o.canOpen { |
| 404 | continue |
| 405 | } |
| 406 | if closer.c == '~' { |
| 407 | if o.count == closer.count { |
| 408 | opener = o |
| 409 | break |
| 410 | } |
| 411 | continue |
| 412 | } |
| 413 | odd := (closer.canOpen || o.canClose) && closer.orig%3 != 0 && (o.orig+closer.orig)%3 == 0 |
| 414 | if !odd { |
| 415 | opener = o |
| 416 | break |
| 417 | } |
| 418 | } |
| 419 | if opener == nil { |
| 420 | *ob = closer.prev |
| 421 | next := closer.next |
| 422 | if !closer.canOpen { |
| 423 | in.removeDelim(closer) |
| 424 | } |
| 425 | closer = next |
| 426 | continue |
| 427 | } |
| 428 | use, kind := 1, iemph |
| 429 | switch { |
| 430 | case closer.c == '~': |
| 431 | use, kind = closer.count, idel |
| 432 | case closer.count >= 2 && opener.count >= 2: |
| 433 | use, kind = 2, istrong |
| 434 | } |
| 435 | opener.count -= use |
| 436 | closer.count -= use |
| 437 | on, cn := opener.n, closer.n |
| 438 | in.count() |
| 439 | e := &node{kind: kind, text: on.text[len(on.text)-use:]} |
| 440 | on.text = on.text[:len(on.text)-use] |
| 441 | cn.text = cn.text[:len(cn.text)-use] |
| 442 | if f := on.next; f != cn { |
| 443 | e.first, e.last = f, cn.prev |
| 444 | f.prev, cn.prev.next = nil, nil |
| 445 | } |
| 446 | on.next, e.prev, e.next, cn.prev = e, on, cn, e |
| 447 | // The delimiters between opener and closer can match nothing now. |
| 448 | for d := closer.prev; d != nil && d != opener; d = d.prev { |
| 449 | in.removeDelim(d) |
| 450 | } |
| 451 | if opener.count == 0 { |
| 452 | in.remove(on) |
| 453 | in.removeDelim(opener) |
| 454 | } |
| 455 | if closer.count == 0 { |
| 456 | next := closer.next |
| 457 | in.remove(cn) |
| 458 | in.removeDelim(closer) |
| 459 | closer = next |
| 460 | } |
| 461 | } |
| 462 | for in.delims != nil && in.delims != bottom { |
| 463 | in.removeDelim(in.delims) |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | // closeBracket handles a ] at i: it closes a link or an image when the |
| 468 | // last open bracket and what follows make one, and is text otherwise. |
| 469 | func (in *inliner) closeBracket(i int) int { |
| 470 | s := in.s |
| 471 | if len(in.brackets) == 0 { |
| 472 | in.addText("]") |
| 473 | return i + 1 |
| 474 | } |
| 475 | br := in.brackets[len(in.brackets)-1] |
| 476 | in.brackets = in.brackets[:len(in.brackets)-1] |
| 477 | // A [ below the floor is inside a link already, and opens none. |
| 478 | inactive := !br.image && len(in.brackets) < in.floor |
| 479 | in.floor = min(in.floor, len(in.brackets)) |
| 480 | var ref *linkRef |
| 481 | end, found := 0, false |
| 482 | if !inactive { |
| 483 | var dest, title string |
| 484 | if dest, title, end, found = in.inlineLink(i + 1); found { |
| 485 | ref = &linkRef{dest: dest, title: title} |
| 486 | } |
| 487 | } |
| 488 | if !inactive && !found && len(in.r.refs) > 0 { |
| 489 | // A full reference, [text][label]; a collapsed one, [label][]; |
| 490 | // or a shortcut, [label]. |
| 491 | label, after := s[br.pos:i], i+1 |
| 492 | if l, e, ok := scanLabel(s, i+1); ok { |
| 493 | label, after = l, e |
| 494 | } else if strings.HasPrefix(s[i+1:], "[]") { |
| 495 | after = i + 3 |
| 496 | } |
| 497 | if r, ok := in.r.lookup(label); ok { |
| 498 | ref, end, found = r, after, true |
| 499 | } |
| 500 | } |
| 501 | if !found { |
| 502 | in.addText("]") |
| 503 | return i + 1 |
| 504 | } |
| 505 | in.emphasis(br.delim) |
| 506 | in.count() |
| 507 | l := &node{kind: ilink, link: ref} |
| 508 | if br.image { |
| 509 | l.kind = iimage |
| 510 | } |
| 511 | if f := br.n.next; f != nil { |
| 512 | l.first, l.last = f, in.root.last |
| 513 | f.prev = nil |
| 514 | } |
| 515 | l.prev = br.n.prev |
| 516 | if l.prev != nil { |
| 517 | l.prev.next = l |
| 518 | } else { |
| 519 | in.root.first = l |
| 520 | } |
| 521 | in.root.last = l |
| 522 | if !br.image { |
| 523 | in.floor = len(in.brackets) // no [ open now opens a link |
| 524 | } |
| 525 | return end |
| 526 | } |
| 527 | |
| 528 | // lookup finds the definition that label names. A label of more than |
| 529 | // maxLabel characters names none. Every label looked up counts its bytes |
| 530 | // against maxLabelBytes, before it is measured or normalized, and once |
| 531 | // they are spent no lookup finds anything. |
| 532 | func (r *renderer) lookup(label string) (*linkRef, bool) { |
| 533 | if len(label) > 4*maxLabel { |
| 534 | return nil, false // more than maxLabel characters |
| 535 | } |
| 536 | if r.labels+len(label) > maxLabelBytes { |
| 537 | r.labels = maxLabelBytes |
| 538 | return nil, false |
| 539 | } |
| 540 | r.labels += len(label) |
| 541 | if utf8.RuneCountInString(label) > maxLabel { |
| 542 | return nil, false |
| 543 | } |
| 544 | ref, ok := r.refs[normalizeLabel(label)] |
| 545 | return ref, ok |
| 546 | } |
| 547 | |
| 548 | // inlineLink reads (destination "title") at i. |
| 549 | func (in *inliner) inlineLink(i int) (dest, title string, end int, ok bool) { |
| 550 | s := in.s |
| 551 | if i >= len(s) || s[i] != '(' { |
| 552 | return "", "", 0, false |
| 553 | } |
| 554 | j := skipSpace(s, i+1, true) |
| 555 | dest, k, ok := scanDest(s, j) |
| 556 | if !ok { |
| 557 | return "", "", 0, false |
| 558 | } |
| 559 | m := skipSpace(s, k, true) |
| 560 | if m > k { |
| 561 | if t, e, ok := scanTitle(s, m); ok { |
| 562 | title, m = t, skipSpace(s, e, true) |
| 563 | } |
| 564 | } |
| 565 | if m >= len(s) || s[m] != ')' { |
| 566 | return "", "", 0, false |
| 567 | } |
| 568 | return dest, title, m + 1, true |
| 569 | } |
| 570 | |
| 571 | // autolink reads <scheme:…> or <address@domain> at i. |
| 572 | func autolink(s string, i int) (*node, int) { |
| 573 | j := i + 1 |
| 574 | k := j |
| 575 | for k < len(s) && k-j < 33 && (isAlpha(s[k]) || k > j && (isDigit(s[k]) || s[k] == '+' || s[k] == '.' || s[k] == '-')) { |
| 576 | k++ |
| 577 | } |
| 578 | if k-j >= 2 && k < len(s) && s[k] == ':' { |
| 579 | for e := k + 1; e < len(s); e++ { |
| 580 | c := s[e] |
| 581 | if c == '>' { |
| 582 | u := s[j:e] |
| 583 | return &node{kind: iautolink, text: u}, e + 1 |
| 584 | } |
| 585 | if c <= ' ' || c == '<' || c == 0x7f { |
| 586 | break |
| 587 | } |
| 588 | } |
| 589 | } |
| 590 | // An email address. |
| 591 | at := -1 |
| 592 | for e := j; e < len(s); e++ { |
| 593 | c := s[e] |
| 594 | switch { |
| 595 | case c == '@' && at < 0 && e > j: |
| 596 | at = e |
| 597 | case c == '>' && at > 0 && e > at+1: |
| 598 | if validDomain(s[at+1 : e]) { |
| 599 | a := s[j:e] |
| 600 | return &node{kind: iautolink, text: a, mail: true}, e + 1 |
| 601 | } |
| 602 | return nil, 0 |
| 603 | case at < 0 && (isAlpha(c) || isDigit(c) || strings.IndexByte(".!#$%&'*+/=?^_`{|}~-", c) >= 0): |
| 604 | case at > 0 && (isAlpha(c) || isDigit(c) || c == '-' || c == '.'): |
| 605 | default: |
| 606 | return nil, 0 |
| 607 | } |
| 608 | } |
| 609 | return nil, 0 |
| 610 | } |
| 611 | |
| 612 | func validDomain(d string) bool { |
| 613 | for _, part := range strings.Split(d, ".") { |
| 614 | if part == "" || len(part) > 63 || part[0] == '-' || part[len(part)-1] == '-' { |
| 615 | return false |
| 616 | } |
| 617 | } |
| 618 | return true |
| 619 | } |
| 620 | |
| 621 | func isAlpha(c byte) bool { return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' } |
| 622 | func isDigit(c byte) bool { return c >= '0' && c <= '9' } |
| 623 | |
| 624 | // bareLink reads a bare http:// or https:// autolink at i, as GitHub |
| 625 | // does: after the line's start, a space or one of *, _, ~ and (, up to |
| 626 | // the next space or <, less trailing punctuation. |
| 627 | func bareLink(s string, i int) (*node, int) { |
| 628 | if i > 0 && !(isSpace(rune(s[i-1])) || strings.IndexByte("*_~(", s[i-1]) >= 0) { |
| 629 | return nil, 0 |
| 630 | } |
| 631 | rest := s[i:] |
| 632 | var n int |
| 633 | switch { |
| 634 | case strings.HasPrefix(rest, "http://"): |
| 635 | n = 7 |
| 636 | case strings.HasPrefix(rest, "https://"): |
| 637 | n = 8 |
| 638 | default: |
| 639 | return nil, 0 |
| 640 | } |
| 641 | if n >= len(rest) || !(isAlpha(rest[n]) || isDigit(rest[n])) { |
| 642 | return nil, 0 |
| 643 | } |
| 644 | e := n |
| 645 | for e < len(rest) && rest[e] > ' ' && rest[e] != '<' && rest[e] != 0x7f { |
| 646 | e++ |
| 647 | } |
| 648 | // A ) at the end goes while the link holds more ) than (. The two |
| 649 | // are counted once, and the count of ) kept as each goes. |
| 650 | opens, closes := strings.Count(rest[:e], "("), strings.Count(rest[:e], ")") |
| 651 | for e > n { |
| 652 | c := rest[e-1] |
| 653 | switch { |
| 654 | case strings.IndexByte("?!.,:*_~'\"", c) >= 0: |
| 655 | e-- |
| 656 | continue |
| 657 | case c == ')' && closes > opens: |
| 658 | e-- |
| 659 | closes-- |
| 660 | continue |
| 661 | case c == ';': |
| 662 | k := e - 2 |
| 663 | for k > n && (isAlpha(rest[k]) || isDigit(rest[k])) { |
| 664 | k-- |
| 665 | } |
| 666 | if k < e-2 && rest[k] == '&' { |
| 667 | e = k |
| 668 | continue |
| 669 | } |
| 670 | } |
| 671 | break |
| 672 | } |
| 673 | u := rest[:e] |
| 674 | return &node{kind: iautolink, text: u}, i + e |
| 675 | } |
| 676 | |
| 677 | // inline renders s, and writes its text to plain when plain is not nil. |
| 678 | // Emphasis, strong emphasis, strikethrough and links nest at most |
| 679 | // MaxDepth deep: deeper, emphasis shows its delimiters as text, and a |
| 680 | // link, an image or an autolink its text alone. |
| 681 | func (r *renderer) inline(s string, plain *strings.Builder) { |
| 682 | if r.stopped { |
| 683 | return |
| 684 | } |
| 685 | t := strings.Trim(s, " \n") |
| 686 | var root *node |
| 687 | if !plainText(t) { |
| 688 | root = r.parseInline(t) |
| 689 | } |
| 690 | if root == nil { |
| 691 | // Text alone, such as most table cells, takes no parse, and |
| 692 | // neither does text past MaxInlineNodes. |
| 693 | r.text(t) |
| 694 | if plain != nil { |
| 695 | plain.WriteString(t) |
| 696 | } |
| 697 | return |
| 698 | } |
| 699 | type frame struct { |
| 700 | n *node |
| 701 | close string |
| 702 | // element is set when the frame opened an element. |
| 703 | element bool |
| 704 | } |
| 705 | var stack []frame |
| 706 | inLink, depth := 0, 0 |
| 707 | push := func(n *node, open, close string, element bool) { |
| 708 | r.raw(open) |
| 709 | stack = append(stack, frame{n, close, element}) |
| 710 | if element { |
| 711 | depth++ |
| 712 | } |
| 713 | } |
| 714 | write := func(s string) { |
| 715 | if plain != nil { |
| 716 | plain.WriteString(s) |
| 717 | } |
| 718 | } |
| 719 | n := root.first |
| 720 | for n != nil || len(stack) > 0 { |
| 721 | if r.done() { |
| 722 | return |
| 723 | } |
| 724 | if n == nil { |
| 725 | f := stack[len(stack)-1] |
| 726 | stack = stack[:len(stack)-1] |
| 727 | r.raw(f.close) |
| 728 | if f.element { |
| 729 | depth-- |
| 730 | if f.n.kind == ilink { |
| 731 | inLink-- |
| 732 | } |
| 733 | } |
| 734 | if !f.element && f.n.kind != ilink { |
| 735 | write(f.n.text) |
| 736 | } |
| 737 | n = f.n.next |
| 738 | continue |
| 739 | } |
| 740 | switch n.kind { |
| 741 | case itext, icode: |
| 742 | if n.kind == icode { |
| 743 | r.raw("<code>") |
| 744 | } |
| 745 | r.text(n.text) |
| 746 | if n.kind == icode { |
| 747 | r.raw("</code>") |
| 748 | } |
| 749 | write(n.text) |
| 750 | switch n.brk { |
| 751 | case isoft: |
| 752 | r.raw("\n") |
| 753 | write(" ") |
| 754 | case ihard: |
| 755 | r.raw("<br>\n") |
| 756 | write(" ") |
| 757 | } |
| 758 | case isoft: |
| 759 | r.raw("\n") |
| 760 | write(" ") |
| 761 | case ihard: |
| 762 | r.raw("<br>\n") |
| 763 | write(" ") |
| 764 | case iemph, istrong, idel: |
| 765 | if depth < MaxDepth { |
| 766 | tag := elements[n.kind] |
| 767 | push(n, "<"+tag+">", "</"+tag+">", true) |
| 768 | } else { |
| 769 | d := escape(n.text) |
| 770 | push(n, d, d, false) |
| 771 | write(n.text) |
| 772 | } |
| 773 | case ilink: |
| 774 | if inLink == 0 && depth < MaxDepth { |
| 775 | if tag, ok := r.linkTag(n); ok { |
| 776 | inLink++ |
| 777 | push(n, tag, "</a>", true) |
| 778 | break |
| 779 | } |
| 780 | } |
| 781 | push(n, "", "", false) |
| 782 | case iimage: |
| 783 | alt := altText(n) |
| 784 | write(alt) |
| 785 | tag, ok := "", false |
| 786 | if inLink == 0 && depth < MaxDepth { |
| 787 | tag, ok = r.linkTag(n) |
| 788 | } |
| 789 | if !ok { |
| 790 | r.text(alt) |
| 791 | break |
| 792 | } |
| 793 | if alt == "" { |
| 794 | alt = n.link.dest |
| 795 | } |
| 796 | r.raw(tag) |
| 797 | r.text(alt) |
| 798 | r.raw("</a>") |
| 799 | case iautolink: |
| 800 | write(n.text) |
| 801 | href, ok := "", false |
| 802 | if inLink == 0 && depth < MaxDepth { |
| 803 | href, ok = r.href(n.dest()) |
| 804 | } |
| 805 | if ok { |
| 806 | r.raw(`<a href="` + escape(href) + `">`) |
| 807 | r.text(n.text) |
| 808 | r.raw("</a>") |
| 809 | } else { |
| 810 | r.text(n.text) |
| 811 | } |
| 812 | } |
| 813 | if len(stack) > 0 && stack[len(stack)-1].n == n { |
| 814 | n = n.first |
| 815 | continue |
| 816 | } |
| 817 | n = n.next |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | // elements names the element of each kind that holds others. |
| 822 | var elements = [...]string{iemph: "em", istrong: "strong", idel: "del"} |
| 823 | |
| 824 | // linkTag is the start tag of a link or an image to n's destination, or |
| 825 | // ok false for a destination that renders as text. It is made once for |
| 826 | // each definition, however many links use it. |
| 827 | func (r *renderer) linkTag(n *node) (string, bool) { |
| 828 | l := n.link |
| 829 | if !l.made { |
| 830 | l.made = true |
| 831 | href, ok := r.href(l.dest) |
| 832 | if !ok { |
| 833 | return "", false |
| 834 | } |
| 835 | l.tag, l.ok = `<a href="`+escape(href)+`"`, true |
| 836 | if l.title != "" { |
| 837 | l.tag += ` title="` + escape(l.title) + `"` |
| 838 | } |
| 839 | l.tag += ">" |
| 840 | } |
| 841 | return l.tag, l.ok |
| 842 | } |
| 843 | |
| 844 | // plainText reports whether s holds no byte that may start markup. |
| 845 | func plainText(s string) bool { |
| 846 | for i := 0; i < len(s); i++ { |
| 847 | if special[s[i]] { |
| 848 | return false |
| 849 | } |
| 850 | } |
| 851 | return true |
| 852 | } |
| 853 | |
| 854 | // altText is the text of n's children, as an image's alt text. |
| 855 | func altText(n *node) string { |
| 856 | var b strings.Builder |
| 857 | var stack []*node |
| 858 | c := n.first |
| 859 | for c != nil || len(stack) > 0 { |
| 860 | if c == nil { |
| 861 | c = stack[len(stack)-1].next |
| 862 | stack = stack[:len(stack)-1] |
| 863 | continue |
| 864 | } |
| 865 | switch c.kind { |
| 866 | case itext, icode, iautolink: |
| 867 | b.WriteString(c.text) |
| 868 | case isoft, ihard: |
| 869 | b.WriteByte(' ') |
| 870 | } |
| 871 | if c.brk != 0 { |
| 872 | b.WriteByte(' ') |
| 873 | } |
| 874 | if c.first != nil { |
| 875 | stack = append(stack, c) |
| 876 | c = c.first |
| 877 | continue |
| 878 | } |
| 879 | c = c.next |
| 880 | } |
| 881 | return b.String() |
| 882 | } |