all repos — grayfriday @ ae9562f685bb5b10588adac83dba61a25d420df6

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