From 630ac466e796e27955aefde7e7677aef3715caa8 Mon Sep 17 00:00:00 2001 From: Jonas Isensee Date: Wed, 3 Jun 2020 14:13:07 +0200 Subject: [PATCH 1/8] Reapply improvements to the tex rendering pipeline --- src/rendering/texformats.jl | 36 ++++++++++++++++++++++++++++-------- templates/md2pdf.tpl | 4 +++- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/rendering/texformats.jl b/src/rendering/texformats.jl index c8ffe5a5..b4204ec3 100644 --- a/src/rendering/texformats.jl +++ b/src/rendering/texformats.jl @@ -3,7 +3,10 @@ abstract type TexFormat <: WeaveFormat end -set_rendering_options!(docformat::TexFormat; keep_unicode = false, kwargs...) = docformat.keep_unicode |= keep_unicode +function set_rendering_options!(docformat::TexFormat; keep_unicode = false, template=nothing, kwargs...) + docformat.keep_unicode |= keep_unicode + docformat.template = get_tex_template(template) +end function formatfigures(chunk, docformat::TexFormat) fignames = chunk.figures @@ -93,6 +96,8 @@ Base.@kwdef mutable struct Tex <: TexFormat fig_env = "figure" # specials keep_unicode = false + template = nothing + tex_deps = "" end register_format!("tex", Tex()) @@ -116,6 +121,8 @@ Base.@kwdef mutable struct TexMinted <: TexFormat fig_env = "figure" # specials keep_unicode = false + template = nothing + tex_deps = "\\usepackage{minted}" end register_format!("texminted", TexMinted()) @@ -142,6 +149,7 @@ Base.@kwdef mutable struct JMarkdown2tex <: TexFormat highlight_theme = nothing template = nothing keep_unicode = false + tex_deps = "" end register_format!("md2tex", JMarkdown2tex()) register_format!("md2pdf", JMarkdown2tex()) @@ -155,17 +163,22 @@ end get_tex_template(::Nothing) = get_template(normpath(TEMPLATE_DIR, "md2pdf.tpl")) get_tex_template(x) = get_template(x) -function render_doc(docformat::JMarkdown2tex, body, doc) +highlight_str(docformat::TexFormat) = "" +highlight_str(docformat::JMarkdown2tex) = + get_highlight_stylesheet(MIME("text/latex"), docformat.highlight_theme) + +function render_doc(docformat::TexFormat, body, doc) return Mustache.render( docformat.template; body = body, - highlight = get_highlight_stylesheet(MIME("text/latex"), docformat.highlight_theme), + highlight = highlight_str(docformat), + tex_deps = docformat.tex_deps, [Pair(Symbol(k), v) for (k, v) in doc.header]..., ) end # very similar to export to html -function format_chunk(chunk::DocChunk, docformat::JMarkdown2tex) +function format_chunk(chunk::DocChunk, docformat::TexFormat) out = IOBuffer() io = IOBuffer() for inline in chunk.content @@ -185,7 +198,7 @@ function format_chunk(chunk::DocChunk, docformat::JMarkdown2tex) return docformat.keep_unicode ? out : uc2tex(out) end -function format_output(result, docformat::JMarkdown2tex) +function format_output(result, docformat::TexFormat) # Highligts has some extra escaping defined, eg of $, ", ... result_escaped = sprint( (io, x) -> @@ -196,8 +209,13 @@ function format_output(result, docformat::JMarkdown2tex) return result_escaped end -function format_code(code, docformat::JMarkdown2tex) - ret = highlight_code(MIME("text/latex"), code, docformat.highlight_theme) +# Highlight code is currently only compatible with lstlistings (JMarkdown2tex) +highlight_code(code, docformat::TexFormat) = code +highlight_code(code, docformat::JMarkdown2tex) = + highlight_code(MIME("text/latex"), code, docformat.highlight_theme) + +function format_code(code, docformat::TexFormat) + ret = highlight_code(code, docformat) docformat.keep_unicode || return uc2tex(ret) return ret end @@ -214,7 +232,9 @@ function uc2tex(s, escape = false) return s end -# should_render(chunk) ? highlight_term(MIME("text/latex"), , docformat.highlight_theme) : "" +format_termchunk(chunk, docformat::TexFormat) = + string(docformat.termstart, chunk.output, docformat.termend, "\n") + format_termchunk(chunk, docformat::JMarkdown2tex) = should_render(chunk) ? highlight_term(MIME("text/latex"), chunk.output, docformat.highlight_theme) : "" diff --git a/templates/md2pdf.tpl b/templates/md2pdf.tpl index 036358aa..b9e79e9c 100644 --- a/templates/md2pdf.tpl +++ b/templates/md2pdf.tpl @@ -7,7 +7,9 @@ \usepackage{graphicx} \usepackage{microtype} \usepackage{hyperref} - +{{#:tex_deps}} +{{{ :tex_deps }}} +{{/:tex_deps}} \setlength{\parindent}{0pt} \setlength{\parskip}{1.2ex} From 5c853150c872099610bb0a1d4dcc6504122e1330 Mon Sep 17 00:00:00 2001 From: Jonas Isensee Date: Sat, 13 Jun 2020 09:14:12 +0200 Subject: [PATCH 2/8] fix math escapes in code and output --- src/rendering/texformats.jl | 57 +++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 18 deletions(-) diff --git a/src/rendering/texformats.jl b/src/rendering/texformats.jl index b4204ec3..27bcb0f6 100644 --- a/src/rendering/texformats.jl +++ b/src/rendering/texformats.jl @@ -107,11 +107,11 @@ register_format!("tex", Tex()) Base.@kwdef mutable struct TexMinted <: TexFormat description = "Latex using minted for highlighting" extension = "tex" - codestart = "\\begin{minted}[mathescape, fontsize=\\small, xleftmargin=0.5em]{julia}" + codestart = "\\begin{minted}[escapeinside=||, mathescape, fontsize=\\small, xleftmargin=0.5em]{julia}" codeend = "\\end{minted}" - termstart = "\\begin{minted}[fontsize=\\footnotesize, xleftmargin=0.5em, mathescape]{jlcon}" + termstart = "\\begin{minted}[escapeinside=||, mathescape, fontsize=\\footnotesize, xleftmargin=0.5em]{jlcon}" termend = "\\end{minted}" - outputstart = "\\begin{minted}[fontsize=\\small, xleftmargin=0.5em, mathescape, frame = leftline]{text}" + outputstart = "\\begin{minted}[escapeinside=||, mathescape, fontsize=\\small, xleftmargin=0.5em, frame = leftline]{text}" outputend = "\\end{minted}" mimetypes = ["application/pdf", "image/png", "text/latex", "text/plain"] fig_ext = ".pdf" @@ -163,15 +163,21 @@ end get_tex_template(::Nothing) = get_template(normpath(TEMPLATE_DIR, "md2pdf.tpl")) get_tex_template(x) = get_template(x) -highlight_str(docformat::TexFormat) = "" -highlight_str(docformat::JMarkdown2tex) = - get_highlight_stylesheet(MIME("text/latex"), docformat.highlight_theme) - function render_doc(docformat::TexFormat, body, doc) return Mustache.render( docformat.template; body = body, - highlight = highlight_str(docformat), + highlight = "", + tex_deps = docformat.tex_deps, + [Pair(Symbol(k), v) for (k, v) in doc.header]..., + ) +end + +function render_doc(docformat::JMarkdown2tex, body, doc) + return Mustache.render( + docformat.template; + body = body, + highlight = get_highlight_stylesheet(MIME("text/latex"), docformat.highlight_theme), tex_deps = docformat.tex_deps, [Pair(Symbol(k), v) for (k, v) in doc.header]..., ) @@ -195,33 +201,37 @@ function format_chunk(chunk::DocChunk, docformat::TexFormat) end clear_buffer_and_format!(io, out, WeaveMarkdown.latex) out = take2string!(out) - return docformat.keep_unicode ? out : uc2tex(out) + return docformat.keep_unicode ? out : uc2tex(docformat, out) end function format_output(result, docformat::TexFormat) + docformat.keep_unicode || return uc2tex(docformat, result, true) + return result +end + +function format_output(result, docformat::JMarkdown2tex) # Highligts has some extra escaping defined, eg of $, ", ... result_escaped = sprint( (io, x) -> Highlights.Format.escape(io, MIME("text/latex"), x, charescape = true), result, ) - docformat.keep_unicode || return uc2tex(result_escaped, true) + docformat.keep_unicode || return uc2tex(docformat, result_escaped, true) return result_escaped end -# Highlight code is currently only compatible with lstlistings (JMarkdown2tex) -highlight_code(code, docformat::TexFormat) = code -highlight_code(code, docformat::JMarkdown2tex) = - highlight_code(MIME("text/latex"), code, docformat.highlight_theme) - function format_code(code, docformat::TexFormat) - ret = highlight_code(code, docformat) - docformat.keep_unicode || return uc2tex(ret) + docformat.keep_unicode || return uc2tex(docformat, code, true) + return ret +end +function format_code(code, docformat::JMarkdown2tex) + ret = highlight_code(MIME("text/latex"), code, docformat.highlight_theme) + docformat.keep_unicode || return uc2tex(docformat, ret, false) return ret end # Convert unicode to tex, escape listings if needed -function uc2tex(s, escape = false) +function uc2tex(::JMarkdown2tex, s, escape = false) for key in keys(latex_symbols) if escape s = replace(s, latex_symbols[key] => "(*@\\ensuremath{$(texify(key))}@*)") @@ -231,6 +241,17 @@ function uc2tex(s, escape = false) end return s end +# Convert unicode to tex, escape listings if needed +function uc2tex(::TexFormat, s, escape = false) + for key in keys(latex_symbols) + if escape + s = replace(s, latex_symbols[key] => "|\$\\ensuremath{$(texify(key))}\$|") + else + s = replace(s, latex_symbols[key] => "\\ensuremath{$(texify(key))}") + end + end + return s +end format_termchunk(chunk, docformat::TexFormat) = string(docformat.termstart, chunk.output, docformat.termend, "\n") From 1135c5ddfaf594e7c35951b7fce507e7d9066fa6 Mon Sep 17 00:00:00 2001 From: Jonas Isensee Date: Sat, 13 Jun 2020 15:50:04 +0200 Subject: [PATCH 3/8] Remove "tex" format --- src/rendering/texformats.jl | 27 +-------------------------- 1 file changed, 1 insertion(+), 26 deletions(-) diff --git a/src/rendering/texformats.jl b/src/rendering/texformats.jl index 27bcb0f6..81f75aab 100644 --- a/src/rendering/texformats.jl +++ b/src/rendering/texformats.jl @@ -76,31 +76,6 @@ function md_length_to_latex(def, reference) return def end -# plain Tex -# --------- - -Base.@kwdef mutable struct Tex <: TexFormat - description = "Latex with custom code environments" - extension = "tex" - codestart = "\\begin{juliacode}" - codeend = "\\end{juliacode}" - termstart = "\\begin{juliaterm}" - termend = "\\end{juliaterm}" - outputstart = "\\begin{juliaout}" - outputend = "\\end{juliaout}" - mimetypes = ["application/pdf", "image/png", "text/latex", "text/plain"] - fig_ext = ".pdf" - out_width = "\\linewidth" - out_height = nothing - fig_pos = "htpb" - fig_env = "figure" - # specials - keep_unicode = false - template = nothing - tex_deps = "" -end -register_format!("tex", Tex()) - # minted Tex # ---------- @@ -241,7 +216,7 @@ function uc2tex(::JMarkdown2tex, s, escape = false) end return s end -# Convert unicode to tex, escape listings if needed + function uc2tex(::TexFormat, s, escape = false) for key in keys(latex_symbols) if escape From b5697409154c53d5fda8387db220b32401318789 Mon Sep 17 00:00:00 2001 From: Shuhei Kadowaki Date: Sun, 14 Jun 2020 00:02:05 +0900 Subject: [PATCH 4/8] rm tests with "tex" format --- test/figureformatter_test.jl | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/test/figureformatter_test.jl b/test/figureformatter_test.jl index 26facec7..98d7b22c 100644 --- a/test/figureformatter_test.jl +++ b/test/figureformatter_test.jl @@ -9,7 +9,6 @@ chunk.figures = ["figs/figures_plot1.png"] @test test_formatfigures(chunk, "md2tex") == "\\includegraphics{figs/figures_plot1.png}\n" -@test test_formatfigures(chunk, "tex") == "\\includegraphics{figs/figures_plot1.png}\n" @test test_formatfigures(chunk, "texminted") == "\\includegraphics{figs/figures_plot1.png}\n" @test test_formatfigures(chunk, "pandoc") == "![](figs/figures_plot1.png)\\ \n\n" @test test_formatfigures(chunk, "github") == "![](figs/figures_plot1.png)\n" @@ -24,7 +23,6 @@ chunk.options[:out_width] = "100%" chunk.options[:fig_cap] = "Nice plot" -@test test_formatfigures(chunk, "tex") == "\\begin{figure}[!h]\n\\center\n\\includegraphics[width=1.0\\linewidth]{figs/figures_plot1.png}\n\\caption{Nice plot}\n\\end{figure}\n" @test test_formatfigures(chunk, "pandoc") == "![Nice plot](figs/figures_plot1.png){width=100%}\n" @test test_formatfigures(chunk, "md2tex") == "\\begin{figure}[!h]\n\\center\n\\includegraphics[width=1.0\\linewidth]{figs/figures_plot1.png}\n\\caption{Nice plot}\n\\end{figure}\n" @test test_formatfigures(chunk, "md2html") == "
\n\n
Nice plot
\n
\n" @@ -35,21 +33,3 @@ chunk.options[:fig_cap] = "Nice plot" chunk.options[:label] = "somefig" @test test_formatfigures(chunk, "pandoc") == "![Nice plot](figs/figures_plot1.png){width=100% #fig:somefig}\n" -@test test_formatfigures(chunk, "tex") == "\\begin{figure}[!h]\n\\center\n\\includegraphics[width=1.0\\linewidth]{figs/figures_plot1.png}\n\\caption{Nice plot}\n\\label{fig:somefig}\n\\end{figure}\n" - - -chunk.options[:label] = nothing -chunk.options[:fig_cap] = nothing -chunk.options[:fig_env] = "center" -chunk.options[:fig_pos] = "" -@test test_formatfigures(chunk, "tex") == "\\begin{center}\n\\includegraphics[width=1.0\\linewidth]{figs/figures_plot1.png}\n\\end{center}\n" - - -chunk.options[:out_width] = "50%" -chunk.options[:out_height] = "75 %" -@test test_formatfigures(chunk, "tex") == "\\begin{center}\n\\includegraphics[width=0.5\\linewidth,height=0.75\\paperheight]{figs/figures_plot1.png}\n\\end{center}\n" - - -chunk.options[:out_width] = "A%" -chunk.options[:out_height] = "0.5\\textwidth" -@test test_formatfigures(chunk, "tex") == "\\begin{center}\n\\includegraphics[width=A%,height=0.5\\textwidth]{figs/figures_plot1.png}\n\\end{center}\n" From 12e148af188c6cea7b1a4fa4864acf4e498a9030 Mon Sep 17 00:00:00 2001 From: Jonas Isensee Date: Sat, 13 Jun 2020 22:16:48 +0200 Subject: [PATCH 5/8] move unicode2tex escape characters to struct --- src/rendering/texformats.jl | 46 +++++++++++++++---------------------- 1 file changed, 19 insertions(+), 27 deletions(-) diff --git a/src/rendering/texformats.jl b/src/rendering/texformats.jl index 81f75aab..5a99ee5b 100644 --- a/src/rendering/texformats.jl +++ b/src/rendering/texformats.jl @@ -98,6 +98,9 @@ Base.@kwdef mutable struct TexMinted <: TexFormat keep_unicode = false template = nothing tex_deps = "\\usepackage{minted}" + # how to escape latex in verbatim/code environment + start_escape = "|\$" + close_escape = "\$|" end register_format!("texminted", TexMinted()) @@ -125,6 +128,9 @@ Base.@kwdef mutable struct JMarkdown2tex <: TexFormat template = nothing keep_unicode = false tex_deps = "" + # how to escape latex in verbatim/code environment + start_escape = "(*@" + close_escape = "@*)" end register_format!("md2tex", JMarkdown2tex()) register_format!("md2pdf", JMarkdown2tex()) @@ -176,12 +182,11 @@ function format_chunk(chunk::DocChunk, docformat::TexFormat) end clear_buffer_and_format!(io, out, WeaveMarkdown.latex) out = take2string!(out) - return docformat.keep_unicode ? out : uc2tex(docformat, out) + return unicode2tex(docformat, out) end function format_output(result, docformat::TexFormat) - docformat.keep_unicode || return uc2tex(docformat, result, true) - return result + return unicode2tex(docformat, result, true) end function format_output(result, docformat::JMarkdown2tex) @@ -191,39 +196,26 @@ function format_output(result, docformat::JMarkdown2tex) Highlights.Format.escape(io, MIME("text/latex"), x, charescape = true), result, ) - docformat.keep_unicode || return uc2tex(docformat, result_escaped, true) - return result_escaped + return unicode2tex(docformat, result_escaped, true) end function format_code(code, docformat::TexFormat) - docformat.keep_unicode || return uc2tex(docformat, code, true) - return ret + return unicode2tex(docformat, code, true) end function format_code(code, docformat::JMarkdown2tex) ret = highlight_code(MIME("text/latex"), code, docformat.highlight_theme) - docformat.keep_unicode || return uc2tex(docformat, ret, false) - return ret -end - -# Convert unicode to tex, escape listings if needed -function uc2tex(::JMarkdown2tex, s, escape = false) - for key in keys(latex_symbols) - if escape - s = replace(s, latex_symbols[key] => "(*@\\ensuremath{$(texify(key))}@*)") - else - s = replace(s, latex_symbols[key] => "\\ensuremath{$(texify(key))}") - end - end - return s + unicode2tex(docformat, ret, false) end -function uc2tex(::TexFormat, s, escape = false) +function unicode2tex(docformat::TexFormat, s, escape = false) + # Check whether to convert at all and return input if not4 + docformat.keep_unicode && return s + starter = docformat.escape_starter + closer = docformat.escape_closer for key in keys(latex_symbols) - if escape - s = replace(s, latex_symbols[key] => "|\$\\ensuremath{$(texify(key))}\$|") - else - s = replace(s, latex_symbols[key] => "\\ensuremath{$(texify(key))}") - end + body = "\\ensuremath{$(texify(key))}" + target = escape ? string(starter, body, closer) : body + s = replace(s, latex_symbols[key] => body) end return s end From 3dfe817584540ac3e71b93b8d76c024803e868af Mon Sep 17 00:00:00 2001 From: Jonas Isensee Date: Sat, 13 Jun 2020 22:56:23 +0200 Subject: [PATCH 6/8] fix bug --- src/rendering/texformats.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/rendering/texformats.jl b/src/rendering/texformats.jl index 5a99ee5b..90238eb6 100644 --- a/src/rendering/texformats.jl +++ b/src/rendering/texformats.jl @@ -210,12 +210,12 @@ end function unicode2tex(docformat::TexFormat, s, escape = false) # Check whether to convert at all and return input if not4 docformat.keep_unicode && return s - starter = docformat.escape_starter - closer = docformat.escape_closer + starter = docformat.start_escape + closer = docformat.close_escape for key in keys(latex_symbols) body = "\\ensuremath{$(texify(key))}" target = escape ? string(starter, body, closer) : body - s = replace(s, latex_symbols[key] => body) + s = replace(s, latex_symbols[key] => target) end return s end From 872f7c715313d134283d8ac993d70664c324f60d Mon Sep 17 00:00:00 2001 From: Shuhei Kadowaki Date: Sun, 14 Jun 2020 13:54:56 +0900 Subject: [PATCH 7/8] construct UNICODE2LATEX at precompile time --- src/Weave.jl | 2 +- src/rendering/rendering.jl | 1 - src/rendering/texformats.jl | 65 +++++++++++++++++++------------------ 3 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/Weave.jl b/src/Weave.jl index 09646fff..e3bf0d77 100644 --- a/src/Weave.jl +++ b/src/Weave.jl @@ -1,6 +1,6 @@ module Weave -using Highlights, Mustache, Requires, Pkg +using Highlights, Mustache, Requires, Pkg, REPL # directories diff --git a/src/rendering/rendering.jl b/src/rendering/rendering.jl index cab31a78..92e99ac4 100644 --- a/src/rendering/rendering.jl +++ b/src/rendering/rendering.jl @@ -5,7 +5,6 @@ # - 4. Document Interface using Mustache, Highlights, .WeaveMarkdown, Markdown, Dates, Printf -using REPL.REPLCompletions: latex_symbols const FORMATS = Dict{String,WeaveFormat}() diff --git a/src/rendering/texformats.jl b/src/rendering/texformats.jl index 90238eb6..b13fad07 100644 --- a/src/rendering/texformats.jl +++ b/src/rendering/texformats.jl @@ -99,8 +99,8 @@ Base.@kwdef mutable struct TexMinted <: TexFormat template = nothing tex_deps = "\\usepackage{minted}" # how to escape latex in verbatim/code environment - start_escape = "|\$" - close_escape = "\$|" + escape_starter = "|\$" + escape_closer = "\$|" end register_format!("texminted", TexMinted()) @@ -129,8 +129,8 @@ Base.@kwdef mutable struct JMarkdown2tex <: TexFormat keep_unicode = false tex_deps = "" # how to escape latex in verbatim/code environment - start_escape = "(*@" - close_escape = "@*)" + escape_starter = "(*@" + escape_closer = "@*)" end register_format!("md2tex", JMarkdown2tex()) register_format!("md2pdf", JMarkdown2tex()) @@ -182,11 +182,11 @@ function format_chunk(chunk::DocChunk, docformat::TexFormat) end clear_buffer_and_format!(io, out, WeaveMarkdown.latex) out = take2string!(out) - return unicode2tex(docformat, out) + return unicode2latex(docformat, out) end function format_output(result, docformat::TexFormat) - return unicode2tex(docformat, result, true) + return unicode2latex(docformat, result, true) end function format_output(result, docformat::JMarkdown2tex) @@ -196,26 +196,42 @@ function format_output(result, docformat::JMarkdown2tex) Highlights.Format.escape(io, MIME("text/latex"), x, charescape = true), result, ) - return unicode2tex(docformat, result_escaped, true) + return unicode2latex(docformat, result_escaped, true) end function format_code(code, docformat::TexFormat) - return unicode2tex(docformat, code, true) + return unicode2latex(docformat, code, true) end function format_code(code, docformat::JMarkdown2tex) ret = highlight_code(MIME("text/latex"), code, docformat.highlight_theme) - unicode2tex(docformat, ret, false) + unicode2latex(docformat, ret, false) end -function unicode2tex(docformat::TexFormat, s, escape = false) - # Check whether to convert at all and return input if not4 +# from julia symbols (e.g. "\bfhoge") to valid latex +const UNICODE2LATEX = let + function texify(s) + return if occursin(r"^\\bf[A-Z]$", s) + replace(s, "\\bf" => "\\bm{\\mathrm{") * "}}" + elseif startswith(s, "\\bfrak") + replace(s, "\\bfrak" => "\\bm{\\mathfrak{") * "}}" + elseif startswith(s, "\\bf") + replace(s, "\\bf" => "\\bm{\\") * "}" + elseif startswith(s, "\\frak") + replace(s, "\\frak" => "\\mathfrak{") * "}" + else + s + end + end + return Dict(unicode => texify(sym) for (sym, unicode) in REPL.REPLCompletions.latex_symbols) +end + +function unicode2latex(docformat::TexFormat, s, escape = false) + # Check whether to convert at all and return input if not docformat.keep_unicode && return s - starter = docformat.start_escape - closer = docformat.close_escape - for key in keys(latex_symbols) - body = "\\ensuremath{$(texify(key))}" - target = escape ? string(starter, body, closer) : body - s = replace(s, latex_symbols[key] => target) + for (unicode, latex) in UNICODE2LATEX + body = "\\ensuremath{$(latex)}" + target = escape ? string(docformat.escape_starter, body, docformat.escape_closer) : body + s = replace(s, unicode => target) end return s end @@ -225,18 +241,3 @@ format_termchunk(chunk, docformat::TexFormat) = format_termchunk(chunk, docformat::JMarkdown2tex) = should_render(chunk) ? highlight_term(MIME("text/latex"), chunk.output, docformat.highlight_theme) : "" - -# Make julia symbols (\bf* etc.) valid latex -function texify(s) - return if occursin(r"^\\bf[A-Z]$", s) - replace(s, "\\bf" => "\\bm{\\mathrm{") * "}}" - elseif startswith(s, "\\bfrak") - replace(s, "\\bfrak" => "\\bm{\\mathfrak{") * "}}" - elseif startswith(s, "\\bf") - replace(s, "\\bf" => "\\bm{\\") * "}" - elseif startswith(s, "\\frak") - replace(s, "\\frak" => "\\mathfrak{") * "}" - else - s - end -end From 092adb680507daa27489289fa5679b293fcddd72 Mon Sep 17 00:00:00 2001 From: Shuhei Kadowaki Date: Sun, 14 Jun 2020 14:25:03 +0900 Subject: [PATCH 8/8] update test --- src/Weave.jl | 2 ++ src/rendering/texformats.jl | 2 +- src/run.jl | 2 +- test/figureformatter_test.jl | 2 +- test/formatter_test.jl | 36 ------------------------------------ test/render/texformats.jl | 30 ++++++++++++++++++++++++++++++ test/runtests.jl | 7 +++++-- 7 files changed, 40 insertions(+), 41 deletions(-) delete mode 100644 test/formatter_test.jl create mode 100644 test/render/texformats.jl diff --git a/src/Weave.jl b/src/Weave.jl index e3bf0d77..c68e0fea 100644 --- a/src/Weave.jl +++ b/src/Weave.jl @@ -32,6 +32,8 @@ end take2string!(io) = String(take!(io)) joinlines(lines) = join(lines, '\n') +get_format(doctype::AbstractString) = FORMATS[doctype] + """ list_out_formats() diff --git a/src/rendering/texformats.jl b/src/rendering/texformats.jl index b13fad07..466dd61b 100644 --- a/src/rendering/texformats.jl +++ b/src/rendering/texformats.jl @@ -222,7 +222,7 @@ const UNICODE2LATEX = let s end end - return Dict(unicode => texify(sym) for (sym, unicode) in REPL.REPLCompletions.latex_symbols) + Dict(unicode => texify(sym) for (sym, unicode) in REPL.REPLCompletions.latex_symbols) end function unicode2latex(docformat::TexFormat, s, escape = false) diff --git a/src/run.jl b/src/run.jl index 3f3013c6..71442ede 100644 --- a/src/run.jl +++ b/src/run.jl @@ -17,7 +17,7 @@ function run_doc( # cache :all, :user, :off, :refresh doc.doctype = isnothing(doctype) ? (doctype = detect_doctype(doc.source)) : doctype - doc.format = deepcopy(FORMATS[doctype]) + doc.format = deepcopy(get_format(doctype)) cwd = doc.cwd = get_cwd(doc, out_path) isdir(cwd) || mkdir(cwd) diff --git a/test/figureformatter_test.jl b/test/figureformatter_test.jl index 98d7b22c..6fb2d25b 100644 --- a/test/figureformatter_test.jl +++ b/test/figureformatter_test.jl @@ -1,4 +1,4 @@ -test_formatfigures(chunk, format) = Weave.formatfigures(chunk, Weave.FORMATS[format]) +test_formatfigures(chunk, format) = Weave.formatfigures(chunk, get_format(format)) # Make a dummy codehunk with figure diff --git a/test/formatter_test.jl b/test/formatter_test.jl deleted file mode 100644 index 52fb9d5c..00000000 --- a/test/formatter_test.jl +++ /dev/null @@ -1,36 +0,0 @@ -# Test disable escaping of unicode -@testset "escape/unescape unicode characters" begin - -content = """ -# Test chunk -α -""" -chunk = Weave.DocChunk(content, 1, 1) -fmt = deepcopy(Weave.FORMATS["md2tex"]) - -f = Weave.format_chunk(chunk, fmt) -@test f == "\\section{Test chunk}\n\\ensuremath{\\alpha}\n\n" - -fmt.keep_unicode = true -f = Weave.format_chunk(chunk, fmt) -@test f == "\\section{Test chunk}\nα\n\n" - - -str = """ -```julia -α = 10 -``` -""" -doc = mock_run(str; doctype = "md2tex") -Weave.set_rendering_options!(doc.format) -doc = Weave.render_doc(doc) -@test occursin(Weave.uc2tex("α"), doc) -@test !occursin("α", doc) - -doc = mock_run(str; doctype = "md2tex") -Weave.set_rendering_options!(doc.format; keep_unicode = true) -doc = Weave.render_doc(doc) -@test occursin("α", doc) -@test !occursin(Weave.uc2tex("α"), doc) - -end diff --git a/test/render/texformats.jl b/test/render/texformats.jl new file mode 100644 index 00000000..17dbdf06 --- /dev/null +++ b/test/render/texformats.jl @@ -0,0 +1,30 @@ +@testset "unicode to latex conversion" begin + unicode2latex(args...) = Weave.unicode2latex(get_format("md2tex"), args...) + + # unit test + let + s = unicode2latex("α = 10") + @test !occursin("α", s) + @test occursin("alpha", s) + end + + # end2end + let + str = """ + ```julia + α = 10 + ``` + """ + doc = mock_run(str; doctype = "md2tex") + Weave.set_rendering_options!(doc.format) + rendered = Weave.render_doc(doc) + @test occursin("alpha", rendered) + @test !occursin("α", rendered) + + doc = mock_run(str; doctype = "md2tex") + Weave.set_rendering_options!(doc.format; keep_unicode = true) + rendered = Weave.render_doc(doc) + @test !occursin("alpha", rendered) + @test occursin("α", rendered) + end +end # @testset "rendering tex formats" diff --git a/test/runtests.jl b/test/runtests.jl index 8831c72d..96e7eae3 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -7,7 +7,7 @@ # %% using Weave, Test -using Weave: WeaveDoc, run_doc +using Weave: WeaveDoc, run_doc, get_format function mock_doc(str, informat = "markdown") @@ -49,6 +49,10 @@ end include("run/test_error.jl") end + @testset "render" begin + include("render/texformats.jl") + end + @testset "conversions" begin include("test_converter.jl") end @@ -58,7 +62,6 @@ end end @testset "legacy" begin - include("formatter_test.jl") include("markdown_test.jl") include("figureformatter_test.jl") include("cache_test.jl")