Rename escape_unicode and move to tex based formatdicts

pull/244/head
Sebastian Pech 2019-10-15 21:48:05 +02:00
parent 0018f0e24c
commit c9e26d5638
6 changed files with 60 additions and 31 deletions

View File

@ -84,7 +84,9 @@ Weave an input document to output file.
* `pandoc_options`: String array of options to pass to pandoc for `pandoc2html` and * `pandoc_options`: String array of options to pass to pandoc for `pandoc2html` and
`pandoc2pdf` formats e.g. ["--toc", "-N"] `pandoc2pdf` formats e.g. ["--toc", "-N"]
* `latex_cmd`: the command used to make pdf from .tex * `latex_cmd`: the command used to make pdf from .tex
* `escape_unicode`: if set to true (default), try to convert unicode characters to respective LaTeX command * `latex_keep_unicode`: if set to true (default is false), do not convert unicode characters to their
respective latex representation. This is especially useful if a font and tex-engine with support for unicode
characters are used.
**Note:** Run Weave from terminal and not using IJulia, Juno or ESS, they tend to mess with capturing output. **Note:** Run Weave from terminal and not using IJulia, Juno or ESS, they tend to mess with capturing output.
""" """
@ -96,14 +98,12 @@ function weave(source ; doctype = :auto,
throw_errors = false, throw_errors = false,
template = nothing, highlight_theme = nothing, css = nothing, template = nothing, highlight_theme = nothing, css = nothing,
pandoc_options = String[]::Array{String}, pandoc_options = String[]::Array{String},
latex_cmd = "xelatex",escape_unicode=true) latex_cmd = "xelatex",latex_keep_unicode=false)
doc = read_doc(source, informat) doc = read_doc(source, informat)
doctype == :auto && (doctype = detect_doctype(doc.source)) doctype == :auto && (doctype = detect_doctype(doc.source))
doc.doctype = doctype doc.doctype = doctype
# Set unicode escape variable
doc.escape_unicode = escape_unicode
# Read args from document header, overrides command line args # Read args from document header, overrides command line args
if haskey(doc.header, "options") if haskey(doc.header, "options")
@ -126,7 +126,7 @@ function weave(source ; doctype = :auto,
mod = mod, mod = mod,
out_path=out_path, args = args, out_path=out_path, args = args,
fig_path = fig_path, fig_ext = fig_ext, cache_path = cache_path, cache=cache, fig_path = fig_path, fig_ext = fig_ext, cache_path = cache_path, cache=cache,
throw_errors = throw_errors) throw_errors = throw_errors,latex_keep_unicode=latex_keep_unicode)
formatted = format(doc) formatted = format(doc)
outname = get_outname(out_path, doc) outname = get_outname(out_path, doc)

View File

@ -19,12 +19,11 @@ mutable struct WeaveDoc
highlight_theme highlight_theme
fig_path::AbstractString fig_path::AbstractString
chunk_defaults::Dict{Symbol,Any} chunk_defaults::Dict{Symbol,Any}
escape_unicode::Bool
function WeaveDoc(source, chunks, header) function WeaveDoc(source, chunks, header)
path, fname = splitdir(abspath(source)) path, fname = splitdir(abspath(source))
basename = splitext(fname)[1] basename = splitext(fname)[1]
new(source, basename, path, chunks, "", nothing, "", "", header, new(source, basename, path, chunks, "", nothing, "", "", header,
"", "", Highlights.Themes.DefaultTheme, "", deepcopy(rcParams[:chunk_defaults]),true) "", "", Highlights.Themes.DefaultTheme, "", deepcopy(rcParams[:chunk_defaults]))
end end
end end

View File

