Skip to content

Commit

Permalink
Add argument for disabling unicode escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianpech committed Sep 5, 2019
1 parent 9fa2872 commit bd5ff70
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 20 deletions.
5 changes: 4 additions & 1 deletion src/Weave.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,15 @@ function weave(source ; doctype = :auto,
throw_errors = false,
template = nothing, highlight_theme = nothing, css = nothing,
pandoc_options = String[]::Array{String},
latex_cmd = "xelatex")
latex_cmd = "xelatex",escape_unicode=true)

doc = read_doc(source, informat)
doctype == :auto && (doctype = detect_doctype(doc.source))
doc.doctype = doctype

# Set unicode escape variable
doc.escape_unicode = escape_unicode

# Read args from document header, overrides command line args
if haskey(doc.header, "options")
(doctype, informat, out_path, args, mod, fig_path, fig_ext,
Expand Down
3 changes: 2 additions & 1 deletion src/chunks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ mutable struct WeaveDoc
highlight_theme
fig_path::AbstractString
chunk_defaults::Dict{Symbol,Any}
escape_unicode::Bool
function WeaveDoc(source, chunks, header)
path, fname = splitdir(abspath(source))
basename = splitext(fname)[1]
new(source, basename, path, chunks, "", nothing, "", "", header,
"", "", Highlights.Themes.DefaultTheme, "", deepcopy(rcParams[:chunk_defaults]))
"", "", Highlights.Themes.DefaultTheme, "", deepcopy(rcParams[:chunk_defaults]),true)
end
end

Expand Down
41 changes: 23 additions & 18 deletions src/format.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function format(doc::WeaveDoc)
end

for chunk in copy(doc.chunks)
result = format_chunk(chunk, formatdict, docformat)
result = format_chunk(chunk, formatdict, docformat; escape_unicode=doc.escape_unicode)
push!(formatted, result)
end

Expand Down Expand Up @@ -114,7 +114,7 @@ function strip_header(chunk::DocChunk)
return chunk
end

function format_chunk(chunk::DocChunk, formatdict, docformat)
function format_chunk(chunk::DocChunk, formatdict, docformat; escape_unicode=true)
return join([format_inline(c) for c in chunk.content], "")
end

Expand All @@ -141,7 +141,7 @@ function addspace(op, inline)
return op
end

function format_chunk(chunk::DocChunk, formatdict, docformat::JMarkdown2tex)
function format_chunk(chunk::DocChunk, formatdict, docformat::JMarkdown2tex; escape_unicode=true)
out = IOBuffer()
io = IOBuffer()
for inline in chunk.content
Expand All @@ -157,10 +157,11 @@ function format_chunk(chunk::DocChunk, formatdict, docformat::JMarkdown2tex)
end
end
ioformat!(io, out)
return uc2tex(String(take!(out)))
escape_unicode && return uc2tex(String(take!(out)))
return String(take!(out))
end

function format_chunk(chunk::DocChunk, formatdict, docformat::JMarkdown2HTML)
function format_chunk(chunk::DocChunk, formatdict, docformat::JMarkdown2HTML; escape_unicode=true)
out = IOBuffer()
io = IOBuffer()
fun = WeaveMarkdown.html
Expand All @@ -180,7 +181,7 @@ function format_chunk(chunk::DocChunk, formatdict, docformat::JMarkdown2HTML)
return String(take!(out))
end

function format_chunk(chunk::CodeChunk, formatdict, docformat)
function format_chunk(chunk::CodeChunk, formatdict, docformat; escape_unicode=true)
#Fill undefined options with format specific defaults
chunk.options[:out_width] == nothing &&
(chunk.options[:out_width] = formatdict[:out_width])
Expand All @@ -196,7 +197,7 @@ function format_chunk(chunk::CodeChunk, formatdict, docformat)
chunk.content = indent(chunk.content, formatdict[:indent])
end

chunk.content = format_code(chunk.content, docformat)
chunk.content = format_code(chunk.content, docformat; escape_unicode=escape_unicode)

