# Markdown document renderer to LaTeX
class LatexRenderer
super MdRenderer
# Generate the LaTeX document wrapper
#
# The header includes:
# * document class
# * package importation
# * begin and end document directives
var wrap_document = false is optional, writable
# LaTeX document class
#
# Default is `article`.
var document_class = "article" is optional, writable
# LaTeX document page format
#
# Default is `letter`.
var page_format = "letter" is optional, writable
# LaTeX font size
#
# Default is `10pt`.
var font_size = "10pt" is optional, writable
# Use `listings` package for code blocks?
var use_listings = false is optional, writable
# LaTeX output under construction
private var latex: Buffer is noinit
# Render `document` as LaTeX
redef fun render(document) do
latex = new Buffer
enter_visit(document)
return latex.write_to_string
end
redef fun visit(node) do node.render_latex(self)
# Indentation level
var indent = 0
# Add a raw `string` to the output
#
# Raw means that the string will not be escaped.
fun add_raw(string: String) do latex.append string
# Add `text` string to the output
#
# The string will be escaped.
fun add_text(text: String) do latex.append latex_escape(text)
# Add a blank line to the output
fun add_line do
if not latex.is_empty and latex.last != '\n' then
latex.add '\n'
end
end
# Add an indentation depending on `ident` level
fun add_indent do latex.append " " * indent
# Escape `string` to LaTeX
fun latex_escape(string: String): String do
var buffer = new Buffer
for i in [0 .. string.length[ do
var c = string.chars[i]
if c == '>' then
buffer.append "\\textgreater"
continue
else if c == '<' then
buffer.append "\\textless"
continue
else if c == '\\' then
buffer.append "\\textbackslash"
continue
else if escaped_chars.has(c) then
buffer.add '\\'
end
buffer.add c
end
return buffer.to_s
end
# LaTeX characters to escape
var escaped_chars = ['%', '$', '{', '}', '_', '#', '&']
end
lib/markdown2/markdown_latex_rendering.nit:22,1--111,3