@ -30,7 +30,7 @@ function format(doc::WeaveDoc)
end end
for chunk in copy(doc.chunks) for chunk in copy(doc.chunks)
result = format_chunk(chunk, formatdict, docformat; escape_unicode=doc.escape_unicode) result = format_chunk(chunk, formatdict, docformat)
push!(formatted, result) push!(formatted, result)
end end
@ -114,7 +114,7 @@ function strip_header(chunk::DocChunk)
return chunk return chunk
end end
function format_chunk(chunk::DocChunk, formatdict, docformat; escape_unicode=true) function format_chunk(chunk::DocChunk, formatdict, docformat)
return join([format_inline(c) for c in chunk.content], "") return join([format_inline(c) for c in chunk.content], "")
end end
@ -141,7 +141,7 @@ function addspace(op, inline)
return op return op
end end
function format_chunk(chunk::DocChunk, formatdict, docformat::JMarkdown2tex; escape_unicode=true) function format_chunk(chunk::DocChunk, formatdict, docformat::JMarkdown2tex)
out = IOBuffer() out = IOBuffer()
io = IOBuffer() io = IOBuffer()
for inline in chunk.content for inline in chunk.content
@ -157,11 +157,11 @@ function format_chunk(chunk::DocChunk, formatdict, docformat::JMarkdown2tex; esc
end end
end end
ioformat!(io, out) ioformat!(io, out)
escape_unicode && return uc2tex(String(take!(out))) formatdict[:keep_unicode] || return uc2tex(String(take!(out)))
return String(take!(out)) return String(take!(out))
end end
function format_chunk(chunk::DocChunk, formatdict, docformat::JMarkdown2HTML; escape_unicode=true) function format_chunk(chunk::DocChunk, formatdict, docformat::JMarkdown2HTML)
out = IOBuffer() out = IOBuffer()
io = IOBuffer() io = IOBuffer()
fun = WeaveMarkdown.html fun = WeaveMarkdown.html
@ -181,7 +181,7 @@ function format_chunk(chunk::DocChunk, formatdict, docformat::JMarkdown2HTML; es
return String(take!(out)) return String(take!(out))
end end
function format_chunk(chunk::CodeChunk, formatdict, docformat; escape_unicode=true) function format_chunk(chunk::CodeChunk, formatdict, docformat)
#Fill undefined options with format specific defaults #Fill undefined options with format specific defaults
chunk.options[:out_width] == nothing && chunk.options[:out_width] == nothing &&
(chunk.options[:out_width] = formatdict[:out_width]) (chunk.options[:out_width] = formatdict[:out_width])
@ -197,7 +197,7 @@ function format_chunk(chunk::CodeChunk, formatdict, docformat; escape_unicode=tr
chunk.content = indent(chunk.content, formatdict[:indent]) chunk.content = indent(chunk.content, formatdict[:indent])
end end
chunk.content = format_code(chunk.content, docformat; escape_unicode=escape_unicode) chunk.content = format_code(chunk.content, docformat)
if !chunk.options[:eval] if !chunk.options[:eval]
if chunk.options[:echo] if chunk.options[:echo]
@ -227,10 +227,10 @@ function format_chunk(chunk::CodeChunk, formatdict, docformat; escape_unicode=tr
else else
if chunk.options[:wrap] if chunk.options[:wrap]
chunk.output = "\n" * wraplines(chunk.output, chunk.options[:line_width]) chunk.output = "\n" * wraplines(chunk.output, chunk.options[:line_width])
chunk.output = format_output(chunk.output, docformat, escape_unicode=escape_unicode) chunk.output = format_output(chunk.output, docformat)
else else
chunk.output = "\n" * rstrip(chunk.output) chunk.output = "\n" * rstrip(chunk.output)
chunk.output = format_output(chunk.output, docformat, escape_unicode=escape_unicode) chunk.output = format_output(chunk.output, docformat)
end end
if haskey(formatdict, :indent) if haskey(formatdict, :indent)
@ -254,29 +254,29 @@ function format_chunk(chunk::CodeChunk, formatdict, docformat; escape_unicode=tr
return result return result
end end
function format_output(result::AbstractString, docformat;escape_unicode=true) function format_output(result::AbstractString, docformat)
return result return result
end end
function format_output(result::AbstractString, docformat::JMarkdown2HTML;escape_unicode=true) function format_output(result::AbstractString, docformat::JMarkdown2HTML)
return Markdown.htmlesc(result) return Markdown.htmlesc(result)
end end
function format_output(result::AbstractString, docformat::JMarkdown2tex;escape_unicode=true) function format_output(result::AbstractString, docformat::JMarkdown2tex)
# Highligts has some extra escaping defined, eg of $, ", ... # Highligts has some extra escaping defined, eg of $, ", ...
result_escaped = sprint( (io, x) -> Highlights.Format.escape(io, MIME("text/latex"), x, charescape=true), result) result_escaped = sprint( (io, x) -> Highlights.Format.escape(io, MIME("text/latex"), x, charescape=true), result)
escape_unicode && return uc2tex(result_escaped, true) docformat.formatdict[:keep_unicode] || return uc2tex(result_escaped, true)
return result_escaped return result_escaped
end end
function format_code(result::AbstractString, docformat;escape_unicode=true) function format_code(result::AbstractString, docformat)
return result return result
end end
function format_code(result::AbstractString, docformat::JMarkdown2tex;escape_unicode=true) function format_code(result::AbstractString, docformat::JMarkdown2tex)
highlighted = highlight(MIME("text/latex"), strip(result), highlighted = highlight(MIME("text/latex"), strip(result),
Highlights.Lexers.JuliaLexer, docformat.formatdict[:theme]) Highlights.Lexers.JuliaLexer, docformat.formatdict[:theme])
escape_unicode && return uc2tex(highlighted) docformat.formatdict[:keep_unicode] || return uc2tex(highlighted)
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 end
@ -310,12 +310,12 @@ function texify(s)
return ts return ts
end end
function format_code(result::AbstractString, docformat::JMarkdown2HTML;escape_unicode=true) function format_code(result::AbstractString, docformat::JMarkdown2HTML)
return highlight(MIME("text/html"), strip(result), return highlight(MIME("text/html"), strip(result),
Highlights.Lexers.JuliaLexer, docformat.formatdict[:theme]) Highlights.Lexers.JuliaLexer, docformat.formatdict[:theme])
end end
function format_code(result::AbstractString, docformat::Pandoc2HTML;escape_unicode=true) function format_code(result::AbstractString, docformat::Pandoc2HTML)
return highlight(MIME("text/html"), strip(result), return highlight(MIME("text/html"), strip(result),
Highlights.Lexers.JuliaLexer, docformat.formatdict[:theme]) Highlights.Lexers.JuliaLexer, docformat.formatdict[:theme])
end end

View File

@ -18,7 +18,8 @@ const tex = Tex("Latex with custom code environments",
:fig_env=> "figure", :fig_env=> "figure",
:fig_pos => "htpb", :fig_pos => "htpb",
:doctype => "tex", :doctype => "tex",
:mimetypes => ["application/pdf", "image/png", "text/latex", "text/plain"] :mimetypes => ["application/pdf", "image/png", "text/latex", "text/plain"],
:keep_unicode => false,
)) ))
const texminted = Tex("Latex using minted for highlighting", const texminted = Tex("Latex using minted for highlighting",
@ -35,7 +36,8 @@ const texminted = Tex("Latex using minted for highlighting",
:fig_env=> "figure", :fig_env=> "figure",
:fig_pos => "htpb", :fig_pos => "htpb",
:doctype => "texminted", :doctype => "texminted",
:mimetypes => ["application/pdf", "image/png", "text/latex", "text/plain"] :mimetypes => ["application/pdf", "image/png", "text/latex", "text/plain"],
:keep_unicode => false,
)) ))
struct Pandoc struct Pandoc
@ -147,7 +149,8 @@ const md2tex = JMarkdown2tex("Julia markdown to latex", Dict{Symbol,Any}(
:out_width => "\\linewidth", :out_width => "\\linewidth",
:mimetypes => ["application/pdf", "image/png", "image/jpg", "text/latex", :mimetypes => ["application/pdf", "image/png", "image/jpg", "text/latex",
"text/markdown", "text/plain"], "text/markdown", "text/plain"],
:doctype=> "md2tex")) :doctype=> "md2tex",
:keep_unicode=>false))
struct MultiMarkdown struct MultiMarkdown