if !chunk.options[:eval]
if chunk.options[:echo]
Expand Down Expand Up @@ -226,10 +227,10 @@ function format_chunk(chunk::CodeChunk, formatdict, docformat)
else
if chunk.options[:wrap]
chunk.output = "\n" * wraplines(chunk.output, chunk.options[:line_width])
chunk.output = format_output(chunk.output, docformat)
chunk.output = format_output(chunk.output, docformat, escape_unicode=escape_unicode)
else
chunk.output = "\n" * rstrip(chunk.output)
chunk.output = format_output(chunk.output, docformat)
chunk.output = format_output(chunk.output, docformat, escape_unicode=escape_unicode)
end

if haskey(formatdict, :indent)
Expand All @@ -253,26 +254,30 @@ function format_chunk(chunk::CodeChunk, formatdict, docformat)
return result
end

function format_output(result::AbstractString, docformat)
function format_output(result::AbstractString, docformat;escape_unicode=true)
return result
end

function format_output(result::AbstractString, docformat::JMarkdown2HTML)
function format_output(result::AbstractString, docformat::JMarkdown2HTML;escape_unicode=true)
return Markdown.htmlesc(result)
end

function format_output(result::AbstractString, docformat::JMarkdown2tex)
return uc2tex(result, true)
function format_output(result::AbstractString, docformat::JMarkdown2tex;escape_unicode=true)
# Highligts has some extra escaping defined, eg of $, ", ...
result_escaped = sprint( (io, x) -> Highlights.Format.escape(io, MIME("text/latex"), x, charescape=true), result)
escape_unicode && return uc2tex(result_escaped, true)
return result_escaped
end

function format_code(result::AbstractString, docformat)
function format_code(result::AbstractString, docformat;escape_unicode=true)
return result
end

function format_code(result::AbstractString, docformat::JMarkdown2tex)
function format_code(result::AbstractString, docformat::JMarkdown2tex;escape_unicode=true)
highlighted = highlight(MIME("text/latex"), strip(result),
Highlights.Lexers.JuliaLexer, docformat.formatdict[:theme])
return uc2tex(highlighted)
escape_unicode && return uc2tex(highlighted)
return highlighted
#return "\\begin{minted}[mathescape, fontsize=\\small, xleftmargin=0.5em]{julia}\n$result\n\\end{minted}\n"
end

Expand Down Expand Up @@ -305,12 +310,12 @@ function texify(s)
return ts
end

function format_code(result::AbstractString, docformat::JMarkdown2HTML)
function format_code(result::AbstractString, docformat::JMarkdown2HTML;escape_unicode=true)
return highlight(MIME("text/html"), strip(result),
Highlights.Lexers.JuliaLexer, docformat.formatdict[:theme])
end

function format_code(result::AbstractString, docformat::Pandoc2HTML)
function format_code(result::AbstractString, docformat::Pandoc2HTML;escape_unicode=true)
return highlight(MIME("text/html"), strip(result),
Highlights.Lexers.JuliaLexer, docformat.formatdict[:theme])
end
Expand Down
17 changes: 17 additions & 0 deletions test/formatter_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,20 @@ ldoc = Weave.run(parsed, doctype = "md2tex")
mdoc = Weave.run(parsed, doctype = "github")
@test mdoc.chunks[1].rich_output == "\n\n### Small markdown sample\n\n**Hello** from `code` block.\n\n"
@test mdoc.chunks[2].rich_output == "\n\n* one\n* two\n* three\n\n"


# Test disable escaping of unicode
content = """
# Test chunk
α
"""

dchunk = Weave.DocChunk(content, 1, 1)

pformat = Weave.formats["md2tex"]

f = Weave.format_chunk(dchunk, pformat.formatdict, pformat)
@test f == "\\section{Test chunk}\n\\ensuremath{\\alpha}\n\n"

f = Weave.format_chunk(dchunk, pformat.formatdict, pformat,escape_unicode=false)
@test f == "\\section{Test chunk}\nα\n\n"

0 comments on commit bd5ff70

Please sign in to comment.