all repos — grayfriday @ c26fdef40e09685eebc206d07bbbea9ddfc3c177

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