all repos — grayfriday @ 6d6be3d2b25b61da82ba5d55a493da404f363bda

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