Use Highlights.jl for md2tex output

pull/137/head
Matti Pastell 2018-07-24 23:40:47 +02:00
parent 57724aff67
commit 4523b3a58e
3 changed files with 38 additions and 46 deletions

View File

@ -3,6 +3,7 @@ import .Markdown2HTML
using Compat using Compat
using Dates using Dates
using Markdown using Markdown
using REPL.REPLCompletions: latex_symbols
function format(doc::WeaveDoc) function format(doc::WeaveDoc)
formatted = AbstractString[] formatted = AbstractString[]
@ -93,6 +94,7 @@ function render_doc(formatted, doc::WeaveDoc, format::JMarkdown2tex)
wversion = "" wversion = ""
wtime = string(Date(now())) wtime = string(Date(now()))
if isempty(doc.template) if isempty(doc.template)
template = Mustache.template_from_file(joinpath(dirname(@__FILE__), "../templates/julia_tex.tpl")) template = Mustache.template_from_file(joinpath(dirname(@__FILE__), "../templates/julia_tex.tpl"))
else else
@ -141,12 +143,10 @@ function format_chunk(chunk::DocChunk, formatdict, docformat::JMarkdown2HTML)
return string(Markdown2HTML.html(m)) return string(Markdown2HTML.html(m))
end end
function format_chunk(chunk::DocChunk, formatdict, docformat::JMarkdown2tex) function format_chunk(chunk::DocChunk, formatdict, docformat::JMarkdown2tex)
text = format_chunk(chunk, formatdict, nothing) text = format_chunk(chunk, formatdict, nothing)
m = Markdown.parse(text) m = Markdown.parse(text)
return Markdown.latex(m) return uc2tex(Markdown.latex(m))
end end
function format_chunk(chunk::CodeChunk, formatdict, docformat) function format_chunk(chunk::CodeChunk, formatdict, docformat)
@ -161,7 +161,6 @@ function format_chunk(chunk::CodeChunk, formatdict, docformat)
(chunk.options[:fig_env] = formatdict[:fig_env]) (chunk.options[:fig_env] = formatdict[:fig_env])
end end
if haskey(formatdict, :indent) if haskey(formatdict, :indent)
chunk.content = indent(chunk.content, formatdict[:indent]) chunk.content = indent(chunk.content, formatdict[:indent])
end end
@ -224,11 +223,15 @@ function format_chunk(chunk::CodeChunk, formatdict, docformat)
end end
function format_output(result::AbstractString, docformat) function format_output(result::AbstractString, docformat)
return(result) return result
end end
function format_output(result::AbstractString, docformat::JMarkdown2HTML) function format_output(result::AbstractString, docformat::JMarkdown2HTML)
return(Markdown.htmlesc(result)) return Markdown.htmlesc(result)
end
function format_output(result::AbstractString, docformat::JMarkdown2tex)
return uc2tex(result, true)
end end
function format_code(result::AbstractString, docformat) function format_code(result::AbstractString, docformat)
@ -236,14 +239,26 @@ function format_code(result::AbstractString, docformat)
end end
function format_code(result::AbstractString, docformat::JMarkdown2tex) function format_code(result::AbstractString, docformat::JMarkdown2tex)
#buf = PipeBuffer() buf = PipeBuffer()
#Highlights.highlight(buf, MIME("text/latex"), strip(result), Highlights.highlight(buf, MIME("text/latex"), strip(result),
# Highlights.Lexers.JuliaLexer, docformat.formatdict[:theme]) Highlights.Lexers.JuliaLexer, docformat.formatdict[:theme])
#flush(buf) flush(buf)
#highlighted = readstring(buf) highlighted = uc2tex(read(buf, String))
#close(buf) close(buf)
#return highlighted return highlighted
return "\\begin{minted}[mathescape, fontsize=\\small, xleftmargin=0.5em]{julia}\n$result\n\\end{minted}\n" #return "\\begin{minted}[mathescape, fontsize=\\small, xleftmargin=0.5em]{julia}\n$result\n\\end{minted}\n"
end
#Convert unicode to tex, escape listings if needed
function uc2tex(s, escape=false)
for key in keys(latex_symbols)
if escape
s = replace(s, latex_symbols[key] => "(*@\\ensuremath{$key}@*)")
else
s = replace(s, latex_symbols[key] => "\\ensuremath{$key}")
end
end
return s
end end
function format_code(result::AbstractString, docformat::JMarkdown2HTML) function format_code(result::AbstractString, docformat::JMarkdown2HTML)
@ -305,13 +320,13 @@ end
function format_termchunk(chunk, formatdict, docformat::JMarkdown2tex) function format_termchunk(chunk, formatdict, docformat::JMarkdown2tex)
if chunk.options[:echo] && chunk.options[:results] != "hidden" if chunk.options[:echo] && chunk.options[:results] != "hidden"
#buf = PipeBuffer() buf = PipeBuffer()
#Highlights.highlight(buf, MIME("text/latex"), strip(chunk.output), Highlights.Lexers.JuliaConsoleLexer) Highlights.highlight(buf, MIME("text/latex"), strip(chunk.output), Highlights.Lexers.JuliaConsoleLexer)
#flush(buf) flush(buf)
#result = readstring(buf) result = read(buf, String)
#close(buf) close(buf)
result = strip(chunk.output) result = strip(chunk.output)
return "\\begin{minted}[mathescape, fontsize=\\small, xleftmargin=0.5em]{julia}\n$result\n\\end{minted}\n" #return "\\begin{minted}[mathescape, fontsize=\\small, xleftmargin=0.5em]{julia}\n$result\n\\end{minted}\n"
else else
result = "" result = ""
end end

