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