diff --git a/src/rendering/common.jl b/src/rendering/common.jl index 1e9145e..8f39b03 100644 --- a/src/rendering/common.jl +++ b/src/rendering/common.jl @@ -1,5 +1,3 @@ -# TODO: fix terminologies: `format_foo` -> `render_foo` - # fallback methods # ---------------- @@ -21,17 +19,17 @@ function restore_header!(doc) pushfirst!(doc.chunks, DocChunk(header_text, 0, 0)) end -format_chunk(chunk::DocChunk, docformat) = join((format_inline(c) for c in chunk.content)) +render_chunk(docformat::WeaveFormat, chunk::DocChunk) = join((render_inline(c) for c in chunk.content)) -format_inline(inline::InlineText) = inline.content +render_inline(inline::InlineText) = inline.content -function format_inline(inline::InlineCode) +function render_inline(inline::InlineCode) isempty(inline.rich_output) || return inline.rich_output isempty(inline.figures) || return inline.figures[end] return inline.output end -function format_chunk(chunk::CodeChunk, docformat) +function render_chunk(docformat::WeaveFormat, chunk::CodeChunk) # Fill undefined options with format specific defaults isnothing(chunk.options[:out_width]) && (chunk.options[:out_width] = docformat.out_width) @@ -44,7 +42,7 @@ function format_chunk(chunk::CodeChunk, docformat) hasproperty(docformat, :indent) && (chunk.content = indent(chunk.content, docformat.indent)) - chunk.content = format_code(chunk.content, docformat) + chunk.content = render_code(docformat, chunk.content) if !chunk.options[:eval] return if chunk.options[:echo] @@ -55,7 +53,7 @@ function format_chunk(chunk::CodeChunk, docformat) end if chunk.options[:term] - result = format_termchunk(chunk, docformat) + result = render_termchunk(docformat, chunk) else result = if chunk.options[:echo] # Convert to output format and highlight (html, tex...) if needed @@ -73,10 +71,10 @@ function format_chunk(chunk::CodeChunk, docformat) if chunk.options[:wrap] chunk.output = '\n' * wraplines(chunk.output, chunk.options[:line_width]) - chunk.output = format_output(chunk.output, docformat) + chunk.output = render_output(docformat, chunk.output) else chunk.output = '\n' * rstrip(chunk.output) - chunk.output = format_output(chunk.output, docformat) + chunk.output = render_output(docformat, chunk.output) end hasproperty(docformat, :indent) && (chunk.output = indent(chunk.output, docformat.indent)) @@ -91,13 +89,13 @@ function format_chunk(chunk::CodeChunk, docformat) # Handle figures if chunk.options[:fig] && length(chunk.figures) > 0 - result *= formatfigures(chunk, docformat) + result *= render_figures(docformat, chunk) end return result end -format_code(code, docformat) = code +render_code(docformat::WeaveFormat, code) = code indent(text, nindent) = join(map(x -> string(repeat(' ', nindent), x), split(text, '\n')), '\n') @@ -124,9 +122,9 @@ function wrapline(text, line_width = 75) result *= text end -format_output(result, docformat) = result +render_output(docformat::WeaveFormat, output) = output -function format_termchunk(chunk, docformat) +function render_termchunk(docformat::WeaveFormat, chunk) return if should_render(chunk) string(docformat.termstart, chunk.output, '\n', docformat.termend, '\n') else diff --git a/src/rendering/htmlformats.jl b/src/rendering/htmlformats.jl index b28a74c..e3cbad2 100644 --- a/src/rendering/htmlformats.jl +++ b/src/rendering/htmlformats.jl @@ -3,10 +3,10 @@ abstract type HTMLFormat <: WeaveFormat end -format_code(code, docformat::HTMLFormat) = +render_code(docformat::HTMLFormat, code) = highlight_code(MIME("text/html"), code, docformat.highlight_theme) -format_termchunk(chunk, docformat::HTMLFormat) = +render_termchunk(docformat::HTMLFormat, chunk) = should_render(chunk) ? highlight_term(MIME("text/html"), chunk.output, docformat.highlight_theme) : "" # Julia markdown @@ -44,7 +44,7 @@ function set_format_options!(docformat::JMarkdown2HTML; template = nothing, css end # very similar to tex version of function -function format_chunk(chunk::DocChunk, docformat::JMarkdown2HTML) +function render_chunk(docformat::JMarkdown2HTML, chunk::DocChunk) out = IOBuffer() io = IOBuffer() for inline in chunk.content @@ -63,9 +63,9 @@ function format_chunk(chunk::DocChunk, docformat::JMarkdown2HTML) return take2string!(out) end -format_output(result, docformat::JMarkdown2HTML) = Markdown.htmlesc(result) +render_output(docformat::JMarkdown2HTML, output) = Markdown.htmlesc(output) -function formatfigures(chunk, docformat::JMarkdown2HTML) +function render_figures(docformat::JMarkdown2HTML, chunk) fignames = chunk.figures caption = chunk.options[:fig_cap] width = chunk.options[:out_width] @@ -156,4 +156,4 @@ function set_format_options!(docformat::Pandoc2HTML; template = nothing, css = n docformat.pandoc_options = pandoc_options end -formatfigures(chunk, docformat::Pandoc2HTML) = formatfigures(chunk, Pandoc()) +render_figures(docformat::Pandoc2HTML, chunk) = render_figures(chunk, Pandoc()) diff --git a/src/rendering/markdownformats.jl b/src/rendering/markdownformats.jl index abb4e40..eeff5e4 100644 --- a/src/rendering/markdownformats.jl +++ b/src/rendering/markdownformats.jl @@ -24,7 +24,7 @@ Base.@kwdef mutable struct GitHubMarkdown <: MarkdownFormat end register_format!("github", GitHubMarkdown()) -function formatfigures(chunk, docformat::GitHubMarkdown) +function render_figures(docformat::GitHubMarkdown, chunk) fignames = chunk.figures caption = chunk.options[:fig_cap] result = "" @@ -70,7 +70,7 @@ Base.@kwdef mutable struct Hugo <: MarkdownFormat end register_format!("hugo", Hugo()) -function formatfigures(chunk, docformat::Hugo) +function render_figures(docformat::Hugo, chunk) relpath = docformat.uglyURLs ? "" : ".." mapreduce(*, enumerate(chunk.figures), init = "") do (index, fig) if index > 1 @@ -107,7 +107,7 @@ Base.@kwdef mutable struct MultiMarkdown <: MarkdownFormat end register_format!("multimarkdown", MultiMarkdown()) -function formatfigures(chunk, docformat::MultiMarkdown) +function render_figures(docformat::MultiMarkdown, chunk) fignames = chunk.figures caption = chunk.options[:fig_cap] result = "" @@ -143,7 +143,7 @@ end abstract type PandocFormat <: MarkdownFormat end -function formatfigures(chunk, docformat::PandocFormat) +function render_figures(docformat::PandocFormat, chunk) fignames = chunk.figures length(fignames) > 0 || (return "") diff --git a/src/rendering/rendering.jl b/src/rendering/rendering.jl index d30dabf..f457338 100644 --- a/src/rendering/rendering.jl +++ b/src/rendering/rendering.jl @@ -19,10 +19,7 @@ function render_doc(doc::WeaveDoc) restore_header!(doc) docformat = doc.format - lines = map(copy(doc.chunks)) do chunk - format_chunk(chunk, docformat) - end - body = join(lines, '\n') + body = joinlines(render_chunk.(Ref(docformat), copy(doc.chunks))) return render_doc(docformat, body, doc) end diff --git a/src/rendering/texformats.jl b/src/rendering/texformats.jl index 3890295..77463b3 100644 --- a/src/rendering/texformats.jl +++ b/src/rendering/texformats.jl @@ -10,7 +10,7 @@ function set_format_options!(docformat::TexFormat; keep_unicode = false, templat end # very similar to export to html -function format_chunk(chunk::DocChunk, docformat::TexFormat) +function render_chunk(docformat::TexFormat, chunk::DocChunk) out = IOBuffer() io = IOBuffer() for inline in chunk.content @@ -30,11 +30,11 @@ function format_chunk(chunk::DocChunk, docformat::TexFormat) return unicode2latex(docformat, out) end -format_output(result, docformat::TexFormat) = unicode2latex(docformat, result, true) +render_output(docformat::TexFormat, output) = unicode2latex(docformat, output, true) -format_code(code, docformat::TexFormat) = unicode2latex(docformat, code, true) +render_code(docformat::TexFormat, code) = unicode2latex(docformat, code, true) -format_termchunk(chunk, docformat::TexFormat) = string(docformat.termstart, chunk.output, docformat.termend, "\n") +render_termchunk(docformat::TexFormat, chunk) = string(docformat.termstart, chunk.output, docformat.termend, "\n") # from julia symbols (e.g. "\bfhoge") to valid latex const UNICODE2LATEX = let @@ -65,7 +65,7 @@ function unicode2latex(docformat::TexFormat, s, escape = false) return s end -function formatfigures(chunk, docformat::TexFormat) +function render_figures(docformat::TexFormat, chunk) fignames = chunk.figures caption = chunk.options[:fig_cap] width = chunk.options[:out_width] @@ -183,22 +183,22 @@ function set_format_options!(docformat::JMarkdownTexFormat; template = nothing, docformat.keep_unicode |= keep_unicode end -function format_output(result, docformat::JMarkdownTexFormat) +function render_output(docformat::JMarkdownTexFormat, output) # Highligts has some extra escaping defined, eg of $, ", ... - result_escaped = sprint( + output_escaped = sprint( (io, x) -> Highlights.Format.escape(io, MIME("text/latex"), x, charescape = true), - result, + output, ) - return unicode2latex(docformat, result_escaped, true) + return unicode2latex(docformat, output_escaped, true) end -function format_code(code, docformat::JMarkdownTexFormat) +function render_code(docformat::JMarkdownTexFormat, code) ret = highlight_code(MIME("text/latex"), code, docformat.highlight_theme) unicode2latex(docformat, ret, false) end -format_termchunk(chunk, docformat::JMarkdownTexFormat) = +render_termchunk(docformat::JMarkdownTexFormat, chunk) = should_render(chunk) ? highlight_term(MIME("text/latex"), chunk.output, docformat.highlight_theme) : "" function render_doc(docformat::JMarkdownTexFormat, body, doc) diff --git a/src/rendering/variousformats.jl b/src/rendering/variousformats.jl index 9755839..c0a0d4a 100644 --- a/src/rendering/variousformats.jl +++ b/src/rendering/variousformats.jl @@ -21,7 +21,7 @@ Base.@kwdef mutable struct Rest <: WeaveFormat end register_format!("rst", Rest()) -function formatfigures(chunk, docformat::Rest) +function render_figures(docformat::Rest, chunk) fignames = chunk.figures caption = chunk.options[:fig_cap] width = chunk.options[:out_width] @@ -66,7 +66,7 @@ Base.@kwdef mutable struct AsciiDoc <: WeaveFormat end register_format!("asciidoc", AsciiDoc()) -function formatfigures(chunk, docformat::AsciiDoc) +function render_figures(docformat::AsciiDoc, chunk) fignames = chunk.figures caption = chunk.options[:fig_cap] width = chunk.options[:out_width] diff --git a/test/figureformatter_test.jl b/test/figureformatter_test.jl deleted file mode 100644 index 6fb2d25..0000000 --- a/test/figureformatter_test.jl +++ /dev/null @@ -1,35 +0,0 @@ -test_formatfigures(chunk, format) = Weave.formatfigures(chunk, get_format(format)) - - -# Make a dummy codehunk with figure -chunk = Weave.CodeChunk("plot(x)", 1, 1, "", Dict()) -options = merge(Weave.get_chunk_defaults(), chunk.options) -merge!(chunk.options, options) -chunk.figures = ["figs/figures_plot1.png"] - - -@test test_formatfigures(chunk, "md2tex") == "\\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" -@test test_formatfigures(chunk, "hugo") == "{{< figure src=\"../figs/figures_plot1.png\" >}}" -@test test_formatfigures(chunk, "multimarkdown") == "![][figs/figures_plot1.png]\n\n[figs/figures_plot1.png]: figs/figures_plot1.png \n" -@test test_formatfigures(chunk, "md2html") == "\n" - - -chunk.options[:out_width] = "100%" -@test test_formatfigures(chunk, "asciidoc") == "image::figs/figures_plot1.png[width=100%]\n" -@test test_formatfigures(chunk, "rst") == ".. image:: figs/figures_plot1.png\n :width: 100%\n\n" - - -chunk.options[:fig_cap] = "Nice plot" -@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" -@test test_formatfigures(chunk, "rst") == ".. figure:: figs/figures_plot1.png\n :width: 100%\n\n Nice plot\n\n" -@test test_formatfigures(chunk, "multimarkdown") == "![Nice plot][figs/figures_plot1.png]\n\n[figs/figures_plot1.png]: figs/figures_plot1.png width=100%\n" -@test test_formatfigures(chunk, "asciidoc") == "image::figs/figures_plot1.png[width=100%,title=\"Nice plot\"]" - - -chunk.options[:label] = "somefig" -@test test_formatfigures(chunk, "pandoc") == "![Nice plot](figs/figures_plot1.png){width=100% #fig:somefig}\n" diff --git a/test/render_figures_test.jl b/test/render_figures_test.jl new file mode 100644 index 0000000..f54c38c --- /dev/null +++ b/test/render_figures_test.jl @@ -0,0 +1,35 @@ +test_render_figures(format, chunk) = Weave.render_figures(get_format(format), chunk) + + +# Make a dummy codehunk with figure +chunk = Weave.CodeChunk("plot(x)", 1, 1, "", Dict()) +options = merge(Weave.get_chunk_defaults(), chunk.options) +merge!(chunk.options, options) +chunk.figures = ["figs/figures_plot1.png"] + + +@test test_render_figures("md2tex", chunk) == "\\includegraphics{figs/figures_plot1.png}\n" +@test test_render_figures("texminted", chunk) == "\\includegraphics{figs/figures_plot1.png}\n" +@test test_render_figures("pandoc", chunk) == "![](figs/figures_plot1.png)\\ \n\n" +@test test_render_figures("github", chunk) == "![](figs/figures_plot1.png)\n" +@test test_render_figures("hugo", chunk) == "{{< figure src=\"../figs/figures_plot1.png\" >}}" +@test test_render_figures("multimarkdown", chunk) == "![][figs/figures_plot1.png]\n\n[figs/figures_plot1.png]: figs/figures_plot1.png \n" +@test test_render_figures("md2html", chunk) == "\n" + + +chunk.options[:out_width] = "100%" +@test test_render_figures("asciidoc", chunk) == "image::figs/figures_plot1.png[width=100%]\n" +@test test_render_figures("rst", chunk) == ".. image:: figs/figures_plot1.png\n :width: 100%\n\n" + + +chunk.options[:fig_cap] = "Nice plot" +@test test_render_figures("pandoc", chunk) == "![Nice plot](figs/figures_plot1.png){width=100%}\n" +@test test_render_figures("md2tex", chunk) == "\\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_render_figures("md2html", chunk) == "
\n\n
Nice plot
\n
\n" +@test test_render_figures("rst", chunk) == ".. figure:: figs/figures_plot1.png\n :width: 100%\n\n Nice plot\n\n" +@test test_render_figures("multimarkdown", chunk) == "![Nice plot][figs/figures_plot1.png]\n\n[figs/figures_plot1.png]: figs/figures_plot1.png width=100%\n" +@test test_render_figures("asciidoc", chunk) == "image::figs/figures_plot1.png[width=100%,title=\"Nice plot\"]" + + +chunk.options[:label] = "somefig" +@test test_render_figures("pandoc", chunk) == "![Nice plot](figs/figures_plot1.png){width=100% #fig:somefig}\n" diff --git a/test/runtests.jl b/test/runtests.jl index 96e7eae..ab07f96 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -63,7 +63,7 @@ end @testset "legacy" begin include("markdown_test.jl") - include("figureformatter_test.jl") + include("render_figures_test.jl") include("cache_test.jl") end end