本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1452 lines
30KB

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