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