all repos — grayfriday @ 3ea84a5811efb21f6663ea315a3dbe3425c6831e

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) TableCell(out *bytes.Buffer, text []byte, align int) {
155	if out.Len() > 0 {
156		out.WriteString(" & ")
157	}
158	out.Write(text)
159}
160
161// TODO: this
162func (options *Latex) Footnotes(out *bytes.Buffer, text func() bool) {
163
164}
165
166func (options *Latex) FootnoteItem(out *bytes.Buffer, name, text []byte, flags int) {
167
168}
169
170func (options *Latex) AutoLink(out *bytes.Buffer, link []byte, kind int) {
171	out.WriteString("\\href{")
172	if kind == LINK_TYPE_EMAIL {
173		out.WriteString("mailto:")
174	}
175	out.Write(link)
176	out.WriteString("}{")
177	out.Write(link)
178	out.WriteString("}")
179}
180
181func (options *Latex) CodeSpan(out *bytes.Buffer, text []byte) {
182	out.WriteString("\\texttt{")
183	escapeSpecialChars(out, text)
184	out.WriteString("}")
185}
186
187func (options *Latex) DoubleEmphasis(out *bytes.Buffer, text []byte) {
188	out.WriteString("\\textbf{")
189	out.Write(text)
190	out.WriteString("}")
191}
192
193func (options *Latex) Emphasis(out *bytes.Buffer, text []byte) {
194	out.WriteString("\\textit{")
195	out.Write(text)
196	out.WriteString("}")
197}
198
199func (options *Latex) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) {
200	if bytes.HasPrefix(link, []byte("http://")) || bytes.HasPrefix(link, []byte("https://")) {
201		// treat it like a link
202		out.WriteString("\\href{")
203		out.Write(link)
204		out.WriteString("}{")
205		out.Write(alt)
206		out.WriteString("}")
207	} else {
208		out.WriteString("\\includegraphics{")
209		out.Write(link)
210		out.WriteString("}")
211	}
212}
213
214func (options *Latex) LineBreak(out *bytes.Buffer) {
215	out.WriteString(" \\\\\n")
216}
217
218func (options *Latex) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) {
219	out.WriteString("\\href{")
220	out.Write(link)
221	out.WriteString("}{")
222	out.Write(content)
223	out.WriteString("}")
224}
225
226func (options *Latex) RawHtmlTag(out *bytes.Buffer, tag []byte) {
227}
228
229func (options *Latex) TripleEmphasis(out *bytes.Buffer, text []byte) {
230	out.WriteString("\\textbf{\\textit{")
231	out.Write(text)
232	out.WriteString("}}")
233}
234
235func (options *Latex) StrikeThrough(out *bytes.Buffer, text []byte) {
236	out.WriteString("\\sout{")
237	out.Write(text)
238	out.WriteString("}")
239}
240
241// TODO: this
242func (options *Latex) FootnoteRef(out *bytes.Buffer, ref []byte, id int) {
243
244}
245
246func needsBackslash(c byte) bool {
247	for _, r := range []byte("_{}%$&\\~") {
248		if c == r {
249			return true
250		}
251	}
252	return false
253}
254
255func escapeSpecialChars(out *bytes.Buffer, text []byte) {
256	for i := 0; i < len(text); i++ {
257		// directly copy normal characters
258		org := i
259
260		for i < len(text) && !needsBackslash(text[i]) {
261			i++
262		}
263		if i > org {
264			out.Write(text[org:i])
265		}
266
267		// escape a character
268		if i >= len(text) {
269			break
270		}
271		out.WriteByte('\\')
272		out.WriteByte(text[i])
273	}
274}
275
276func (options *Latex) Entity(out *bytes.Buffer, entity []byte) {
277	// TODO: convert this into a unicode character or something
278	out.Write(entity)
279}
280
281func (options *Latex) NormalText(out *bytes.Buffer, text []byte) {
282	escapeSpecialChars(out, text)
283}
284
285// header and footer
286func (options *Latex) DocumentHeader(out *bytes.Buffer) {
287	out.WriteString("\\documentclass{article}\n")
288	out.WriteString("\n")
289	out.WriteString("\\usepackage{graphicx}\n")
290	out.WriteString("\\usepackage{listings}\n")
291	out.WriteString("\\usepackage[margin=1in]{geometry}\n")
292	out.WriteString("\\usepackage[utf8]{inputenc}\n")
293	out.WriteString("\\usepackage{verbatim}\n")
294	out.WriteString("\\usepackage[normalem]{ulem}\n")
295	out.WriteString("\\usepackage{hyperref}\n")
296	out.WriteString("\n")
297	out.WriteString("\\hypersetup{colorlinks,%\n")
298	out.WriteString("  citecolor=black,%\n")
299	out.WriteString("  filecolor=black,%\n")
300	out.WriteString("  linkcolor=black,%\n")
301	out.WriteString("  urlcolor=black,%\n")
302	out.WriteString("  pdfstartview=FitH,%\n")
303	out.WriteString("  breaklinks=true,%\n")
304	out.WriteString("  pdfauthor={Blackfriday Markdown Processor v")
305	out.WriteString(VERSION)
306	out.WriteString("}}\n")
307	out.WriteString("\n")
308	out.WriteString("\\newcommand{\\HRule}{\\rule{\\linewidth}{0.5mm}}\n")
309	out.WriteString("\\addtolength{\\parskip}{0.5\\baselineskip}\n")
310	out.WriteString("\\parindent=0pt\n")
311	out.WriteString("\n")
312	out.WriteString("\\begin{document}\n")
313}
314
315func (options *Latex) DocumentFooter(out *bytes.Buffer) {
316	out.WriteString("\n\\end{document}\n")
317}