set rendering options with dispatch:

- remove unused field defs
- in theory extensible, once documented
- fix test
- start to change terminology, `format` -> `render`
pull/350/head
Shuhei Kadowaki 2020-06-01 22:36:48 +09:00
parent ae0dc70ffa
commit 55d2c975cd
8 changed files with 57 additions and 70 deletions

View File

@ -128,7 +128,6 @@ function weave(
)
doc = WeaveDoc(source, informat)
# TODO : put all argument parsing into separate function
# run document
# ------------
@ -175,20 +174,20 @@ function weave(
throw_errors = throw_errors,
)
# format document
# render document
# ---------------
# overwrites options with those specified in header, that are needed for formatting document
# overwrites options with those specified in header, that are needed for rendering document
# NOTE: these YAML options can be given dynamically
if !isnothing(weave_options)
if haskey(weave_options, "template")
template = weave_options["template"]
# resolve relative to this document
# resolve relative to this document
template isa AbstractString && (template = normpath(dirname(source), template))
end
if haskey(weave_options, "css")
css = weave_options["css"]
# resolve relative to this document
# resolve relative to this document
css isa AbstractString && (css = normpath(dirname(source), css))
end
highlight_theme = get(weave_options, "highlight_theme", highlight_theme)
@ -197,11 +196,8 @@ function weave(
keep_unicode = get(weave_options, "keep_unicode", keep_unicode)
end
doc.format.keep_unicode = doc.format.keep_unicode | keep_unicode
doc.format.highlight_theme = get_highlight_theme(highlight_theme)
# this overwrites template given in docformat
doc.format.template = template
rendered = format(doc; css = css)
set_rendering_options!(doc; template = template, highlight_theme = highlight_theme, css = css, keep_unicode = keep_unicode)
rendered = render_doc(doc)
outname = get_outname(out_path, doc)

View File

@ -1,6 +1,10 @@
# TODO: fix terminologies: `format_foo` -> `render_foo`
# fallback methods
# ----------------
set_rendering_options!(docformat::WeaveFormat; kwargs...) = return
# TODO: is there any other format where we want to restore headers ?
# make this field of format struct
const HEADER_PRESERVE_DOCTYPES = ("github", "hugo")
@ -139,7 +143,7 @@ end
should_render(chunk) = chunk.options[:echo] && chunk.options[:results] "hidden"
render_doc(_, body, args...; kwargs...) = body
render_doc(docformat, body, args...) = body
# utilities
# ---------

View File

@ -12,7 +12,6 @@ Base.@kwdef mutable struct JMarkdown2HTML <: HTMLFormat
fig_ext = ".png"
mimetypes = ["image/png", "image/jpg", "image/svg+xml",
"text/html", "text/markdown", "text/plain"]
keep_unicode = false
extension = "html"
termstart = codestart
termend = codeend
@ -20,11 +19,24 @@ Base.@kwdef mutable struct JMarkdown2HTML <: HTMLFormat
out_height = nothing
fig_pos = nothing
fig_env = nothing
template = nothing
stylesheet = nothing
highlight_theme = nothing
template = normpath(TEMPLATE_DIR, "md2html.tpl")
end
register_format!("md2html", JMarkdown2HTML())
function set_rendering_options!(docformat::JMarkdown2HTML; template = nothing, css = nothing, highlight_theme = nothing, kwargs...)
docformat.template = get_html_template(template)
docformat.stylesheet = get_stylesheet(css)
docformat.highlight_theme = get_highlight_theme(highlight_theme)
end
get_html_template(::Nothing) = get_template(normpath(TEMPLATE_DIR, "md2html.tpl"))
get_html_template(x) = get_template(x)
get_stylesheet(::Nothing) = get_stylesheet(normpath(STYLESHEET_DIR, "skeleton.css"))
get_stylesheet(path::AbstractString) = read(path, String)
Base.@kwdef mutable struct Pandoc2HTML <: HTMLFormat
description = "Markdown to HTML (requires Pandoc 2)"
codestart = "\n"
@ -35,15 +47,12 @@ Base.@kwdef mutable struct Pandoc2HTML <: HTMLFormat
extension = "md"
mimetypes = ["image/png", "image/svg+xml", "image/jpg",
"text/html", "text/markdown", "text/plain"]
keep_unicode = false
termstart = codestart
termend = codeend
out_width = nothing
out_height = nothing
fig_pos = nothing
fig_env = nothing
highlight_theme = nothing
template = normpath(TEMPLATE_DIR, "pandoc2html.tpl")
end
register_format!("pandoc2html", Pandoc2HTML())
@ -121,9 +130,9 @@ function render_doc(docformat::JMarkdown2HTML, body, doc; css = nothing)
weave_version, weave_date = weave_info()
return Mustache.render(
get_html_template(docformat.template);
docformat.template;
body = body,
stylesheet = get_stylesheet(css),
stylesheet = docformat.stylesheet,
highlight_stylesheet = get_highlight_stylesheet(MIME("text/html"), docformat.highlight_theme),
header_script = doc.header_script,
weave_source = weave_source,
@ -132,8 +141,3 @@ function render_doc(docformat::JMarkdown2HTML, body, doc; css = nothing)
[Pair(Symbol(k), v) for (k, v) in doc.header]...,
)
end
get_stylesheet(::Nothing) = get_stylesheet(normpath(STYLESHEET_DIR, "skeleton.css"))
get_stylesheet(path::AbstractString) = read(path, String)
get_html_template(::Nothing) = get_template(normpath(TEMPLATE_DIR, "md2html.tpl"))
get_html_template(x) = get_template(x)

View File

@ -13,16 +13,12 @@ Base.@kwdef mutable struct GitHubMarkdown <: MarkdownFormat
extension = "md"
mimetypes = ["image/png", "image/svg+xml", "image/jpg",
"text/markdown", "text/plain"]
keep_unicode = false
termstart = codestart
termend = codeend
out_width = nothing
out_height = nothing
fig_pos = nothing
fig_env = nothing
highlight_theme = nothing
#this could be removed
template = nothing
end
register_format!("github", GitHubMarkdown())
@ -60,7 +56,6 @@ Base.@kwdef mutable struct Hugo <: MarkdownFormat
fig_ext = ".png"
extension = "md"
uglyURLs = false # if `false`, prepend figure path by `..`
keep_unicode = false
mimetypes = default_mime_types
termstart = codestart
termend = codeend
@ -68,9 +63,6 @@ Base.@kwdef mutable struct Hugo <: MarkdownFormat
out_height = nothing
fig_pos = nothing
fig_env = nothing
highlight_theme = nothing
#this could be removed
template = nothing
end
register_format!("hugo", Hugo())
@ -99,16 +91,12 @@ Base.@kwdef mutable struct MultiMarkdown <: MarkdownFormat
outputend = "````\n\n"
fig_ext = ".png"
extension = "md"
keep_unicode = false
termstart = codestart
termend = codeend
out_width = nothing
out_height = nothing
fig_pos = nothing
fig_env = nothing
highlight_theme = nothing
#this could be removed
template = nothing
mimetypes = default_mime_types
end
register_format!("multimarkdown", MultiMarkdown())
@ -158,15 +146,12 @@ Base.@kwdef mutable struct Pandoc <: MarkdownFormat
# Prefer png figures for markdown conversion, svg doesn't work with latex
mimetypes = ["image/png", "image/jpg", "image/svg+xml",
"text/markdown", "text/plain"]
keep_unicode = false
termstart = codestart
termend = codeend
out_width = nothing
out_height = nothing
fig_pos = nothing
fig_env = nothing
highlight_theme = nothing
template = nothing
end
register_format!("pandoc", Pandoc())
register_format!("pandoc2pdf", Pandoc())

View File

@ -12,9 +12,11 @@ const FORMATS = Dict{String,WeaveFormat}()
# TODO: do some assertion for necessary fields of `format`
register_format!(format_name::AbstractString, format::WeaveFormat) = push!(FORMATS, format_name => format)
register_format!(_,format) = error("Format needs to be a subtype of WeaveFormat.")
register_format!(_, format) = error("Format needs to be a subtype of WeaveFormat.")
function format(doc; kwargs...)
set_rendering_options!(doc; kwargs...) = set_rendering_options!(doc.format; kwargs...)
function render_doc(doc)
docformat = doc.format
restore_header!(doc)
@ -24,7 +26,7 @@ function format(doc; kwargs...)
end
body = join(lines, '\n')
return render_doc(docformat, body, doc; kwargs...)
return render_doc(docformat, body, doc)
end

View File

@ -3,6 +3,8 @@
abstract type TexFormat <: WeaveFormat end
set_rendering_options!(docformat::TexFormat; keep_unicode = false, kwargs...) = docformat.keep_unicode |= keep_unicode
Base.@kwdef mutable struct JMarkdown2tex <: TexFormat
description = "Julia markdown to latex"
codestart = ""
@ -26,6 +28,24 @@ end
register_format!("md2tex", JMarkdown2tex())
register_format!("md2pdf", JMarkdown2tex())
function set_rendering_options!(docformat::JMarkdown2tex; template = nothing, highlight_theme = nothing, keep_unicode = false, kwargs...)
docformat.template = get_tex_template(template)
docformat.highlight_theme = get_highlight_theme(highlight_theme)
docformat.keep_unicode |= keep_unicode
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)
return Mustache.render(
docformat.template;
body = body,
highlight = get_highlight_stylesheet(MIME("text/latex"), docformat.highlight_theme),
[Pair(Symbol(k), v) for (k, v) in doc.header]...,
)
end
Base.@kwdef mutable struct Tex <: TexFormat
description = "Latex with custom code environments"
codestart = "\\begin{juliacode}"
@ -42,8 +62,6 @@ Base.@kwdef mutable struct Tex <: TexFormat
keep_unicode = false
out_width = "\\linewidth"
out_height = nothing
highlight_theme = nothing
template = normpath(TEMPLATE_DIR, "md2pdf.tpl")
end
register_format!("tex", Tex())
@ -66,8 +84,6 @@ Base.@kwdef mutable struct TexMinted <: TexFormat
out_height = nothing
fig_env = "figure"
fig_pos = "htpb"
highlight_theme = nothing
template = normpath(TEMPLATE_DIR, "md2pdf.tpl")
end
register_format!("texminted", TexMinted())
@ -121,7 +137,7 @@ function uc2tex(s, escape = false)
return s
end
#should_render(chunk) ? highlight_term(MIME("text/latex"), , docformat.highlight_theme) : ""
# should_render(chunk) ? highlight_term(MIME("text/latex"), , docformat.highlight_theme) : ""
format_termchunk(chunk, docformat::JMarkdown2tex) =
should_render(chunk) ? highlight_term(MIME("text/latex"), chunk.output, docformat.highlight_theme) : ""
@ -207,15 +223,3 @@ function md_length_to_latex(def, reference)
end
return def
end
function render_doc(docformat::JMarkdown2tex, body, doc; kwargs...)
return Mustache.render(
get_tex_template(docformat.template);
body = body,
highlight = get_highlight_stylesheet(MIME("text/latex"), docformat.highlight_theme),
[Pair(Symbol(k), v) for (k, v) in doc.header]...,
)
end
get_tex_template(::Nothing) = get_template(normpath(TEMPLATE_DIR, "md2pdf.tpl"))
get_tex_template(x) = get_template(x)

