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.

938 lines
24 KiB

  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. // HTML rendering backend
  12. //
  13. //
  14. package blackfriday
  15. import (
  16. "bytes"
  17. "fmt"
  18. "regexp"
  19. "strconv"
  20. "strings"
  21. )
  22. // Html renderer configuration options.
  23. const (
  24. HTML_SKIP_HTML = 1 << iota // skip preformatted HTML blocks
  25. HTML_SKIP_STYLE // skip embedded <style> elements
  26. HTML_SKIP_IMAGES // skip embedded images
  27. HTML_SKIP_LINKS // skip all links
  28. HTML_SAFELINK // only link to trusted protocols
  29. HTML_NOFOLLOW_LINKS // only link with rel="nofollow"
  30. HTML_NOREFERRER_LINKS // only link with rel="noreferrer"
  31. HTML_HREF_TARGET_BLANK // add a blank target
  32. HTML_TOC // generate a table of contents
  33. HTML_OMIT_CONTENTS // skip the main contents (for a standalone table of contents)
  34. HTML_COMPLETE_PAGE // generate a complete HTML page
  35. HTML_USE_XHTML // generate XHTML output instead of HTML
  36. HTML_USE_SMARTYPANTS // enable smart punctuation substitutions
  37. HTML_SMARTYPANTS_FRACTIONS // enable smart fractions (with HTML_USE_SMARTYPANTS)
  38. HTML_SMARTYPANTS_DASHES // enable smart dashes (with HTML_USE_SMARTYPANTS)
  39. HTML_SMARTYPANTS_LATEX_DASHES // enable LaTeX-style dashes (with HTML_USE_SMARTYPANTS and HTML_SMARTYPANTS_DASHES)
  40. HTML_SMARTYPANTS_ANGLED_QUOTES // enable angled double quotes (with HTML_USE_SMARTYPANTS) for double quotes rendering
  41. HTML_SMARTYPANTS_QUOTES_NBSP // enable "French guillemets" (with HTML_USE_SMARTYPANTS)
  42. HTML_FOOTNOTE_RETURN_LINKS // generate a link at the end of a footnote to return to the source
  43. )
  44. var (
  45. alignments = []string{
  46. "left",
  47. "right",
  48. "center",
  49. }
  50. // TODO: improve this regexp to catch all possible entities:
  51. htmlEntity = regexp.MustCompile(`&[a-z]{2,5};`)
  52. )
  53. type HtmlRendererParameters struct {
  54. // Prepend this text to each relative URL.
  55. AbsolutePrefix string
  56. // Add this text to each footnote anchor, to ensure uniqueness.
  57. FootnoteAnchorPrefix string
  58. // Show this text inside the <a> tag for a footnote return link, if the
  59. // HTML_FOOTNOTE_RETURN_LINKS flag is enabled. If blank, the string
  60. // <sup>[return]</sup> is used.
  61. FootnoteReturnLinkContents string
  62. // If set, add this text to the front of each Header ID, to ensure
  63. // uniqueness.
  64. HeaderIDPrefix string
  65. // If set, add this text to the back of each Header ID, to ensure uniqueness.
  66. HeaderIDSuffix string
  67. }
  68. // Html is a type that implements the Renderer interface for HTML output.
  69. //
  70. // Do not create this directly, instead use the HtmlRenderer function.
  71. type Html struct {
  72. flags int // HTML_* options
  73. closeTag string // how to end singleton tags: either " />" or ">"
  74. title string // document title
  75. css string // optional css file url (used with HTML_COMPLETE_PAGE)
  76. parameters HtmlRendererParameters
  77. // table of contents data
  78. tocMarker int
  79. headerCount int
  80. currentLevel int
  81. toc *bytes.Buffer
  82. // Track header IDs to prevent ID collision in a single generation.
  83. headerIDs map[string]int
  84. smartypants *smartypantsRenderer
  85. }
  86. const (
  87. xhtmlClose = " />"
  88. htmlClose = ">"
  89. )
  90. // HtmlRenderer creates and configures an Html object, which
  91. // satisfies the Renderer interface.
  92. //
  93. // flags is a set of HTML_* options ORed together.
  94. // title is the title of the document, and css is a URL for the document's
  95. // stylesheet.
  96. // title and css are only used when HTML_COMPLETE_PAGE is selected.
  97. func HtmlRenderer(flags int, title string, css string) Renderer {
  98. return HtmlRendererWithParameters(flags, title, css, HtmlRendererParameters{})
  99. }
  100. func HtmlRendererWithParameters(flags int, title string,
  101. css string, renderParameters HtmlRendererParameters) Renderer {
  102. // configure the rendering engine
  103. closeTag := htmlClose
  104. if flags&HTML_USE_XHTML != 0 {
  105. closeTag = xhtmlClose
  106. }
  107. if renderParameters.FootnoteReturnLinkContents == "" {
  108. renderParameters.FootnoteReturnLinkContents = `<sup>[return]</sup>`
  109. }
  110. return &Html{
  111. flags: flags,
  112. closeTag: closeTag,
  113. title: title,
  114. css: css,
  115. parameters: renderParameters,
  116. headerCount: 0,
  117. currentLevel: 0,
  118. toc: new(bytes.Buffer),
  119. headerIDs: make(map[string]int),
  120. smartypants: smartypants(flags),
  121. }
  122. }
  123. // Using if statements is a bit faster than a switch statement. As the compiler
  124. // improves, this should be unnecessary this is only worthwhile because
  125. // attrEscape is the single largest CPU user in normal use.
  126. // Also tried using map, but that gave a ~3x slowdown.
  127. func escapeSingleChar(char byte) (string, bool) {
  128. if char == '"' {
  129. return "&quot;", true
  130. }
  131. if char == '&' {
  132. return "&amp;", true
  133. }
  134. if char == '<' {
  135. return "&lt;", true
  136. }
  137. if char == '>' {
  138. return "&gt;", true
  139. }
  140. return "", false
  141. }
  142. func attrEscape(out *bytes.Buffer, src []byte) {
  143. org := 0
  144. for i, ch := range src {
  145. if entity, ok := escapeSingleChar(ch); ok {
  146. if i > org {
  147. // copy all the normal characters since the last escape
  148. out.Write(src[org:i])
  149. }
  150. org = i + 1
  151. out.WriteString(entity)
  152. }
  153. }
  154. if org < len(src) {
  155. out.Write(src[org:])
  156. }
  157. }
  158. func entityEscapeWithSkip(out *bytes.Buffer, src []byte, skipRanges [][]int) {
  159. end := 0
  160. for _, rang := range skipRanges {
  161. attrEscape(out, src[end:rang[0]])
  162. out.Write(src[rang[0]:rang[1]])
  163. end = rang[1]
  164. }
  165. attrEscape(out, src[end:])
  166. }
  167. func (options *Html) GetFlags() int {
  168. return options.flags
  169. }
  170. func (options *Html) TitleBlock(out *bytes.Buffer, text []byte) {
  171. text = bytes.TrimPrefix(text, []byte("% "))
  172. text = bytes.Replace(text, []byte("\n% "), []byte("\n"), -1)
  173. out.WriteString("<h1 class=\"title\">")
  174. out.Write(text)
  175. out.WriteString("\n</h1>")
  176. }
  177. func (options *Html) Header(out *bytes.Buffer, text func() bool, level int, id string) {
  178. marker := out.Len()
  179. doubleSpace(out)
  180. if id == "" && options.flags&HTML_TOC != 0 {
  181. id = fmt.Sprintf("toc_%d", options.headerCount)
  182. }
  183. if id != "" {
  184. id = options.ensureUniqueHeaderID(id)
  185. if options.parameters.HeaderIDPrefix != "" {
  186. id = options.parameters.HeaderIDPrefix + id
  187. }
  188. if options.parameters.HeaderIDSuffix != "" {
  189. id = id + options.parameters.HeaderIDSuffix
  190. }
  191. out.WriteString(fmt.Sprintf("<h%d id=\"%s\">", level, id))
  192. } else {
  193. out.WriteString(fmt.Sprintf("<h%d>", level))
  194. }
  195. tocMarker := out.Len()
  196. if !text() {
  197. out.Truncate(marker)
  198. return
  199. }
  200. // are we building a table of contents?
  201. if options.flags&HTML_TOC != 0 {
  202. options.TocHeaderWithAnchor(out.Bytes()[tocMarker:], level, id)
  203. }
  204. out.WriteString(fmt.Sprintf("</h%d>\n", level))
  205. }
  206. func (options *Html) BlockHtml(out *bytes.Buffer, text []byte) {
  207. if options.flags&HTML_SKIP_HTML != 0 {
  208. return
  209. }
  210. doubleSpace(out)
  211. out.Write(text)
  212. out.WriteByte('\n')
  213. }
  214. func (options *Html) HRule(out *bytes.Buffer) {
  215. doubleSpace(out)
  216. out.WriteString("<hr")
  217. out.WriteString(options.closeTag)
  218. out.WriteByte('\n')
  219. }
  220. func (options *Html) BlockCode(out *bytes.Buffer, text []byte, info string) {
  221. doubleSpace(out)
  222. endOfLang := strings.IndexAny(info, "\t ")
  223. if endOfLang < 0 {
  224. endOfLang = len(info)
  225. }
  226. lang := info[:endOfLang]
  227. if len(lang) == 0 || lang == "." {
  228. out.WriteString("<pre><code>")
  229. } else {
  230. out.WriteString("<pre><code class=\"language-")
  231. attrEscape(out, []byte(lang))
  232. out.WriteString("\">")
  233. }
  234. attrEscape(out, text)
  235. out.WriteString("</code></pre>\n")
  236. }
  237. func (options *Html) BlockQuote(out *bytes.Buffer, text []byte) {
  238. doubleSpace(out)
  239. out.WriteString("<blockquote>\n")
  240. out.Write(text)
  241. out.WriteString("</blockquote>\n")
  242. }
  243. func (options *Html) Table(out *bytes.Buffer, header []byte, body []byte, columnData []int) {
  244. doubleSpace(out)
  245. out.WriteString("<table>\n<thead>\n")
  246. out.Write(header)
  247. out.WriteString("</thead>\n\n<tbody>\n")
  248. out.Write(body)
  249. out.WriteString("</tbody>\n</table>\n")
  250. }
  251. func (options *Html) TableRow(out *bytes.Buffer, text []byte) {
  252. doubleSpace(out)
  253. out.WriteString("<tr>\n")
  254. out.Write(text)
  255. out.WriteString("\n</tr>\n")
  256. }
  257. func (options *Html) TableHeaderCell(out *bytes.Buffer, text []byte, align int) {
  258. doubleSpace(out)
  259. switch align {
  260. case TABLE_ALIGNMENT_LEFT:
  261. out.WriteString("<th align=\"left\">")
  262. case TABLE_ALIGNMENT_RIGHT:
  263. out.WriteString("<th align=\"right\">")
  264. case TABLE_ALIGNMENT_CENTER:
  265. out.WriteString("<th align=\"center\">")
  266. default:
  267. out.WriteString("<th>")
  268. }
  269. out.Write(text)
  270. out.WriteString("</th>")
  271. }
  272. func (options *Html) TableCell(out *bytes.Buffer, text []byte, align int) {
  273. doubleSpace(out)
  274. switch align {
  275. case TABLE_ALIGNMENT_LEFT:
  276. out.WriteString("<td align=\"left\">")
  277. case TABLE_ALIGNMENT_RIGHT:
  278. out.WriteString("<td align=\"right\">")
  279. case TABLE_ALIGNMENT_CENTER:
  280. out.WriteString("<td align=\"center\">")
  281. default:
  282. out.WriteString("<td>")
  283. }
  284. out.Write(text)
  285. out.WriteString("</td>")
  286. }
  287. func (options *Html) Footnotes(out *bytes.Buffer, text func() bool) {
  288. out.WriteString("<div class=\"footnotes\">\n")
  289. options.HRule(out)
  290. options.List(out, text, LIST_TYPE_ORDERED)
  291. out.WriteString("</div>\n")
  292. }
  293. func (options *Html) FootnoteItem(out *bytes.Buffer, name, text []byte, flags int) {
  294. if flags&LIST_ITEM_CONTAINS_BLOCK != 0 || flags&LIST_ITEM_BEGINNING_OF_LIST != 0 {
  295. doubleSpace(out)
  296. }
  297. slug := slugify(name)
  298. out.WriteString(`<li id="`)
  299. out.WriteString(`fn:`)
  300. out.WriteString(options.parameters.FootnoteAnchorPrefix)
  301. out.Write(slug)
  302. out.WriteString(`">`)
  303. out.Write(text)
  304. if options.flags&HTML_FOOTNOTE_RETURN_LINKS != 0 {
  305. out.WriteString(` <a class="footnote-return" href="#`)
  306. out.WriteString(`fnref:`)
  307. out.WriteString(options.parameters.FootnoteAnchorPrefix)
  308. out.Write(slug)
  309. out.WriteString(`">`)
  310. out.WriteString(options.parameters.FootnoteReturnLinkContents)
  311. out.WriteString(`</a>`)
  312. }
  313. out.WriteString("</li>\n")
  314. }
  315. func (options *Html) List(out *bytes.Buffer, text func() bool, flags int) {
  316. marker := out.Len()
  317. doubleSpace(out)
  318. if flags&LIST_TYPE_DEFINITION != 0 {
  319. out.WriteString("<dl>")
  320. } else if flags&LIST_TYPE_ORDERED != 0 {
  321. out.WriteString("<ol>")
  322. } else {
  323. out.WriteString("<ul>")
  324. }
  325. if !text() {
  326. out.Truncate(marker)
  327. return
  328. }
  329. if flags&LIST_TYPE_DEFINITION != 0 {
  330. out.WriteString("</dl>\n")
  331. } else if flags&LIST_TYPE_ORDERED != 0 {
  332. out.WriteString("</ol>\n")
  333. } else {
  334. out.WriteString("</ul>\n")
  335. }
  336. }
  337. func (options *Html) ListItem(out *bytes.Buffer, text []byte, flags int) {
  338. if (flags&LIST_ITEM_CONTAINS_BLOCK != 0 && flags&LIST_TYPE_DEFINITION == 0) ||
  339. flags&LIST_ITEM_BEGINNING_OF_LIST != 0 {
  340. doubleSpace(out)
  341. }
  342. if flags&LIST_TYPE_TERM != 0 {
  343. out.WriteString("<dt>")
  344. } else if flags&LIST_TYPE_DEFINITION != 0 {
  345. out.WriteString("<dd>")
  346. } else {
  347. out.WriteString("<li>")
  348. }
  349. out.Write(text)
  350. if flags&LIST_TYPE_TERM != 0 {
  351. out.WriteString("</dt>\n")
  352. } else if flags&LIST_TYPE_DEFINITION != 0 {
  353. out.WriteString("</dd>\n")
  354. } else {
  355. out.WriteString("</li>\n")
  356. }
  357. }
  358. func (options *Html) Paragraph(out *bytes.Buffer, text func() bool) {
  359. marker := out.Len()
  360. doubleSpace(out)
  361. out.WriteString("<p>")
  362. if !text() {
  363. out.Truncate(marker)
  364. return
  365. }
  366. out.WriteString("</p>\n")
  367. }
  368. func (options *Html) AutoLink(out *bytes.Buffer, link []byte, kind int) {
  369. skipRanges := htmlEntity.FindAllIndex(link, -1)
  370. if options.flags&HTML_SAFELINK != 0 && !isSafeLink(link) && kind != LINK_TYPE_EMAIL {
  371. // mark it but don't link it if it is not a safe link: no smartypants
  372. out.WriteString("<tt>")
  373. entityEscapeWithSkip(out, link, skipRanges)
  374. out.WriteString("</tt>")
  375. return
  376. }
  377. out.WriteString("<a href=\"")
  378. if kind == LINK_TYPE_EMAIL {
  379. out.WriteString("mailto:")
  380. } else {
  381. options.maybeWriteAbsolutePrefix(out, link)
  382. }
  383. entityEscapeWithSkip(out, link, skipRanges)
  384. var relAttrs []string
  385. if options.flags&HTML_NOFOLLOW_LINKS != 0 && !isRelativeLink(link) {
  386. relAttrs = append(relAttrs, "nofollow")
  387. }
  388. if options.flags&HTML_NOREFERRER_LINKS != 0 && !isRelativeLink(link) {
  389. relAttrs = append(relAttrs, "noreferrer")
  390. }
  391. if len(relAttrs) > 0 {
  392. out.WriteString(fmt.Sprintf("\" rel=\"%s", strings.Join(relAttrs, " ")))
  393. }
  394. // blank target only add to external link
  395. if options.flags&HTML_HREF_TARGET_BLANK != 0 && !isRelativeLink(link) {
  396. out.WriteString("\" target=\"_blank")
  397. }
  398. out.WriteString("\">")
  399. // Pretty print: if we get an email address as
  400. // an actual URI, e.g. `mailto:foo@bar.com`, we don't
  401. // want to print the `mailto:` prefix
  402. switch {
  403. case bytes.HasPrefix(link, []byte("mailto://")):
  404. attrEscape(out, link[len("mailto://"):])
  405. case bytes.HasPrefix(link, []byte("mailto:")):
  406. attrEscape(out, link[len("mailto:"):])
  407. default:
  408. entityEscapeWithSkip(out, link, skipRanges)
  409. }
  410. out.WriteString("</a>")
  411. }
  412. func (options *Html) CodeSpan(out *bytes.Buffer, text []byte) {
  413. out.WriteString("<code>")
  414. attrEscape(out, text)
  415. out.WriteString("</code>")
  416. }
  417. func (options *Html) DoubleEmphasis(out *bytes.Buffer, text []byte) {
  418. out.WriteString("<strong>")
  419. out.Write(text)
  420. out.WriteString("</strong>")
  421. }
  422. func (options *Html) Emphasis(out *bytes.Buffer, text []byte) {
  423. if len(text) == 0 {
  424. return
  425. }
  426. out.WriteString("<em>")
  427. out.Write(text)
  428. out.WriteString("</em>")
  429. }
  430. func (options *Html) maybeWriteAbsolutePrefix(out *bytes.Buffer, link []byte) {
  431. if options.parameters.AbsolutePrefix != "" && isRelativeLink(link) && link[0] != '.' {
  432. out.WriteString(options.parameters.AbsolutePrefix)
  433. if link[0] != '/' {
  434. out.WriteByte('/')
  435. }
  436. }
  437. }
  438. func (options *Html) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) {
  439. if options.flags&HTML_SKIP_IMAGES != 0 {
  440. return
  441. }
  442. out.WriteString("<img src=\"")
  443. options.maybeWriteAbsolutePrefix(out, link)
  444. attrEscape(out, link)
  445. out.WriteString("\" alt=\"")
  446. if len(alt) > 0 {
  447. attrEscape(out, alt)
  448. }
  449. if len(title) > 0 {
  450. out.WriteString("\" title=\"")
  451. attrEscape(out, title)
  452. }
  453. out.WriteByte('"')
  454. out.WriteString(options.closeTag)
  455. }
  456. func (options *Html) LineBreak(out *bytes.Buffer) {
  457. out.WriteString("<br")
  458. out.WriteString(options.closeTag)
  459. out.WriteByte('\n')
  460. }
  461. func (options *Html) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) {
  462. if options.flags&HTML_SKIP_LINKS != 0 {
  463. // write the link text out but don't link it, just mark it with typewriter font
  464. out.WriteString("<tt>")
  465. attrEscape(out, content)
  466. out.WriteString("</tt>")
  467. return
  468. }
  469. if options.flags&HTML_SAFELINK != 0 && !isSafeLink(link) {
  470. // write the link text out but don't link it, just mark it with typewriter font
  471. out.WriteString("<tt>")
  472. attrEscape(out, content)
  473. out.WriteString("</tt>")
  474. return
  475. }
  476. out.WriteString("<a href=\"")
  477. options.maybeWriteAbsolutePrefix(out, link)
  478. attrEscape(out, link)
  479. if len(title) > 0 {
  480. out.WriteString("\" title=\"")
  481. attrEscape(out, title)
  482. }
  483. var relAttrs []string
  484. if options.flags&HTML_NOFOLLOW_LINKS != 0 && !isRelativeLink(link) {
  485. relAttrs = append(relAttrs, "nofollow")
  486. }
  487. if options.flags&HTML_NOREFERRER_LINKS != 0 && !isRelativeLink(link) {
  488. relAttrs = append(relAttrs, "noreferrer")
  489. }
  490. if len(relAttrs) > 0 {
  491. out.WriteString(fmt.Sprintf("\" rel=\"%s", strings.Join(relAttrs, " ")))
  492. }
  493. // blank target only add to external link
  494. if options.flags&HTML_HREF_TARGET_BLANK != 0 && !isRelativeLink(link) {
  495. out.WriteString("\" target=\"_blank")
  496. }
  497. out.WriteString("\">")
  498. out.Write(content)
  499. out.WriteString("</a>")
  500. return
  501. }
  502. func (options *Html) RawHtmlTag(out *bytes.Buffer, text []byte) {
  503. if options.flags&HTML_SKIP_HTML != 0 {
  504. return
  505. }
  506. if options.flags&HTML_SKIP_STYLE != 0 && isHtmlTag(text, "style") {
  507. return
  508. }
  509. if options.flags&HTML_SKIP_LINKS != 0 && isHtmlTag(text, "a") {
  510. return
  511. }
  512. if options.flags&HTML_SKIP_IMAGES != 0 && isHtmlTag(text, "img") {
  513. return
  514. }
  515. out.Write(text)
  516. }
  517. func (options *Html) TripleEmphasis(out *bytes.Buffer, text []byte) {
  518. out.WriteString("<strong><em>")
  519. out.Write(text)
  520. out.WriteString("</em></strong>")
  521. }
  522. func (options *Html) StrikeThrough(out *bytes.Buffer, text []byte) {
  523. out.WriteString("<del>")
  524. out.Write(text)
  525. out.WriteString("</del>")
  526. }
  527. func (options *Html) FootnoteRef(out *bytes.Buffer, ref []byte, id int) {
  528. slug := slugify(ref)
  529. out.WriteString(`<sup class="footnote-ref" id="`)
  530. out.WriteString(`fnref:`)
  531. out.WriteString(options.parameters.FootnoteAnchorPrefix)
  532. out.Write(slug)
  533. out.WriteString(`"><a href="#`)
  534. out.WriteString(`fn:`)
  535. out.WriteString(options.parameters.FootnoteAnchorPrefix)
  536. out.Write(slug)
  537. out.WriteString(`">`)
  538. out.WriteString(strconv.Itoa(id))
  539. out.WriteString(`</a></sup>`)
  540. }
  541. func (options *Html) Entity(out *bytes.Buffer, entity []byte) {
  542. out.Write(entity)
  543. }
  544. func (options *Html) NormalText(out *bytes.Buffer, text []byte) {
  545. if options.flags&HTML_USE_SMARTYPANTS != 0 {
  546. options.Smartypants(out, text)
  547. } else {
  548. attrEscape(out, text)
  549. }
  550. }
  551. func (options *Html) Smartypants(out *bytes.Buffer, text []byte) {
  552. smrt := smartypantsData{false, false}
  553. // first do normal entity escaping
  554. var escaped bytes.Buffer
  555. attrEscape(&escaped, text)
  556. text = escaped.Bytes()
  557. mark := 0
  558. for i := 0; i < len(text); i++ {
  559. if action := options.smartypants[text[i]]; action != nil {
  560. if i > mark {
  561. out.Write(text[mark:i])
  562. }
  563. previousChar := byte(0)
  564. if i > 0 {
  565. previousChar = text[i-1]
  566. }
  567. i += action(out, &smrt, previousChar, text[i:])
  568. mark = i + 1
  569. }
  570. }
  571. if mark < len(text) {
  572. out.Write(text[mark:])
  573. }
  574. }
  575. func (options *Html) DocumentHeader(out *bytes.Buffer) {
  576. if options.flags&HTML_COMPLETE_PAGE == 0 {
  577. return
  578. }
  579. ending := ""
  580. if options.flags&HTML_USE_XHTML != 0 {
  581. out.WriteString("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" ")
  582. out.WriteString("\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n")
  583. out.WriteString("<html xmlns=\"http://www.w3.org/1999/xhtml\">\n")
  584. ending = " /"
  585. } else {
  586. out.WriteString("<!DOCTYPE html>\n")
  587. out.WriteString("<html>\n")
  588. }
  589. out.WriteString("<head>\n")
  590. out.WriteString(" <title>")
  591. options.NormalText(out, []byte(options.title))
  592. out.WriteString("</title>\n")
  593. out.WriteString(" <meta name=\"GENERATOR\" content=\"Blackfriday Markdown Processor v")
  594. out.WriteString(VERSION)
  595. out.WriteString("\"")
  596. out.WriteString(ending)
  597. out.WriteString(">\n")
  598. out.WriteString(" <meta charset=\"utf-8\"")
  599. out.WriteString(ending)
  600. out.WriteString(">\n")
  601. if options.css != "" {
  602. out.WriteString(" <link rel=\"stylesheet\" type=\"text/css\" href=\"")
  603. attrEscape(out, []byte(options.css))
  604. out.WriteString("\"")
  605. out.WriteString(ending)
  606. out.WriteString(">\n")
  607. }
  608. out.WriteString("</head>\n")
  609. out.WriteString("<body>\n")
  610. options.tocMarker = out.Len()
  611. }
  612. func (options *Html) DocumentFooter(out *bytes.Buffer) {
  613. // finalize and insert the table of contents
  614. if options.flags&HTML_TOC != 0 {
  615. options.TocFinalize()
  616. // now we have to insert the table of contents into the document
  617. var temp bytes.Buffer
  618. // start by making a copy of everything after the document header
  619. temp.Write(out.Bytes()[options.tocMarker:])
  620. // now clear the copied material from the main output buffer
  621. out.Truncate(options.tocMarker)
  622. // corner case spacing issue
  623. if options.flags&HTML_COMPLETE_PAGE != 0 {
  624. out.WriteByte('\n')
  625. }
  626. // insert the table of contents
  627. out.WriteString("<nav>\n")
  628. out.Write(options.toc.Bytes())
  629. out.WriteString("</nav>\n")
  630. // corner case spacing issue
  631. if options.flags&HTML_COMPLETE_PAGE == 0 && options.flags&HTML_OMIT_CONTENTS == 0 {
  632. out.WriteByte('\n')
  633. }
  634. // write out everything that came after it
  635. if options.flags&HTML_OMIT_CONTENTS == 0 {
  636. out.Write(temp.Bytes())
  637. }
  638. }
  639. if options.flags&HTML_COMPLETE_PAGE != 0 {
  640. out.WriteString("\n</body>\n")
  641. out.WriteString("</html>\n")
  642. }
  643. }
  644. func (options *Html) TocHeaderWithAnchor(text []byte, level int, anchor string) {
  645. for level > options.currentLevel {
  646. switch {
  647. case bytes.HasSuffix(options.toc.Bytes(), []byte("</li>\n")):
  648. // this sublist can nest underneath a header
  649. size := options.toc.Len()
  650. options.toc.Truncate(size - len("</li>\n"))
  651. case options.currentLevel > 0:
  652. options.toc.WriteString("<li>")
  653. }
  654. if options.toc.Len() > 0 {
  655. options.toc.WriteByte('\n')
  656. }
  657. options.toc.WriteString("<ul>\n")
  658. options.currentLevel++
  659. }
  660. for level < options.currentLevel {
  661. options.toc.WriteString("</ul>")
  662. if options.currentLevel > 1 {
  663. options.toc.WriteString("</li>\n")
  664. }
  665. options.currentLevel--
  666. }
  667. options.toc.WriteString("<li><a href=\"#")
  668. if anchor != "" {
  669. options.toc.WriteString(anchor)
  670. } else {
  671. options.toc.WriteString("toc_")
  672. options.toc.WriteString(strconv.Itoa(options.headerCount))
  673. }
  674. options.toc.WriteString("\">")
  675. options.headerCount++
  676. options.toc.Write(text)
  677. options.toc.WriteString("</a></li>\n")
  678. }
  679. func (options *Html) TocHeader(text []byte, level int) {
  680. options.TocHeaderWithAnchor(text, level, "")
  681. }
  682. func (options *Html) TocFinalize() {
  683. for options.currentLevel > 1 {
  684. options.toc.WriteString("</ul></li>\n")
  685. options.currentLevel--
  686. }
  687. if options.currentLevel > 0 {
  688. options.toc.WriteString("</ul>\n")
  689. }
  690. }
  691. func isHtmlTag(tag []byte, tagname string) bool {
  692. found, _ := findHtmlTagPos(tag, tagname)
  693. return found
  694. }
  695. // Look for a character, but ignore it when it's in any kind of quotes, it
  696. // might be JavaScript
  697. func skipUntilCharIgnoreQuotes(html []byte, start int, char byte) int {
  698. inSingleQuote := false
  699. inDoubleQuote := false
  700. inGraveQuote := false
  701. i := start
  702. for i < len(html) {
  703. switch {
  704. case html[i] == char && !inSingleQuote && !inDoubleQuote && !inGraveQuote:
  705. return i
  706. case html[i] == '\'':
  707. inSingleQuote = !inSingleQuote
  708. case html[i] == '"':
  709. inDoubleQuote = !inDoubleQuote
  710. case html[i] == '`':
  711. inGraveQuote = !inGraveQuote
  712. }
  713. i++
  714. }
  715. return start
  716. }
  717. func findHtmlTagPos(tag []byte, tagname string) (bool, int) {
  718. i := 0
  719. if i < len(tag) && tag[0] != '<' {
  720. return false, -1
  721. }
  722. i++
  723. i = skipSpace(tag, i)
  724. if i < len(tag) && tag[i] == '/' {
  725. i++
  726. }
  727. i = skipSpace(tag, i)
  728. j := 0
  729. for ; i < len(tag); i, j = i+1, j+1 {
  730. if j >= len(tagname) {
  731. break
  732. }
  733. if strings.ToLower(string(tag[i]))[0] != tagname[j] {
  734. return false, -1
  735. }
  736. }
  737. if i == len(tag) {
  738. return false, -1
  739. }
  740. rightAngle := skipUntilCharIgnoreQuotes(tag, i, '>')
  741. if rightAngle > i {
  742. return true, rightAngle
  743. }
  744. return false, -1
  745. }
  746. func skipUntilChar(text []byte, start int, char byte) int {
  747. i := start
  748. for i < len(text) && text[i] != char {
  749. i++
  750. }
  751. return i
  752. }
  753. func skipSpace(tag []byte, i int) int {
  754. for i < len(tag) && isspace(tag[i]) {
  755. i++
  756. }
  757. return i
  758. }
  759. func skipChar(data []byte, start int, char byte) int {
  760. i := start
  761. for i < len(data) && data[i] == char {
  762. i++
  763. }
  764. return i
  765. }
  766. func doubleSpace(out *bytes.Buffer) {
  767. if out.Len() > 0 {
  768. out.WriteByte('\n')
  769. }
  770. }
  771. func isRelativeLink(link []byte) (yes bool) {
  772. // a tag begin with '#'
  773. if link[0] == '#' {
  774. return true
  775. }
  776. // link begin with '/' but not '//', the second maybe a protocol relative link
  777. if len(link) >= 2 && link[0] == '/' && link[1] != '/' {
  778. return true
  779. }
  780. // only the root '/'
  781. if len(link) == 1 && link[0] == '/' {
  782. return true
  783. }
  784. // current directory : begin with "./"
  785. if bytes.HasPrefix(link, []byte("./")) {
  786. return true
  787. }
  788. // parent directory : begin with "../"
  789. if bytes.HasPrefix(link, []byte("../")) {
  790. return true
  791. }
  792. return false
  793. }
  794. func (options *Html) ensureUniqueHeaderID(id string) string {
  795. for count, found := options.headerIDs[id]; found; count, found = options.headerIDs[id] {
  796. tmp := fmt.Sprintf("%s-%d", id, count+1)
  797. if _, tmpFound := options.headerIDs[tmp]; !tmpFound {
  798. options.headerIDs[id] = count + 1
  799. id = tmp
  800. } else {
  801. id = id + "-1"
  802. }
  803. }
  804. if _, found := options.headerIDs[id]; !found {
  805. options.headerIDs[id] = 0
  806. }
  807. return id
  808. }