all repos — grayfriday @ cc0d56d0920643c43d4fb60b26d62ac10893c6dd

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