Weave.jl/src/rendering/pandocformats.jl

85 lines
2.6 KiB
Julia
Raw Normal View History

2020-06-14 11:15:05 +02:00
abstract type PandocFormat <: WeaveFormat end
function render_figures(docformat::PandocFormat, chunk)
fignames = chunk.figures
length(fignames) > 0 || (return "")
caption = chunk.options[:fig_cap]
label = get(chunk.options, :label, nothing)
result = ""
figstring = ""
attribs = ""
width = chunk.options[:out_width]
height = chunk.options[:out_height]
# Build figure attibutes
attribs = String[]
isnothing(width) || push!(attribs, "width=$width")
isnothing(height) || push!(attribs, "height=$height")
isnothing(label) || push!(attribs, "#fig:$label")
attribs = isempty(attribs) ? "" : "{" * join(attribs, " ") * "}"
if !isnothing(caption)
result *= "![$caption]($(fignames[1]))$attribs\n"
for fig in fignames[2:end]
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"
end
end
return result
end
Base.@kwdef mutable struct Pandoc <: PandocFormat
2020-06-14 14:33:55 +02:00
description = "Pandoc Markdown"
2020-06-14 11:15:05 +02:00
extension = "md"
codestart = "~~~~{.julia}"
2020-10-02 21:21:19 +02:00
codeend = "~~~~~~~~~~~~~\n"
2020-06-14 11:15:05 +02:00
termstart = codestart
termend = codeend
outputstart = "~~~~"
outputend = "~~~~\n\n"
# Prefer png figures for markdown conversion, svg doesn't work with latex
mimetypes = ["image/png", "image/jpg", "image/svg+xml", "text/markdown", "text/plain"]
fig_ext = ".png"
out_width = nothing
out_height = nothing
fig_pos = nothing
fig_env = nothing
2020-06-14 13:57:37 +02:00
# specials
preserve_header = true
2020-06-14 11:15:05 +02:00
end
register_format!("pandoc", Pandoc())
const DEFAULT_PANDOC_OPTIONS = String[]
Base.@kwdef mutable struct Pandoc2PDF <: PandocFormat
2020-06-14 14:33:55 +02:00
description = "PDF via intermediate Pandoc Markdown"
2020-06-14 11:15:05 +02:00
extension = "md"
codestart = "~~~~{.julia}"
2020-10-02 21:21:19 +02:00
codeend = "~~~~~~~~~~~~~\n"
2020-06-14 11:15:05 +02:00
termstart = codestart
termend = codeend
outputstart = "~~~~"
outputend = "~~~~\n\n"
# Prefer png figures for markdown conversion, svg doesn't work with latex
mimetypes = ["image/png", "image/jpg", "image/svg+xml", "text/markdown", "text/plain"]
fig_ext = ".png"
out_width = nothing
out_height = nothing
fig_pos = nothing
fig_env = nothing
# specials
2020-06-14 13:57:37 +02:00
preserve_header = true
2020-06-14 11:15:05 +02:00
header_template = normpath(TEMPLATE_DIR, "pandoc2pdf_header.txt")
pandoc_options = DEFAULT_PANDOC_OPTIONS
end
register_format!("pandoc2pdf", Pandoc2PDF())
function set_format_options!(docformat::Pandoc2PDF; pandoc_options = DEFAULT_PANDOC_OPTIONS, _kwargs...)
docformat.pandoc_options = pandoc_options
end