From b4ebf0cd163262f263e900174f7ead56f9dcd8cd Mon Sep 17 00:00:00 2001 From: Dave Kleinschmidt Date: Thu, 1 Feb 2018 13:13:11 -0500 Subject: [PATCH 1/3] generate pandoc-crossref style labels for figures also clean up attrib code --- src/formatters.jl | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/formatters.jl b/src/formatters.jl index c99596a..197d2e3 100644 --- a/src/formatters.jl +++ b/src/formatters.jl @@ -369,7 +369,10 @@ end function formatfigures(chunk, docformat::Pandoc) fignames = chunk.figures + length(fignames) > 0 || (return "") + caption = chunk.options[:fig_cap] + label = get(chunk.options, :label, nothing) result = "" figstring = "" attribs = "" @@ -377,21 +380,21 @@ function formatfigures(chunk, docformat::Pandoc) height = chunk.options[:out_height] #Build figure attibutes - width == nothing || (attribs = "width=$width") - (attribs ≠ "" && height ≠ nothing ) && (attribs *= " ") - height == nothing || (attribs *= "height=$height") - attribs == "" || (attribs = "{$attribs}") - length(fignames) > 0 || (return "") + attribs = String[] + width == nothing || push!(attribs, "width=$width") + height == nothing || push!(attribs, "height=$height") + label == nothing || push!(attribs, "#fig:$label") + attribs = join(attribs, " ") if caption != nothing - result *= "![$caption]($(fignames[1]))$attribs\n" + result *= "![$caption]($(fignames[1])){$attribs}\n" for fig = fignames[2:end] - result *= "![]($fig)$attribs\n" + result *= "![]($fig){$attribs}\n" println("Warning, only the first figure gets a caption\n") end else for fig in fignames - result *= "![]($fig)$attribs\\ \n\n" + result *= "![]($fig){$attribs}\\ \n\n" end end return result From 6ff3bee3e8290544d4b5dcfc952db5c1f36c9284 Mon Sep 17 00:00:00 2001 From: Dave Kleinschmidt Date: Mon, 21 May 2018 15:20:15 -0400 Subject: [PATCH 2/3] handle empty attribs --- src/formatters.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/formatters.jl b/src/formatters.jl index 197d2e3..0e99e82 100644 --- a/src/formatters.jl +++ b/src/formatters.jl @@ -384,17 +384,17 @@ function formatfigures(chunk, docformat::Pandoc) width == nothing || push!(attribs, "width=$width") height == nothing || push!(attribs, "height=$height") label == nothing || push!(attribs, "#fig:$label") - attribs = join(attribs, " ") + attribs = isempty(attribs) ? "" : "{" * join(attribs, " ") * "}" if caption != nothing - result *= "![$caption]($(fignames[1])){$attribs}\n" + result *= "![$caption]($(fignames[1]))$attribs\n" for fig = fignames[2:end] - result *= "![]($fig){$attribs}\n" + result *= "![]($fig)$attribs\n" println("Warning, only the first figure gets a caption\n") end else for fig in fignames - result *= "![]($fig){$attribs}\\ \n\n" + result *= "![]($fig)$attribs\\ \n\n" end end return result From 28428e08324a7949204e4f7eec7d2f7a1230888e Mon Sep 17 00:00:00 2001 From: Matti Pastell Date: Fri, 27 Jul 2018 10:22:30 +0300 Subject: [PATCH 3/3] Add tests for pandoc labels, closes #127 --- test/documents/plotsjl/plotsjl_test_gr.md.ref | 2 +- test/figureformatter_test.jl | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/test/documents/plotsjl/plotsjl_test_gr.md.ref b/test/documents/plotsjl/plotsjl_test_gr.md.ref index b819b4b..0be12b3 100644 --- a/test/documents/plotsjl/plotsjl_test_gr.md.ref +++ b/test/documents/plotsjl/plotsjl_test_gr.md.ref @@ -38,4 +38,4 @@ scatter!(rand(100),markersize=6,c=:orange) ![](figures/plotsjl_test_gr_3_1.png)\ -![A random walk.](figures/plotsjl_test_gr_random_1.png) +![A random walk.](figures/plotsjl_test_gr_random_1.png){#fig:random} diff --git a/test/figureformatter_test.jl b/test/figureformatter_test.jl index 8ffb34b..6dc0dda 100644 --- a/test/figureformatter_test.jl +++ b/test/figureformatter_test.jl @@ -29,3 +29,5 @@ chunk.options[:out_width] = "100%" @test Weave.formatfigures(chunk, Weave.multimarkdown) == "![Nice plot][figs/figures_plot1.png]\n\n[figs/figures_plot1.png]: figs/figures_plot1.png width=100%\n" @test Weave.formatfigures(chunk, Weave.adoc) == "image::figs/figures_plot1.png[width=100%,title=\"Nice plot\"]" +chunk.options[:label] = "somefig" +@test Weave.formatfigures(chunk, Weave.pandoc) == "![Nice plot](figs/figures_plot1.png){width=100% #fig:somefig}\n"