Weave.jl/src/rendering/miscformats.jl

225 lines
5.7 KiB
Julia
Raw Normal View History

# GitHub markdown
# ---------------
2020-05-30 17:49:20 +02:00
2020-06-14 11:15:05 +02:00
Base.@kwdef mutable struct GitHubMarkdown <: WeaveFormat
2020-06-14 14:33:55 +02:00
description = "GitHub Markdown"
2020-06-02 08:31:18 +02:00
extension = "md"
2020-09-27 18:10:21 +02:00
codestart = "```julia"
2020-10-02 21:21:19 +02:00
codeend = "```\n"
2020-06-02 08:31:18 +02:00
termstart = codestart
termend = codeend
2020-09-27 18:10:21 +02:00
outputstart = "```"
outputend = "```\n\n"
2020-05-31 09:49:20 +02:00
fig_ext = ".png"
mimetypes = ["image/png", "image/svg+xml", "image/jpg",
"text/markdown", "text/plain"]
out_width = nothing
out_height = nothing
fig_pos = nothing
fig_env = nothing
2020-06-02 08:31:18 +02:00
# specials
preserve_header = true
end
register_format!("github", GitHubMarkdown())
2020-05-30 17:49:20 +02:00
2020-06-14 10:06:52 +02:00
function render_figures(docformat::GitHubMarkdown, chunk)
fignames = chunk.figures
caption = chunk.options[:fig_cap]
result = ""
figstring = ""
length(fignames) > 0 || (return "")
2020-06-02 12:00:40 +02:00
if !isnothing(caption)
result *= "![$caption]($(fignames[1]))\n"
for fig in fignames[2:end]
result *= "![]($fig)\n"
println("Warning, only the first figure gets a caption\n")
end
else
for fig in fignames
result *= "![]($fig)\n"
end
end
return result
end
# Hugo markdown
# -------------
2020-06-14 11:15:05 +02:00
Base.@kwdef mutable struct Hugo <: WeaveFormat
2020-06-14 14:33:55 +02:00
description = "Hugo Markdown (using shortcodes)"
2020-06-02 08:31:18 +02:00
extension = "md"
2020-09-27 18:10:21 +02:00
codestart = "```julia"
2020-10-02 21:21:19 +02:00
codeend = "```\n"
2020-06-02 08:31:18 +02:00
termstart = codestart
termend = codeend
2020-09-27 18:10:21 +02:00
outputstart = "```"
outputend = "```\n\n"
2020-05-31 13:26:27 +02:00
mimetypes = default_mime_types
2020-06-02 08:31:18 +02:00
fig_ext = ".png"
out_width = nothing
out_height = nothing
fig_pos = nothing
fig_env = nothing
2020-06-02 08:31:18 +02:00
# specials
preserve_header = true
uglyURLs = false # if `false`, prepend figure path by `..`
end
register_format!("hugo", Hugo())
2020-05-30 17:49:20 +02:00
2020-06-14 10:06:52 +02:00
function render_figures(docformat::Hugo, chunk)
relpath = docformat.uglyURLs ? "" : ".."
mapreduce(*, enumerate(chunk.figures), init = "") do (index, fig)
if index > 1
@warn("Only the first figure gets a caption.")
title_spec = ""
else
caption = chunk.options[:fig_cap]
2020-06-02 12:00:40 +02:00
title_spec = isnothing(caption) ? "" : "title=\"$(caption)\" "
end
"{{< figure src=\"$(joinpath(relpath, fig))\" $(title_spec) >}}"
end
end
# multi language markdown
# -----------------------
2020-06-14 11:15:05 +02:00
Base.@kwdef mutable struct MultiMarkdown <: WeaveFormat
description = "MultiMarkdown"
2020-06-02 08:31:18 +02:00
extension = "md"
2020-09-27 18:10:21 +02:00
codestart = "```julia"
2020-10-02 21:21:19 +02:00
codeend = "```\n"
2020-06-02 08:31:18 +02:00
termstart = codestart
termend = codeend
2020-09-27 18:10:21 +02:00
outputstart = "```"
outputend = "```\n\n"
2020-06-02 08:31:18 +02:00
mimetypes = default_mime_types
fig_ext = ".png"
out_width = nothing
out_height = nothing
fig_pos = nothing
fig_env = nothing
2020-06-02 08:31:18 +02:00
# specials
preserve_header = true
end
register_format!("multimarkdown", MultiMarkdown())
2020-05-30 17:49:20 +02:00
2020-06-14 10:06:52 +02:00
function render_figures(docformat::MultiMarkdown, chunk)
fignames = chunk.figures
caption = chunk.options[:fig_cap]
result = ""
figstring = ""
if chunk.options[:out_width] == nothing
width = ""
else
width = "width=$(chunk.options[:out_width])"
end
length(fignames) > 0 || (return "")
2020-06-02 12:00:40 +02:00
if !isnothing(caption)
result *= "![$caption][$(fignames[1])]\n\n"
result *= "[$(fignames[1])]: $(fignames[1]) $width\n"
for fig in fignames[2:end]
result *= "![][$fig]\n\n"
result *= "[$fig]: $fig $width\n"
println("Warning, only the first figure gets a caption\n")
end
else
for fig in fignames
result *= "![][$fig]\n\n"
result *= "[$fig]: $fig $width\n"
end
end
return result
end
2020-05-30 17:49:20 +02:00
2020-06-14 11:15:05 +02:00
# Rest
# ----
2020-05-30 17:49:20 +02:00
2020-06-14 11:15:05 +02:00
Base.@kwdef mutable struct Rest <: WeaveFormat
description = "reStructuredText and Sphinx"
extension = "rst"
codestart = ".. code-block:: julia\n"
2020-10-02 21:21:19 +02:00
codeend = "\n"
2020-06-14 11:15:05 +02:00
termstart = codestart
termend = codeend
outputstart = "::\n"
outputend = "\n\n"
mimetypes = default_mime_types
fig_ext = ".png"
out_width = "15 cm"
out_height = nothing
fig_pos = nothing
fig_env = nothing
# specials
indent = 4
end
register_format!("rst", Rest())
2020-05-30 17:49:20 +02:00
2020-06-14 11:15:05 +02:00
function render_figures(docformat::Rest, chunk)
2020-05-30 17:49:20 +02:00
fignames = chunk.figures
caption = chunk.options[:fig_cap]
2020-06-14 11:15:05 +02:00
width = chunk.options[:out_width]
2020-05-30 17:49:20 +02:00
result = ""
figstring = ""
2020-06-14 11:15:05 +02:00
for fig in fignames
figstring *= @sprintf(".. image:: %s\n :width: %s\n\n", fig, width)
end
2020-05-30 17:49:20 +02:00
2020-06-02 12:00:40 +02:00
if !isnothing(caption)
2020-06-14 11:15:05 +02:00
result *= string(
".. figure:: $(fignames[1])\n",
" :width: $width\n\n",
" $caption\n\n",
)
2020-05-30 17:49:20 +02:00
else
2020-06-14 11:15:05 +02:00
result *= figstring
return result
2020-05-30 17:49:20 +02:00
end
end
2020-06-14 09:36:51 +02:00
2020-06-14 11:15:05 +02:00
# Ansii
# -----
2020-06-14 09:36:51 +02:00
2020-06-14 11:15:05 +02:00
# asciidoc -b html5 -a source-highlighter=pygments ...
Base.@kwdef mutable struct AsciiDoc <: WeaveFormat
description = "AsciiDoc"
extension = "txt"
codestart = "[source,julia]\n--------------------------------------"
2020-10-02 21:21:19 +02:00
codeend = "--------------------------------------\n"
2020-06-14 09:36:51 +02:00
termstart = codestart
termend = codeend
2020-06-14 11:15:05 +02:00
outputstart = "--------------------------------------"
outputend = "--------------------------------------\n\n"
mimetypes = default_mime_types
2020-06-14 09:36:51 +02:00
fig_ext = ".png"
2020-06-14 11:15:05 +02:00
out_width = "600"
2020-06-14 09:36:51 +02:00
out_height = nothing
fig_pos = nothing
fig_env = nothing
end
2020-06-14 11:15:05 +02:00
register_format!("asciidoc", AsciiDoc())
2020-06-14 09:36:51 +02:00
2020-06-14 11:15:05 +02:00
function render_figures(docformat::AsciiDoc, chunk)
fignames = chunk.figures
caption = chunk.options[:fig_cap]
width = chunk.options[:out_width]
result = ""
figstring = ""
for fig in fignames
figstring *= @sprintf("image::%s[width=%s]\n", fig, width)
end
if !isnothing(caption)
result *= string("image::$(fignames[1])", "[width=$width,", "title=\"$caption\"]")
else
result *= figstring
return result
end
2020-06-14 09:36:51 +02:00
end