本站源代码
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

335 lines
7.8KB

  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. // LaTeX rendering backend
  12. //
  13. //
  14. package blackfriday
  15. import (
  16. "bytes"
  17. "strings"
  18. )
  19. // Latex is a type that implements the Renderer interface for LaTeX output.
  20. //
  21. // Do not create this directly, instead use the LatexRenderer function.
  22. type Latex struct {
  23. }
  24. // LatexRenderer creates and configures a Latex object, which
  25. // satisfies the Renderer interface.
  26. //
  27. // flags is a set of LATEX_* options ORed together (currently no such options
  28. // are defined).
  29. func LatexRenderer(flags int) Renderer {
  30. return &Latex{}
  31. }
  32. func (options *Latex) GetFlags() int {
  33. return 0
  34. }
  35. // render code chunks using verbatim, or listings if we have a language
  36. func (options *Latex) BlockCode(out *bytes.Buffer, text []byte, info string) {
  37. if info == "" {
  38. out.WriteString("\n\\begin{verbatim}\n")
  39. } else {
  40. lang := strings.Fields(info)[0]
  41. out.WriteString("\n\\begin{lstlisting}[language=")
  42. out.WriteString(lang)
  43. out.WriteString("]\n")
  44. }
  45. out.Write(text)
  46. if info == "" {
  47. out.WriteString("\n\\end{verbatim}\n")
  48. } else {
  49. out.WriteString("\n\\end{lstlisting}\n")
  50. }
  51. }
  52. func (options *Latex) TitleBlock(out *bytes.Buffer, text []byte) {
  53. }
  54. func (options *Latex) BlockQuote(out *bytes.Buffer, text []byte) {
  55. out.WriteString("\n\\begin{quotation}\n")
  56. out.Write(text)
  57. out.WriteString("\n\\end{quotation}\n")
  58. }
  59. func (options *Latex) BlockHtml(out *bytes.Buffer, text []byte) {
  60. // a pretty lame thing to do...
  61. out.WriteString("\n\\begin{verbatim}\n")
  62. out.Write(text)
  63. out.WriteString("\n\\end{verbatim}\n")
  64. }
  65. func (options *Latex) Header(out *bytes.Buffer, text func() bool, level int, id string) {
  66. marker := out.Len()
  67. switch level {
  68. case 1:
  69. out.WriteString("\n\\section{")
  70. case 2:
  71. out.WriteString("\n\\subsection{")
  72. case 3:
  73. out.WriteString("\n\\subsubsection{")
  74. case 4:
  75. out.WriteString("\n\\paragraph{")
  76. case 5:
  77. out.WriteString("\n\\subparagraph{")
  78. case 6:
  79. out.WriteString("\n\\textbf{")
  80. }
  81. if !text() {
  82. out.Truncate(marker)
  83. return
  84. }
  85. out.WriteString("}\n")
  86. }
  87. func (options *Latex) HRule(out *bytes.Buffer) {
  88. out.WriteString("\n\\HRule\n")
  89. }
  90. func (options *Latex) List(out *bytes.Buffer, text func() bool, flags int) {
  91. marker := out.Len()
  92. if flags&LIST_TYPE_ORDERED != 0 {
  93. out.WriteString("\n\\begin{enumerate}\n")
  94. } else {
  95. out.WriteString("\n\\begin{itemize}\n")
  96. }
  97. if !text() {
  98. out.Truncate(marker)
  99. return
  100. }
  101. if flags&LIST_TYPE_ORDERED != 0 {
  102. out.WriteString("\n\\end{enumerate}\n")
  103. } else {
  104. out.WriteString("\n\\end{itemize}\n")
  105. }
  106. }
  107. func (options *Latex) ListItem(out *bytes.Buffer, text []byte, flags int) {
  108. out.WriteString("\n\\item ")
  109. out.Write(text)
  110. }
  111. func (options *Latex) Paragraph(out *bytes.Buffer, text func() bool) {
  112. marker := out.Len()
  113. out.WriteString("\n")
  114. if !text() {
  115. out.Truncate(marker)
  116. return
  117. }
  118. out.WriteString("\n")
  119. }
  120. func (options *Latex) Table(out *bytes.Buffer, header []byte, body []byte, columnData []int) {
  121. out.WriteString("\n\\begin{tabular}{")
  122. for _, elt := range columnData {
  123. switch elt {
  124. case TABLE_ALIGNMENT_LEFT:
  125. out.WriteByte('l')
  126. case TABLE_ALIGNMENT_RIGHT:
  127. out.WriteByte('r')
  128. default:
  129. out.WriteByte('c')
  130. }
  131. }
  132. out.WriteString("}\n")
  133. out.Write(header)
  134. out.WriteString(" \\\\\n\\hline\n")
  135. out.Write(body)
  136. out.WriteString("\n\\end{tabular}\n")
  137. }
  138. func (options *Latex) TableRow(out *bytes.Buffer, text []byte) {
  139. if out.Len() > 0 {
  140. out.WriteString(" \\\\\n")
  141. }
  142. out.Write(text)
  143. }
  144. func (options *Latex) TableHeaderCell(out *bytes.Buffer, text []byte, align int) {
  145. if out.Len() > 0 {
  146. out.WriteString(" & ")
  147. }
  148. out.Write(text)
  149. }
  150. func (options *Latex) TableCell(out *bytes.Buffer, text []byte, align int) {
  151. if out.Len() > 0 {
  152. out.WriteString(" & ")
  153. }
  154. out.Write(text)
  155. }
  156. // TODO: this
  157. func (options *Latex) Footnotes(out *bytes.Buffer, text func() bool) {
  158. }
  159. func (options *Latex) FootnoteItem(out *bytes.Buffer, name, text []byte, flags int) {
  160. }
  161. func (options *Latex) AutoLink(out *bytes.Buffer, link []byte, kind int) {
  162. out.WriteString("\\href{")
  163. if kind == LINK_TYPE_EMAIL {
  164. out.WriteString("mailto:")
  165. }
  166. out.Write(link)
  167. out.WriteString("}{")
  168. out.Write(link)
  169. out.WriteString("}")
  170. }
  171. func (options *Latex) CodeSpan(out *bytes.Buffer, text []byte) {
  172. out.WriteString("\\texttt{")
  173. escapeSpecialChars(out, text)
  174. out.WriteString("}")
  175. }
  176. func (options *Latex) DoubleEmphasis(out *bytes.Buffer, text []byte) {
  177. out.WriteString("\\textbf{")
  178. out.Write(text)
  179. out.WriteString("}")
  180. }
  181. func (options *Latex) Emphasis(out *bytes.Buffer, text []byte) {
  182. out.WriteString("\\textit{")
  183. out.Write(text)
  184. out.WriteString("}")
  185. }
  186. func (options *Latex) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) {
  187. if bytes.HasPrefix(link, []byte("http://")) || bytes.HasPrefix(link, []byte("https://")) {
  188. // treat it like a link
  189. out.WriteString("\\href{")
  190. out.Write(link)
  191. out.WriteString("}{")
  192. out.Write(alt)
  193. out.WriteString("}")
  194. } else {
  195. out.WriteString("\\includegraphics{")
  196. out.Write(link)
  197. out.WriteString("}")
  198. }
  199. }
  200. func (options *Latex) LineBreak(out *bytes.Buffer) {
  201. out.WriteString(" \\\\\n")
  202. }
  203. func (options *Latex) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) {
  204. out.WriteString("\\href{")
  205. out.Write(link)
  206. out.WriteString("}{")
  207. out.Write(content)
  208. out.WriteString("}")
  209. }
  210. func (options *Latex) RawHtmlTag(out *bytes.Buffer, tag []byte) {
  211. }
  212. func (options *Latex) TripleEmphasis(out *bytes.Buffer, text []byte) {
  213. out.WriteString("\\textbf{\\textit{")
  214. out.Write(text)
  215. out.WriteString("}}")
  216. }
  217. func (options *Latex) StrikeThrough(out *bytes.Buffer, text []byte) {
  218. out.WriteString("\\sout{")
  219. out.Write(text)
  220. out.WriteString("}")
  221. }
  222. // TODO: this
  223. func (options *Latex) FootnoteRef(out *bytes.Buffer, ref []byte, id int) {
  224. }
  225. func needsBackslash(c byte) bool {
  226. for _, r := range []byte("_{}%$&\\~#") {
  227. if c == r {
  228. return true
  229. }
  230. }
  231. return false
  232. }
  233. func escapeSpecialChars(out *bytes.Buffer, text []byte) {
  234. for i := 0; i < len(text); i++ {
  235. // directly copy normal characters
  236. org := i
  237. for i < len(text) && !needsBackslash(text[i]) {
  238. i++
  239. }
  240. if i > org {
  241. out.Write(text[org:i])
  242. }
  243. // escape a character
  244. if i >= len(text) {
  245. break
  246. }
  247. out.WriteByte('\\')
  248. out.WriteByte(text[i])
  249. }
  250. }
  251. func (options *Latex) Entity(out *bytes.Buffer, entity []byte) {
  252. // TODO: convert this into a unicode character or something
  253. out.Write(entity)
  254. }
  255. func (options *Latex) NormalText(out *bytes.Buffer, text []byte) {
  256. escapeSpecialChars(out, text)
  257. }
  258. // header and footer
  259. func (options *Latex) DocumentHeader(out *bytes.Buffer) {
  260. out.WriteString("\\documentclass{article}\n")
  261. out.WriteString("\n")
  262. out.WriteString("\\usepackage{graphicx}\n")
  263. out.WriteString("\\usepackage{listings}\n")
  264. out.WriteString("\\usepackage[margin=1in]{geometry}\n")
  265. out.WriteString("\\usepackage[utf8]{inputenc}\n")
  266. out.WriteString("\\usepackage{verbatim}\n")
  267. out.WriteString("\\usepackage[normalem]{ulem}\n")
  268. out.WriteString("\\usepackage{hyperref}\n")
  269. out.WriteString("\n")
  270. out.WriteString("\\hypersetup{colorlinks,%\n")
  271. out.WriteString(" citecolor=black,%\n")
  272. out.WriteString(" filecolor=black,%\n")
  273. out.WriteString(" linkcolor=black,%\n")
  274. out.WriteString(" urlcolor=black,%\n")
  275. out.WriteString(" pdfstartview=FitH,%\n")
  276. out.WriteString(" breaklinks=true,%\n")
  277. out.WriteString(" pdfauthor={Blackfriday Markdown Processor v")
  278. out.WriteString(VERSION)
  279. out.WriteString("}}\n")
  280. out.WriteString("\n")
  281. out.WriteString("\\newcommand{\\HRule}{\\rule{\\linewidth}{0.5mm}}\n")
  282. out.WriteString("\\addtolength{\\parskip}{0.5\\baselineskip}\n")
  283. out.WriteString("\\parindent=0pt\n")
  284. out.WriteString("\n")
  285. out.WriteString("\\begin{document}\n")
  286. }
  287. func (options *Latex) DocumentFooter(out *bytes.Buffer) {
  288. out.WriteString("\n\\end{document}\n")
  289. }
上海开阖软件有限公司 沪ICP备12045867号-1