View File

@ -26,13 +26,18 @@ Run code chunks and capture output from parsed document.
function Base.run(doc::WeaveDoc; doctype = :auto, function Base.run(doc::WeaveDoc; doctype = :auto,
mod::Union{Module, Symbol} = :sandbox, out_path=:doc, mod::Union{Module, Symbol} = :sandbox, out_path=:doc,
args=Dict(), fig_path = "figures", fig_ext = nothing, args=Dict(), fig_path = "figures", fig_ext = nothing,
cache_path = "cache", cache = :off, throw_errors=false) cache_path = "cache", cache = :off, throw_errors=false, latex_keep_unicode=false)
#cache :all, :user, :off, :refresh #cache :all, :user, :off, :refresh
doc.cwd = get_cwd(doc, out_path) doc.cwd = get_cwd(doc, out_path)
doctype == :auto && (doctype = detect_doctype(doc.source)) doctype == :auto && (doctype = detect_doctype(doc.source))
doc.doctype = doctype doc.doctype = doctype
doc.format = formats[doctype] doc.format = formats[doctype]
if (haskey(doc.format.formatdict, :keep_unicode))
doc.format.formatdict[:keep_unicode] = latex_keep_unicode
end
isdir(doc.cwd) || mkpath(doc.cwd) isdir(doc.cwd) || mkpath(doc.cwd)
if occursin("2pdf", doctype) && cache == :off if occursin("2pdf", doctype) && cache == :off

View File

@ -126,6 +126,28 @@ pformat = Weave.formats["md2tex"]
f = Weave.format_chunk(dchunk, pformat.formatdict, pformat) f = Weave.format_chunk(dchunk, pformat.formatdict, pformat)
@test f == "\\section{Test chunk}\n\\ensuremath{\\alpha}\n\n" @test f == "\\section{Test chunk}\n\\ensuremath{\\alpha}\n\n"
pformat.formatdict[:keep_unicode] = true
f = Weave.format_chunk(dchunk, pformat.formatdict, pformat,escape_unicode=false) f = Weave.format_chunk(dchunk, pformat.formatdict, pformat)
@test f == "\\section{Test chunk}\nα\n\n" @test f == "\\section{Test chunk}\nα\n\n"
function doc_from_string(str)
parsed = Weave.parse_doc(str,"markdown")
header = Weave.parse_header(parsed[1])
Weave.WeaveDoc("",parsed,header)
end
doc_content = """
```julia
α = 10
```
"""
parsed = doc_from_string(doc_content)
ldoc = Weave.run(parsed, doctype = "md2tex")
@test occursin(Weave.uc2tex("α"),Weave.format(ldoc))
@test !occursin("α",Weave.format(ldoc))
parsed = doc_from_string(doc_content)
ldoc = Weave.run(parsed, doctype = "md2tex",latex_keep_unicode=true)
@test occursin("α",Weave.format(ldoc))
@test !occursin(Weave.uc2tex("α"),Weave.format(ldoc))