2011-05-30 19:06:20 +02:00
|
|
|
//
|
2011-06-28 04:11:32 +02:00
|
|
|
// Blackfriday Markdown Processor
|
|
|
|
// Available at http://github.com/russross/blackfriday
|
|
|
|
//
|
|
|
|
// Copyright © 2011 Russ Ross <russ@russross.com>.
|
2011-06-28 19:30:10 +02:00
|
|
|
// Distributed under the Simplified BSD License.
|
2011-06-28 04:11:32 +02:00
|
|
|
// See README.md for details.
|
2011-05-30 19:06:20 +02:00
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// LaTeX rendering backend
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
package blackfriday
|
|
|
|
|
2016-03-30 20:13:02 +02:00
|
|
|
import "bytes"
|
2011-05-30 19:06:20 +02:00
|
|
|
|
2011-07-07 19:56:45 +02:00
|
|
|
// Latex is a type that implements the Renderer interface for LaTeX output.
|
|
|
|
//
|
|
|
|
// Do not create this directly, instead use the LatexRenderer function.
|
2011-06-29 19:13:17 +02:00
|
|
|
type Latex struct {
|
2016-04-01 14:37:21 +02:00
|
|
|
w HTMLWriter
|
2011-06-29 19:13:17 +02:00
|
|
|
}
|
|
|
|
|
2011-07-07 19:56:45 +02:00
|
|
|
// LatexRenderer creates and configures a Latex object, which
|
|
|
|
// satisfies the Renderer interface.
|
|
|
|
//
|
|
|
|
// flags is a set of LATEX_* options ORed together (currently no such options
|
|
|
|
// are defined).
|
2011-06-29 19:13:17 +02:00
|
|
|
func LatexRenderer(flags int) Renderer {
|
2016-04-01 14:37:21 +02:00
|
|
|
var writer HTMLWriter
|
2015-11-05 20:55:49 +01:00
|
|
|
return &Latex{
|
|
|
|
w: writer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-09 20:14:32 +01:00
|
|
|
func (r *Latex) Write(b []byte) (int, error) {
|
|
|
|
return r.w.Write(b)
|
|
|
|
}
|
|
|
|
|
2011-05-30 19:06:20 +02:00
|
|
|
// render code chunks using verbatim, or listings if we have a language
|
2015-11-01 20:27:55 +01:00
|
|
|
func (r *Latex) BlockCode(text []byte, lang string) {
|
2011-05-30 19:06:20 +02:00
|
|
|
if lang == "" {
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.WriteString("\n\\begin{verbatim}\n")
|
2011-05-30 19:06:20 +02:00
|
|
|
} else {
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.WriteString("\n\\begin{lstlisting}[language=")
|
|
|
|
r.w.WriteString(lang)
|
|
|
|
r.w.WriteString("]\n")
|
2011-05-30 19:06:20 +02:00
|
|
|
}
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.Write(text)
|
2011-05-30 19:06:20 +02:00
|
|
|
if lang == "" {
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.WriteString("\n\\end{verbatim}\n")
|
2011-05-30 19:06:20 +02:00
|
|
|
} else {
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.WriteString("\n\\end{lstlisting}\n")
|
2011-05-30 19:06:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-01 20:27:55 +01:00
|
|
|
func (r *Latex) TitleBlock(text []byte) {
|
2014-08-02 04:54:21 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-11-01 20:27:55 +01:00
|
|
|
func (r *Latex) BlockQuote(text []byte) {
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.WriteString("\n\\begin{quotation}\n")
|
|
|
|
r.w.Write(text)
|
|
|
|
r.w.WriteString("\n\\end{quotation}\n")
|
2011-05-30 19:06:20 +02:00
|
|
|
}
|
|
|
|
|
2015-11-01 20:27:55 +01:00
|
|
|
func (r *Latex) BlockHtml(text []byte) {
|
2011-06-29 18:43:10 +02:00
|
|
|
// a pretty lame thing to do...
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.WriteString("\n\\begin{verbatim}\n")
|
|
|
|
r.w.Write(text)
|
|
|
|
r.w.WriteString("\n\\end{verbatim}\n")
|
2011-06-29 18:43:10 +02:00
|
|
|
}
|
2011-05-30 19:06:20 +02:00
|
|
|
|
2015-11-04 21:14:02 +01:00
|
|
|
func (r *Latex) BeginHeader(level int, id string) {
|
2011-05-30 19:06:20 +02:00
|
|
|
switch level {
|
|
|
|
case 1:
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.WriteString("\n\\section{")
|
2011-05-30 19:06:20 +02:00
|
|
|
case 2:
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.WriteString("\n\\subsection{")
|
2011-05-30 19:06:20 +02:00
|
|
|
case 3:
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.WriteString("\n\\subsubsection{")
|
2011-05-30 19:06:20 +02:00
|
|
|
case 4:
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.WriteString("\n\\paragraph{")
|
2011-05-30 19:06:20 +02:00
|
|
|
case 5:
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.WriteString("\n\\subparagraph{")
|
2011-05-30 19:06:20 +02:00
|
|
|
case 6:
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.WriteString("\n\\textbf{")
|
2011-05-30 19:06:20 +02:00
|
|
|
}
|
2015-10-26 19:25:23 +01:00
|
|
|
}
|
|
|
|
|
2015-11-04 21:14:02 +01:00
|
|
|
func (r *Latex) EndHeader(level int, id string, header []byte) {
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.WriteString("}\n")
|
2011-05-30 19:06:20 +02:00
|
|
|
}
|
|
|
|
|
2015-11-01 20:27:55 +01:00
|
|
|
func (r *Latex) HRule() {
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.WriteString("\n\\HRule\n")
|
2011-05-30 19:06:20 +02:00
|
|
|
}
|
|
|
|
|
2015-11-01 20:27:55 +01:00
|
|
|
func (r *Latex) BeginList(flags ListType) {
|
2015-10-26 17:16:57 +01:00
|
|
|
if flags&ListTypeOrdered != 0 {
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.WriteString("\n\\begin{enumerate}\n")
|
2011-05-30 19:06:20 +02:00
|
|
|
} else {
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.WriteString("\n\\begin{itemize}\n")
|
2011-05-30 19:06:20 +02:00
|
|
|
}
|
2015-10-26 19:32:33 +01:00
|
|
|
}
|
|
|
|
|
2015-11-01 20:27:55 +01:00
|
|
|
func (r *Latex) EndList(flags ListType) {
|
2015-10-26 17:16:57 +01:00
|
|
|
if flags&ListTypeOrdered != 0 {
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.WriteString("\n\\end{enumerate}\n")
|
2011-05-30 19:06:20 +02:00
|
|
|
} else {
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.WriteString("\n\\end{itemize}\n")
|
2011-05-30 19:06:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-01 20:27:55 +01:00
|
|
|
func (r *Latex) ListItem(text []byte, flags ListType) {
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.WriteString("\n\\item ")
|
|
|
|
r.w.Write(text)
|
2011-05-30 19:06:20 +02:00
|
|
|
}
|
|
|
|
|
2015-11-01 20:27:55 +01:00
|
|
|
func (r *Latex) BeginParagraph() {
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.WriteString("\n")
|
2015-10-26 19:35:42 +01:00
|
|
|
}
|
|
|
|
|
2015-11-01 20:27:55 +01:00
|
|
|
func (r *Latex) EndParagraph() {
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.WriteString("\n")
|
2011-05-30 19:06:20 +02:00
|
|
|
}
|
|
|
|
|
2016-04-01 10:44:59 +02:00
|
|
|
func (r *Latex) Table(header []byte, body []byte, columnData []CellAlignFlags) {
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.WriteString("\n\\begin{tabular}{")
|
2011-05-30 19:06:20 +02:00
|
|
|
for _, elt := range columnData {
|
|
|
|
switch elt {
|
2015-10-26 17:16:57 +01:00
|
|
|
case TableAlignmentLeft:
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.WriteByte('l')
|
2015-10-26 17:16:57 +01:00
|
|
|
case TableAlignmentRight:
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.WriteByte('r')
|
2011-05-30 19:06:20 +02:00
|
|
|
default:
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.WriteByte('c')
|
2011-05-30 19:06:20 +02:00
|
|
|
}
|
|
|
|
}
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.WriteString("}\n")
|
|
|
|
r.w.Write(header)
|
|
|
|
r.w.WriteString(" \\\\\n\\hline\n")
|
|
|
|
r.w.Write(body)
|
|
|
|
r.w.WriteString("\n\\end{tabular}\n")
|
2011-05-30 19:06:20 +02:00
|
|
|
}
|
|
|
|
|
2015-11-01 20:27:55 +01:00
|
|
|
func (r *Latex) TableRow(text []byte) {
|
2016-03-30 14:56:53 +02:00
|
|
|
r.w.WriteString(" \\\\\n")
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.Write(text)
|
2011-05-30 19:06:20 +02:00
|
|
|
}
|
|
|
|
|
2016-04-01 10:44:59 +02:00
|
|
|
func (r *Latex) TableHeaderCell(out *bytes.Buffer, text []byte, align CellAlignFlags) {
|
2013-10-16 12:36:33 +02:00
|
|
|
if out.Len() > 0 {
|
|
|
|
out.WriteString(" & ")
|
|
|
|
}
|
|
|
|
out.Write(text)
|
|
|
|
}
|
|
|
|
|
2016-04-01 10:44:59 +02:00
|
|
|
func (r *Latex) TableCell(out *bytes.Buffer, text []byte, align CellAlignFlags) {
|
2011-05-30 19:06:20 +02:00
|
|
|
if out.Len() > 0 {
|
|
|
|
out.WriteString(" & ")
|
|
|
|
}
|
|
|
|
out.Write(text)
|
|
|
|
}
|
|
|
|
|
2013-06-25 03:18:47 +02:00
|
|
|
// TODO: this
|
2015-11-01 20:27:55 +01:00
|
|
|
func (r *Latex) BeginFootnotes() {
|
2015-10-26 19:39:08 +01:00
|
|
|
}
|
2013-06-26 17:57:51 +02:00
|
|
|
|
2015-10-26 19:39:08 +01:00
|
|
|
// TODO: this
|
2015-11-01 20:27:55 +01:00
|
|
|
func (r *Latex) EndFootnotes() {
|
2013-06-26 17:57:51 +02:00
|
|
|
}
|
|
|
|
|
2015-11-01 20:27:55 +01:00
|
|
|
func (r *Latex) FootnoteItem(name, text []byte, flags ListType) {
|
2013-06-25 03:18:47 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-11-01 20:27:55 +01:00
|
|
|
func (r *Latex) AutoLink(link []byte, kind LinkType) {
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.WriteString("\\href{")
|
2015-10-26 17:16:57 +01:00
|
|
|
if kind == LinkTypeEmail {
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.WriteString("mailto:")
|
2011-05-30 19:06:20 +02:00
|
|
|
}
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.Write(link)
|
|
|
|
r.w.WriteString("}{")
|
|
|
|
r.w.Write(link)
|
|
|
|
r.w.WriteString("}")
|
2011-05-30 19:06:20 +02:00
|
|
|
}
|
|
|
|
|
2015-11-01 20:27:55 +01:00
|
|
|
func (r *Latex) CodeSpan(text []byte) {
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.WriteString("\\texttt{")
|
|
|
|
r.escapeSpecialChars(text)
|
|
|
|
r.w.WriteString("}")
|
2011-05-30 19:06:20 +02:00
|
|
|
}
|
|
|
|
|
2015-11-01 20:27:55 +01:00
|
|
|
func (r *Latex) DoubleEmphasis(text []byte) {
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.WriteString("\\textbf{")
|
|
|
|
r.w.Write(text)
|
|
|
|
r.w.WriteString("}")
|
2011-05-30 19:06:20 +02:00
|
|
|
}
|
|
|
|
|
2015-11-01 20:27:55 +01:00
|
|
|
func (r *Latex) Emphasis(text []byte) {
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.WriteString("\\textit{")
|
|
|
|
r.w.Write(text)
|
|
|
|
r.w.WriteString("}")
|
2011-05-30 19:06:20 +02:00
|
|
|
}
|
|
|
|
|
2015-11-01 20:27:55 +01:00
|
|
|
func (r *Latex) Image(link []byte, title []byte, alt []byte) {
|
2011-05-30 19:06:20 +02:00
|
|
|
if bytes.HasPrefix(link, []byte("http://")) || bytes.HasPrefix(link, []byte("https://")) {
|
|
|
|
// treat it like a link
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.WriteString("\\href{")
|
|
|
|
r.w.Write(link)
|
|
|
|
r.w.WriteString("}{")
|
|
|
|
r.w.Write(alt)
|
|
|
|
r.w.WriteString("}")
|
2011-05-30 19:06:20 +02:00
|
|
|
} else {
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.WriteString("\\includegraphics{")
|
|
|
|
r.w.Write(link)
|
|
|
|
r.w.WriteString("}")
|
2011-05-30 19:06:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-01 20:27:55 +01:00
|
|
|
func (r *Latex) LineBreak() {
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.WriteString(" \\\\\n")
|
2011-05-30 19:06:20 +02:00
|
|
|
}
|
|
|
|
|
2015-11-01 20:27:55 +01:00
|
|
|
func (r *Latex) Link(link []byte, title []byte, content []byte) {
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.WriteString("\\href{")
|
|
|
|
r.w.Write(link)
|
|
|
|
r.w.WriteString("}{")
|
|
|
|
r.w.Write(content)
|
|
|
|
r.w.WriteString("}")
|
2011-05-30 19:06:20 +02:00
|
|
|
}
|
|
|
|
|
2015-11-01 20:27:55 +01:00
|
|
|
func (r *Latex) RawHtmlTag(tag []byte) {
|
2011-05-30 19:06:20 +02:00
|
|
|
}
|
|
|
|
|
2015-11-01 20:27:55 +01:00
|
|
|
func (r *Latex) TripleEmphasis(text []byte) {
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.WriteString("\\textbf{\\textit{")
|
|
|
|
r.w.Write(text)
|
|
|
|
r.w.WriteString("}}")
|
2011-05-30 19:06:20 +02:00
|
|
|
}
|
|
|
|
|
2015-11-01 20:27:55 +01:00
|
|
|
func (r *Latex) StrikeThrough(text []byte) {
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.WriteString("\\sout{")
|
|
|
|
r.w.Write(text)
|
|
|
|
r.w.WriteString("}")
|
2011-05-30 19:06:20 +02:00
|
|
|
}
|
|
|
|
|
2013-06-25 03:18:47 +02:00
|
|
|
// TODO: this
|
2015-11-01 20:27:55 +01:00
|
|
|
func (r *Latex) FootnoteRef(ref []byte, id int) {
|
2013-06-25 03:18:47 +02:00
|
|
|
}
|
|
|
|
|
2011-05-30 19:06:20 +02:00
|
|
|
func needsBackslash(c byte) bool {
|
2015-07-20 21:48:36 +02:00
|
|
|
for _, r := range []byte("_{}%$&\\~#") {
|
2011-05-30 19:06:20 +02:00
|
|
|
if c == r {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-11-05 20:55:49 +01:00
|
|
|
func (r *Latex) escapeSpecialChars(text []byte) {
|
2011-05-30 19:06:20 +02:00
|
|
|
for i := 0; i < len(text); i++ {
|
|
|
|
// directly copy normal characters
|
|
|
|
org := i
|
|
|
|
|
|
|
|
for i < len(text) && !needsBackslash(text[i]) {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
if i > org {
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.Write(text[org:i])
|
2011-05-30 19:06:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// escape a character
|
|
|
|
if i >= len(text) {
|
|
|
|
break
|
|
|
|
}
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.WriteByte('\\')
|
|
|
|
r.w.WriteByte(text[i])
|
2011-05-30 19:06:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-01 20:27:55 +01:00
|
|
|
func (r *Latex) Entity(entity []byte) {
|
2011-06-29 18:43:10 +02:00
|
|
|
// TODO: convert this into a unicode character or something
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.Write(entity)
|
2011-06-29 18:43:10 +02:00
|
|
|
}
|
|
|
|
|
2015-11-01 20:27:55 +01:00
|
|
|
func (r *Latex) NormalText(text []byte) {
|
2015-11-05 20:55:49 +01:00
|
|
|
r.escapeSpecialChars(text)
|
2011-05-30 19:06:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// header and footer
|
2015-11-01 20:27:55 +01:00
|
|
|
func (r *Latex) DocumentHeader() {
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.WriteString("\\documentclass{article}\n")
|
|
|
|
r.w.WriteString("\n")
|
|
|
|
r.w.WriteString("\\usepackage{graphicx}\n")
|
|
|
|
r.w.WriteString("\\usepackage{listings}\n")
|
|
|
|
r.w.WriteString("\\usepackage[margin=1in]{geometry}\n")
|
|
|
|
r.w.WriteString("\\usepackage[utf8]{inputenc}\n")
|
|
|
|
r.w.WriteString("\\usepackage{verbatim}\n")
|
|
|
|
r.w.WriteString("\\usepackage[normalem]{ulem}\n")
|
|
|
|
r.w.WriteString("\\usepackage{hyperref}\n")
|
|
|
|
r.w.WriteString("\n")
|
|
|
|
r.w.WriteString("\\hypersetup{colorlinks,%\n")
|
|
|
|
r.w.WriteString(" citecolor=black,%\n")
|
|
|
|
r.w.WriteString(" filecolor=black,%\n")
|
|
|
|
r.w.WriteString(" linkcolor=black,%\n")
|
|
|
|
r.w.WriteString(" urlcolor=black,%\n")
|
|
|
|
r.w.WriteString(" pdfstartview=FitH,%\n")
|
|
|
|
r.w.WriteString(" breaklinks=true,%\n")
|
|
|
|
r.w.WriteString(" pdfauthor={Blackfriday Markdown Processor v")
|
|
|
|
r.w.WriteString(VERSION)
|
|
|
|
r.w.WriteString("}}\n")
|
|
|
|
r.w.WriteString("\n")
|
|
|
|
r.w.WriteString("\\newcommand{\\HRule}{\\rule{\\linewidth}{0.5mm}}\n")
|
|
|
|
r.w.WriteString("\\addtolength{\\parskip}{0.5\\baselineskip}\n")
|
|
|
|
r.w.WriteString("\\parindent=0pt\n")
|
|
|
|
r.w.WriteString("\n")
|
|
|
|
r.w.WriteString("\\begin{document}\n")
|
2011-05-30 19:06:20 +02:00
|
|
|
}
|
|
|
|
|
2015-11-01 20:27:55 +01:00
|
|
|
func (r *Latex) DocumentFooter() {
|
2015-11-05 20:55:49 +01:00
|
|
|
r.w.WriteString("\n\\end{document}\n")
|
2011-05-30 19:06:20 +02:00
|
|
|
}
|
2016-03-30 13:47:30 +02:00
|
|
|
|
|
|
|
func (r *Latex) Render(ast *Node) []byte {
|
|
|
|
return nil
|
|
|
|
}
|