View File

@ -10,17 +10,12 @@ Base.@kwdef mutable struct Rest <: WeaveFormat
indent = 4
fig_ext = ".png"
extension = "rst"
keep_unicode = false
termstart = codestart
termend = codeend
out_width = "15 cm"
out_height = nothing
fig_pos = nothing
fig_env = nothing
highlight_theme = nothing
#this could be removed if argument parsing checked whether the format was
# compatible with templates
template = nothing
mimetypes = default_mime_types
end
register_format!("rst", Rest())
@ -60,16 +55,12 @@ Base.@kwdef mutable struct AsciiDoc <: WeaveFormat
outputend = "--------------------------------------\n\n"
fig_ext = ".png"
extension = "txt"
keep_unicode = false
termstart = codestart
termend = codeend
out_width = "600"
out_height = nothing
fig_pos = nothing
fig_env = nothing
highlight_theme = nothing
#this could be removed
template = nothing
mimetypes = default_mime_types
end
register_format!("asciidoc", AsciiDoc())

View File

@ -104,13 +104,14 @@ str = """
```
"""
doc = mock_doc(str; doctype = "md2tex")
doc = Weave.format(doc)
Weave.set_rendering_options!(doc.format)
doc = Weave.render_doc(doc)
@test occursin(Weave.uc2tex("α"), doc)
@test !occursin("α", doc)
doc = mock_doc(str; doctype = "md2tex")
doc.format.keep_unicode = true
doc = Weave.format(doc)
Weave.set_rendering_options!(doc.format; keep_unicode = true)
doc = Weave.render_doc(doc)
@test occursin("α", doc)
@test !occursin(Weave.uc2tex("α"), doc)