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