all repos — grayfriday @ 91771dc3efc5036725ada61e386b65338a671945

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