View File

@ -139,8 +139,8 @@ end
const md2tex = JMarkdown2tex("Julia markdown to latex", Dict{Symbol,Any}( const md2tex = JMarkdown2tex("Julia markdown to latex", Dict{Symbol,Any}(
:codestart => "", :codestart => "",
:codeend=> "", :codeend=> "",
:outputstart=> "\\begin{minted}[fontsize=\\small, xleftmargin=0.5em, mathescape, frame = leftline]{text}", :outputstart=> "\\begin{lstlisting}",
:outputend=> "\\end{minted}\n", :outputend=> "\\end{lstlisting}\n",
:fig_ext=> ".pdf", :fig_ext=> ".pdf",
:extension=> "tex", :extension=> "tex",
:out_width => "\\linewidth", :out_width => "\\linewidth",
@ -468,7 +468,6 @@ function formatfigures(chunk, docformat::MultiMarkdown)
return result return result
end end
function formatfigures(chunk, docformat::Rest) function formatfigures(chunk, docformat::Rest)
fignames = chunk.figures fignames = chunk.figures
caption = chunk.options[:fig_cap] caption = chunk.options[:fig_cap]
@ -490,7 +489,6 @@ function formatfigures(chunk, docformat::Rest)
end end
end end
function formatfigures(chunk, docformat::AsciiDoc) function formatfigures(chunk, docformat::AsciiDoc)
fignames = chunk.figures fignames = chunk.figures
caption = chunk.options[:fig_cap] caption = chunk.options[:fig_cap]
@ -514,7 +512,6 @@ function formatfigures(chunk, docformat::AsciiDoc)
end end
end end
#Add new supported formats here #Add new supported formats here
const formats = Dict{AbstractString, Any}("tex" => tex, const formats = Dict{AbstractString, Any}("tex" => tex,
"texminted" => texminted, "texminted" => texminted,

View File

@ -8,19 +8,6 @@
\usepackage{hyperref} \usepackage{hyperref}
\setlength{\parindent}{0pt} \setlength{\parindent}{0pt}
\setlength{\parskip}{1.2ex} \setlength{\parskip}{1.2ex}
\usepackage{minted}
\usepackage{ifxetex,ifluatex}
\newif\ifxetexorluatex
\ifxetex
\xetexorluatextrue
\else
\ifluatex
\xetexorluatextrue
\else
\xetexorluatexfalse
\fi
\fi
\hypersetup \hypersetup
{ pdfauthor = { {{{:author}}} }, { pdfauthor = { {{{:author}}} },
@ -31,7 +18,6 @@
urlcolor=blue urlcolor=blue
} }
{{#:title}} {{#:title}}
\title{ {{{ :title }}} } \title{ {{{ :title }}} }
{{/:title}} {{/:title}}
@ -44,13 +30,7 @@
\date{ {{{ :date }}} } \date{ {{{ :date }}} }
{{/:date}} {{/:date}}
%\ifxetexorluatex {{ :highlight }}
% \usepackage{fontspec}
% \setmainfont{CMU Serif Roman}
% \setmonofont{CMU Typewriter Text}
% \usepackage{unicode-math}
% \setmathfont{Latin Modern Math}
%\fi
\begin{document} \begin{document}