all repos — grayfriday @ d0d854958e924057b72e090b0adfb2b0cef2c3b6

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