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