latex.go 7.8 KB

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