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