all repos — grayfriday @ d71c7591087c576f285a39d05f2f3724acc54ab3

blackfriday fork with a few changes

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