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