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