all repos — grayfriday @ ef087889f49d63f4ada853a7b255072f85ec970f

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