separate format logic from WeaveDoc:

- this is just a first step, though
pull/341/head
Shuhei Kadowaki 2020-05-23 20:07:59 +09:00
parent f71a491847
commit 3c76b804dc
6 changed files with 48 additions and 93 deletions

View File

@ -115,7 +115,7 @@ function weave(
cache::Symbol = :off,
throw_errors::Bool = false,
template::Union{Nothing,AbstractString,Mustache.MustacheTokens} = nothing,
css::Union{Nothing,AbstractString} = nothing,
css::Union{Nothing,AbstractString} = nothing, # TODO: rename to `stylesheet`
highlight_theme::Union{Nothing,Type{<:Highlights.AbstractTheme}} = nothing,
pandoc_options::Vector{<:AbstractString} = String[],
latex_cmd::AbstractString = "xelatex",
@ -191,12 +191,8 @@ function weave(
keep_unicode = get(weave_options, "keep_unicode", keep_unicode)
end
isnothing(template) || (doc.template = template)
isnothing(highlight_theme) || (doc.highlight_theme = highlight_theme)
# isnothing(theme) || (doc.theme = theme) # Reserved for themes
isnothing(css) || (doc.css = css)
get!(doc.format.formatdict, :keep_unicode, keep_unicode)
formatted = format(doc)
formatted = format(doc, template, highlight_theme; css = css)
outname = get_outname(out_path, doc)
@ -207,7 +203,7 @@ function weave(
if doctype == "pandoc2html"
mdname = outname
outname = get_outname(out_path, doc, ext = "html")
pandoc2html(formatted, doc, outname, pandoc_options)
pandoc2html(formatted, doc, highlight_theme, outname, pandoc_options)
rm(mdname)
elseif doctype == "pandoc2pdf"
mdname = outname

View File

@ -1,7 +1,7 @@
using Mustache, Highlights, .WeaveMarkdown, Markdown, Dates, Pkg
using REPL.REPLCompletions: latex_symbols
function format(doc)
function format(doc, template = nothing, highlight_theme = nothing; css = nothing)
format = doc.format
# Complete format dictionaries with defaults
@ -13,46 +13,29 @@ function format(doc)
get!(formatdict, :fig_pos, nothing)
get!(formatdict, :fig_env, nothing)
formatdict[:theme] = doc.highlight_theme
formatdict[:theme] = highlight_theme = get_highlight_theme(highlight_theme)
restore_header!(doc)
formatted = String[]
for chunk in copy(doc.chunks)
result = format_chunk(chunk, formatdict, format)
push!(formatted, result)
formatted_lines = map(copy(doc.chunks)) do chunk
format_chunk(chunk, formatdict, format)
end
formatted = join(formatted, "\n")
formatted = join(formatted_lines, '\n')
# Render using a template if needed
return render_doc(formatted, doc)
return format isa JMarkdown2HTML ? render2html(formatted, doc, template, css, highlight_theme) :
format isa JMarkdown2tex ? render2tex(formatted, doc, template, highlight_theme) :
formatted
end
render_doc(formatted, doc) = render_doc(formatted, doc, doc.format)
render_doc(formatted, doc, format) = formatted
function render_doc(formatted, doc, format::JMarkdown2HTML)
template = if isa(doc.template, Mustache.MustacheTokens)
doc.template
else
template_path = isempty(doc.template) ? normpath(TEMPLATE_DIR, "julia_html.tpl") : doc.template
Mustache.template_from_file(template_path)
end
themepath = isempty(doc.css) ? normpath(TEMPLATE_DIR, "skeleton_css.css") : doc.css
themecss = read(themepath, String)
highlightcss = stylesheet(MIME("text/html"), doc.highlight_theme)
function render2html(formatted, doc, template, css, highlight_theme)
_, source = splitdir(abspath(doc.source))
wversion, wdate = weave_info()
return Mustache.render(
template;
get_template(template, false);
body = formatted,
themecss = themecss,
highlightcss = highlightcss,
themecss = get_stylesheet(css),
highlightcss = get_highlight_stylesheet(MIME("text/html"), highlight_theme),
header_script = doc.header_script,
source = source,
wversion = wversion,
@ -61,25 +44,28 @@ function render_doc(formatted, doc, format::JMarkdown2HTML)
)
end
function render_doc(formatted, doc, format::JMarkdown2tex)
template = if isa(doc.template, Mustache.MustacheTokens)
doc.template
else
template_path = isempty(doc.template) ? normpath(TEMPLATE_DIR, "julia_tex.tpl") : doc.template
Mustache.template_from_file(template_path)
end
highlight = stylesheet(MIME("text/latex"), doc.highlight_theme)
function render2tex(formatted, doc, template, highlight_theme)
return Mustache.render(
template;
get_template(template, true);
body = formatted,
highlight = highlight,
highlight = get_highlight_stylesheet(MIME("text/latex"), highlight_theme),
[Pair(Symbol(k), v) for (k, v) in doc.header]...,
)
end
stylesheet(m::MIME, theme) = sprint((io, x) -> Highlights.stylesheet(io, m, x), theme)
get_highlight_theme(::Nothing) = Highlights.Themes.DefaultTheme
get_highlight_theme(highlight_theme::Type{<:Highlights.AbstractTheme}) = highlight_theme
get_template(::Nothing, tex::Bool = false) =
Mustache.template_from_file(normpath(TEMPLATE_DIR, tex ? "julia_tex.tpl" : "julia_html.tpl"))
get_template(path::AbstractString, tex) = Mustache.template_from_file(path)
get_template(tpl::Mustache.MustacheTokens, tex) = tpl
get_stylesheet(::Nothing) = get_stylesheet(normpath(TEMPLATE_DIR, "skeleton_css.css"))
get_stylesheet(path::AbstractString) = read(path, String)
get_highlight_stylesheet(mime, highlight_theme) =
sprint((io, x) -> Highlights.stylesheet(io, mime, x), highlight_theme)
const WEAVE_VERSION = try
'v' * Pkg.TOML.parsefile(normpath(PKG_DIR, "Project.toml"))["version"]

View File

@ -1,18 +1,7 @@
"""
pandoc2html(formatted::AbstractString, doc::WeaveDoc)
Convert output from pandoc markdown to html using Weave.jl template
"""
function pandoc2html(
formatted::AbstractString,
doc::WeaveDoc,
outname::AbstractString,
pandoc_options,
)
weavedir = dirname(@__FILE__)
html_template = joinpath(weavedir, "../templates/pandoc_skeleton.html")
css_template = joinpath(weavedir, "../templates/pandoc_skeleton.css")
highlightcss = stylesheet(MIME("text/html"), doc.highlight_theme)
function pandoc2html(formatted, doc, highlight_theme, outname, pandoc_options)
template_path = normpath(PKG_DIR, "templates/pandoc_skeleton.html")
themecss_path = normpath(PKG_DIR, "templates/pandoc_skeleton.css")
highlightcss = get_highlight_stylesheet(MIME("text/html"), highlight_theme)
path, wsource = splitdir(abspath(doc.source))
wversion, wdate = weave_info()
@ -37,8 +26,12 @@ function pandoc2html(
try
cmd = `pandoc -f markdown+raw_html -s --mathjax=""
$filt $citeproc $pandoc_options
--template $html_template -H $css_template $self_contained
-V wversion=$wversion -V wdate=$wdate -V wsource=$wsource
--template $template_path
-H $themecss_path
$self_contained
-V wversion=$wversion
-V wdate=$wdate
-V wsource=$wsource
-V highlightcss=$highlightcss
-V headerscript=$header_script
-o $outname`
@ -54,17 +47,7 @@ function pandoc2html(
end
end
"""
pandoc2pdf(formatted::AbstractString, doc::WeaveDoc)
Convert output from pandoc markdown to pdf using Weave.jl template
"""
function pandoc2pdf(
formatted::AbstractString,
doc::WeaveDoc,
outname::AbstractString,
pandoc_options,
)
function pandoc2pdf(formatted, doc, outname, pandoc_options)
weavedir = dirname(@__FILE__)
header_template = joinpath(weavedir, "../templates/pandoc_header.txt")

View File

@ -36,9 +36,6 @@ function WeaveDoc(source, informat = nothing)
"",
"",
header,
"",
"",
Highlights.Themes.DefaultTheme,
chunk_defaults,
)
end

View File

@ -12,9 +12,6 @@ mutable struct WeaveDoc
doctype::String
header_script::String
header::Dict
template::Union{AbstractString,Mustache.MustacheTokens}
css::AbstractString
highlight_theme::Type{<:Highlights.AbstractTheme}
chunk_defaults::Dict{Symbol,Any}
end

View File

@ -1,3 +1,7 @@
# TODO: this test is horrible, refactor
using Weave: Highlights.Themes.DefaultTheme
# Test rendering of doc chunks
content = """
# Test chunk
@ -22,7 +26,7 @@ parsed = Weave.WeaveDoc("documents/chunk_options.noweb")
doc = run_doc(parsed, doctype = "md2html")
c_check = "<pre class='hljl'>\n<span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-p'>[</span><span class='hljl-ni'>12</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-ni'>10</span><span class='hljl-p'>]</span><span class='hljl-t'>\n</span><span class='hljl-nf'>println</span><span class='hljl-p'>(</span><span class='hljl-n'>y</span><span class='hljl-p'>)</span>\n</pre>\n"
doc.format.formatdict[:theme] = doc.highlight_theme
doc.format.formatdict[:theme] = DefaultTheme
c = Weave.format_code(doc.chunks[3].content, doc.format)
@test c_check == c
@ -30,16 +34,12 @@ o_check = "\nprintln&#40;x&#41;\n"
o = Weave.format_output(doc.chunks[4].content, doc.format)
@test o_check == o
doc.template = "templates/mini.tpl"
rendered = Weave.render_doc("Hello", doc)
@test rendered == "\nHello\n"
# Tex format
parsed = Weave.WeaveDoc("documents/chunk_options.noweb")
doc = run_doc(parsed, doctype = "md2tex")
c_check = "\\begin{lstlisting}\n(*@\\HLJLnf{println}@*)(*@\\HLJLp{(}@*)(*@\\HLJLn{x}@*)(*@\\HLJLp{)}@*)\n\\end{lstlisting}\n"
doc.format.formatdict[:theme] = doc.highlight_theme
doc.format.formatdict[:theme] = DefaultTheme
c = Weave.format_code(doc.chunks[4].content, doc.format)
@test c_check == c
@ -47,10 +47,6 @@ o_check = "\nx = [12, 10]\nprintln(y)\n"
o = Weave.format_output(doc.chunks[3].content, doc.format)
@test o_check == o
doc.template = "templates/mini.tpl"
rendered = Weave.render_doc("Hello", doc)
@test rendered == "\nHello\n"
# Test wrapping
cows = repeat("🐄", 100)