mirror of https://github.com/mpastell/Weave.jl
commit
60fc450c88
15
src/Weave.jl
15
src/Weave.jl
|
@ -22,6 +22,7 @@ end
|
|||
isnothing(::Any) = false
|
||||
isnothing(::Nothing) = true
|
||||
end
|
||||
take2string!(io) = String(take!(io))
|
||||
|
||||
"""
|
||||
list_out_formats()
|
||||
|
@ -116,7 +117,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",
|
||||
|
@ -192,28 +193,24 @@ 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)
|
||||
rendered = format(doc, template, highlight_theme; css = css)
|
||||
|
||||
outname = get_outname(out_path, doc)
|
||||
|
||||
open(io->write(io,formatted), outname, "w")
|
||||
open(io->write(io,rendered), outname, "w")
|
||||
|
||||
# document generation via external programs
|
||||
doctype = doc.doctype
|
||||
if doctype == "pandoc2html"
|
||||
mdname = outname
|
||||
outname = get_outname(out_path, doc, ext = "html")
|
||||
pandoc2html(formatted, doc, outname, pandoc_options)
|
||||
pandoc2html(rendered, doc, highlight_theme, outname, pandoc_options)
|
||||
rm(mdname)
|
||||
elseif doctype == "pandoc2pdf"
|
||||
mdname = outname
|
||||
outname = get_outname(out_path, doc, ext = "pdf")
|
||||
pandoc2pdf(formatted, doc, outname, pandoc_options)
|
||||
pandoc2pdf(rendered, doc, outname, pandoc_options)
|
||||
rm(mdname)
|
||||
elseif doctype == "md2pdf"
|
||||
run_latex(doc, outname, latex_cmd)
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
# module Markdown2HTML
|
||||
# Markdown to HTML writer, Modified from Julia Base.Markdown html writer
|
||||
|
||||
using Markdown:
|
||||
MD,
|
||||
Header,
|
||||
|
@ -19,13 +19,10 @@ using Markdown:
|
|||
LaTeX,
|
||||
isordered
|
||||
|
||||
function tohtml(io::IO, m::MIME"text/html", x)
|
||||
show(io, m, x)
|
||||
end
|
||||
|
||||
function tohtml(io::IO, m::MIME"text/plain", x)
|
||||
htmlesc(io, sprint(show, m, x))
|
||||
end
|
||||
tohtml(io::IO, m::MIME"text/html", x) = show(io, m, x)
|
||||
|
||||
tohtml(io::IO, m::MIME"text/plain", x) = htmlesc(io, sprint(show, m, x))
|
||||
|
||||
function tohtml(io::IO, m::MIME"image/png", img)
|
||||
print(io, """<img src="data:image/png;base64,""")
|
||||
|
@ -33,9 +30,7 @@ function tohtml(io::IO, m::MIME"image/png", img)
|
|||
print(io, "\" />")
|
||||
end
|
||||
|
||||
function tohtml(m::MIME"image/svg+xml", img)
|
||||
show(io, m, img)
|
||||
end
|
||||
tohtml(m::MIME"image/svg+xml", img) = show(io, m, img)
|
||||
|
||||
# AbstractDisplay infrastructure
|
||||
|
||||
|
@ -168,9 +163,7 @@ function html(io::IO, md::List)
|
|||
end
|
||||
end
|
||||
|
||||
function html(io::IO, md::HorizontalRule)
|
||||
tag(io, :hr)
|
||||
end
|
||||
html(io::IO, md::HorizontalRule) = tag(io, :hr)
|
||||
|
||||
function html(io::IO, tex::LaTeX)
|
||||
withtag(io, :p, :class => "math") do
|
||||
|
@ -178,9 +171,7 @@ function html(io::IO, tex::LaTeX)
|
|||
end
|
||||
end
|
||||
|
||||
function html(io::IO, comment::Comment)
|
||||
write(io, "\n<!-- $(comment.text) -->\n")
|
||||
end
|
||||
html(io::IO, comment::Comment) = write(io, "\n<!-- $(comment.text) -->\n")
|
||||
|
||||
function html(io::IO, md::Table)
|
||||
withtag(io, :table) do
|
||||
|
@ -218,9 +209,7 @@ function htmlinline(io::IO, tex::LaTeX)
|
|||
end
|
||||
end
|
||||
|
||||
function htmlinline(io::IO, md::Union{Symbol,AbstractString})
|
||||
htmlesc(io, md)
|
||||
end
|
||||
htmlinline(io::IO, md::Union{Symbol,AbstractString}) = htmlesc(io, md)
|
||||
|
||||
function htmlinline(io::IO, md::Bold)
|
||||
withtag(io, :strong) do
|
||||
|
@ -234,9 +223,7 @@ function htmlinline(io::IO, md::Italic)
|
|||
end
|
||||
end
|
||||
|
||||
function htmlinline(io::IO, md::Image)
|
||||
tag(io, :img, :src => md.url, :alt => md.alt)
|
||||
end
|
||||
htmlinline(io::IO, md::Image) = tag(io, :img, :src => md.url, :alt => md.alt)
|
||||
|
||||
function htmlinline(io::IO, f::Footnote)
|
||||
withtag(io, :a, :href => "#footnote-$(f.id)", :class => "footnote") do
|
||||
|
@ -250,18 +237,12 @@ function htmlinline(io::IO, link::Link)
|
|||
end
|
||||
end
|
||||
|
||||
function htmlinline(io::IO, br::LineBreak)
|
||||
tag(io, :br)
|
||||
end
|
||||
htmlinline(io::IO, br::LineBreak) = tag(io, :br)
|
||||
|
||||
function htmlinline(io::IO, comment::Comment)
|
||||
write(io, "<!-- $(comment.text) -->")
|
||||
end
|
||||
htmlinline(io::IO, comment::Comment) = write(io, "<!-- $(comment.text) -->")
|
||||
|
||||
htmlinline(io::IO, x) = tohtml(io, x)
|
||||
|
||||
# API
|
||||
|
||||
html(md) = sprint(html, md)
|
||||
|
||||
# end
|
||||
|
|
|
@ -1,12 +1,9 @@
|
|||
import Markdown: latex, latexinline
|
||||
|
||||
# Remove comments that can occur inside a line
|
||||
function latexinline(io, comment::WeaveMarkdown.Comment)
|
||||
write(io, "")
|
||||
end
|
||||
|
||||
function latex(io::IO, comment::WeaveMarkdown.Comment)
|
||||
function latex(io::IO, comment::Comment)
|
||||
for line in split(comment.text, r"\r\n|\n")
|
||||
write(io, "% $line\n")
|
||||
end
|
||||
end
|
||||
|
||||
latexinline(io, comment::Comment) = write(io, "")
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
# This module extends the julia markdown parser to improve compatibility with Jupyter, Pandoc etc.
|
||||
module WeaveMarkdown
|
||||
|
||||
using ..Weave: isnothing, take2string!
|
||||
using Markdown
|
||||
import Markdown: @trigger, @breaking, Code, MD, withstream, startswith, LaTeX
|
||||
|
||||
|
||||
function __init__()
|
||||
# NOTE:
|
||||
# overwriting `Markdown.latex` function should be done here in order to allow
|
||||
|
@ -37,7 +40,7 @@ end
|
|||
if !isempty(estr)
|
||||
estr = Markdown.startswith(stream, r"^\$\$$"m)
|
||||
if isempty(estr)
|
||||
push!(block, LaTeX(String(take!(buffer)) |> chomp))
|
||||
push!(block, LaTeX(take2string!(buffer) |> chomp))
|
||||
end
|
||||
return true
|
||||
else
|
||||
|
@ -58,7 +61,7 @@ end
|
|||
line = readline(stream, keep = true)
|
||||
write(buffer, line)
|
||||
if occursin(r"-->$", line)
|
||||
s = replace(String(take!(buffer)) |> chomp, r"-->$" => "")
|
||||
s = replace(take2string!(buffer) |> chomp, r"-->$" => "")
|
||||
push!(block, Comment(s))
|
||||
return true
|
||||
end
|
||||
|
@ -71,7 +74,7 @@ end
|
|||
withstream(stream) do
|
||||
Markdown.startswith(stream, "<!--") || return
|
||||
text = Markdown.readuntil(stream, "-->")
|
||||
text ≡ nothing && return
|
||||
isnothing(text) && return
|
||||
return Comment(text)
|
||||
end
|
||||
end
|
||||
|
@ -88,6 +91,8 @@ for key in keys(Markdown.julia.inner)
|
|||
end
|
||||
end
|
||||
|
||||
|
||||
include("html.jl")
|
||||
include("latex.jl")
|
||||
end
|
||||
|
||||
end # module
|
||||
|
|
|
@ -111,7 +111,7 @@ end
|
|||
function Base.display(report::Report, m::MIME"text/html", data)
|
||||
io = IOBuffer()
|
||||
show(IOContext(io, :limit => true), m, data)
|
||||
report.rich_output *= "\n" * String(take!(io))
|
||||
report.rich_output *= string('\n', take2string!(io))
|
||||
end
|
||||
|
||||
# Catch "rich_output"
|
||||
|
@ -131,7 +131,7 @@ end
|
|||
|
||||
function Base.display(report::Report, m::MIME"text/latex", data)
|
||||
s = repr(m, data)
|
||||
report.rich_output *= "\n" * s
|
||||
report.rich_output *= string('\n', s)
|
||||
end
|
||||
|
||||
"""Add saved figure name to results and return the name"""
|
||||
|
|
326
src/format.jl
326
src/format.jl
|
@ -1,86 +1,72 @@
|
|||
# TODO: reorganize this file into multiple files corresponding to each output format
|
||||
|
||||
using Mustache, Highlights, .WeaveMarkdown, Markdown, Dates, Pkg
|
||||
using REPL.REPLCompletions: latex_symbols
|
||||
|
||||
function format(doc)
|
||||
format = doc.format
|
||||
function format(doc, template = nothing, highlight_theme = nothing; css = nothing)
|
||||
docformat = doc.format
|
||||
|
||||
# Complete format dictionaries with defaults
|
||||
formatdict = format.formatdict
|
||||
get!(formatdict, :termstart, formatdict[:codestart])
|
||||
get!(formatdict, :termend, formatdict[:codeend])
|
||||
get!(formatdict, :out_width, nothing)
|
||||
get!(formatdict, :out_height, nothing)
|
||||
get!(formatdict, :fig_pos, nothing)
|
||||
get!(formatdict, :fig_env, nothing)
|
||||
|
||||
formatdict[:cwd] = doc.cwd # pass wd to figure formatters
|
||||
formatdict[:theme] = doc.highlight_theme
|
||||
get!(docformat.formatdict, :termstart, docformat.formatdict[:codestart])
|
||||
get!(docformat.formatdict, :termend, docformat.formatdict[:codeend])
|
||||
get!(docformat.formatdict, :out_width, nothing)
|
||||
get!(docformat.formatdict, :out_height, nothing)
|
||||
get!(docformat.formatdict, :fig_pos, nothing)
|
||||
get!(docformat.formatdict, :fig_env, nothing)
|
||||
get_highlight_theme(docformat) = 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)
|
||||
lines = map(copy(doc.chunks)) do chunk
|
||||
format_chunk(chunk, docformat)
|
||||
end
|
||||
formatted = join(formatted, "\n")
|
||||
body = join(lines, '\n')
|
||||
|
||||
# Render using a template if needed
|
||||
return render_doc(formatted, doc)
|
||||
return format isa JMarkdown2HTML ? render2html(body, doc, template, css, highlight_theme) :
|
||||
format isa JMarkdown2tex ? render2tex(body, doc, template, highlight_theme) :
|
||||
body
|
||||
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)
|
||||
|
||||
_, source = splitdir(abspath(doc.source))
|
||||
wversion, wdate = weave_info()
|
||||
function render2html(body, doc, template, css, highlight_theme)
|
||||
_, weave_source = splitdir(abspath(doc.source))
|
||||
weave_version, weave_date = weave_info()
|
||||
|
||||
return Mustache.render(
|
||||
template;
|
||||
body = formatted,
|
||||
themecss = themecss,
|
||||
highlightcss = highlightcss,
|
||||
get_template(template, false);
|
||||
body = body,
|
||||
stylesheet = get_stylesheet(css),
|
||||
highlight_stylesheet = get_highlight_stylesheet(MIME("text/html"), highlight_theme),
|
||||
header_script = doc.header_script,
|
||||
source = source,
|
||||
wversion = wversion,
|
||||
wdate = wdate,
|
||||
weave_source = weave_source,
|
||||
weave_version = weave_version,
|
||||
weave_date = weave_date,
|
||||
[Pair(Symbol(k), v) for (k, v) in doc.header]...,
|
||||
)
|
||||
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(body, doc, template, highlight_theme)
|
||||
return Mustache.render(
|
||||
template;
|
||||
body = formatted,
|
||||
highlight = highlight,
|
||||
get_template(template, true);
|
||||
body = body,
|
||||
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_highlight_theme(docformat) = get_highlight_theme(get(docformat.formatdict, :highlight_theme, nothing))
|
||||
|
||||
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"]
|
||||
|
@ -109,7 +95,7 @@ function restore_header!(doc)
|
|||
pushfirst!(doc.chunks, DocChunk(header_text, 0, 0))
|
||||
end
|
||||
|
||||
format_chunk(chunk::DocChunk, formatdict, docformat) = join((format_inline(c) for c in chunk.content))
|
||||
format_chunk(chunk::DocChunk, docformat) = join((format_inline(c) for c in chunk.content))
|
||||
|
||||
format_inline(inline::InlineText) = inline.content
|
||||
|
||||
|
@ -119,106 +105,99 @@ function format_inline(inline::InlineCode)
|
|||
return inline.output
|
||||
end
|
||||
|
||||
function ioformat!(io::IOBuffer, out::IOBuffer, fun = WeaveMarkdown.latex)
|
||||
text = String(take!(io))
|
||||
if !isempty(text)
|
||||
m = Markdown.parse(text, flavor = WeaveMarkdown.weavemd)
|
||||
write(out, string(fun(m)))
|
||||
end
|
||||
end
|
||||
|
||||
addspace(op, inline) = (inline.ctype === :line && (op = "\n$op\n"); op)
|
||||
|
||||
function format_chunk(chunk::DocChunk, formatdict, docformat::JMarkdown2tex)
|
||||
function format_chunk(chunk::DocChunk, docformat::JMarkdown2tex)
|
||||
out = IOBuffer()
|
||||
io = IOBuffer()
|
||||
for inline in chunk.content
|
||||
if isa(inline, InlineText)
|
||||
write(io, inline.content)
|
||||
elseif !isempty(inline.rich_output)
|
||||
ioformat!(io, out)
|
||||
write(out, addspace(inline.rich_output, inline))
|
||||
clear_buffer_and_format!(io, out, WeaveMarkdown.latex)
|
||||
write(out, addlines(inline.rich_output, inline))
|
||||
elseif !isempty(inline.figures)
|
||||
write(io, inline.figures[end], inline)
|
||||
elseif !isempty(inline.output)
|
||||
write(io, addspace(inline.output, inline))
|
||||
write(io, addlines(inline.output, inline))
|
||||
end
|
||||
end
|
||||
ioformat!(io, out)
|
||||
formatdict[:keep_unicode] || return uc2tex(String(take!(out)))
|
||||
return String(take!(out))
|
||||
clear_buffer_and_format!(io, out, WeaveMarkdown.latex)
|
||||
out = take2string!(out)
|
||||
return docformat.formatdict[:keep_unicode] ? out : uc2tex(out)
|
||||
end
|
||||
|
||||
function format_chunk(chunk::DocChunk, formatdict, docformat::JMarkdown2HTML)
|
||||
function format_chunk(chunk::DocChunk, docformat::JMarkdown2HTML)
|
||||
out = IOBuffer()
|
||||
io = IOBuffer()
|
||||
fun = WeaveMarkdown.html
|
||||
for inline in chunk.content
|
||||
if isa(inline, InlineText)
|
||||
write(io, inline.content)
|
||||
elseif !isempty(inline.rich_output)
|
||||
ioformat!(io, out, fun)
|
||||
write(out, addspace(inline.rich_output, inline))
|
||||
clear_buffer_and_format!(io, out, WeaveMarkdown.html)
|
||||
write(out, addlines(inline.rich_output, inline))
|
||||
elseif !isempty(inline.figures)
|
||||
write(io, inline.figures[end])
|
||||
elseif !isempty(inline.output)
|
||||
write(io, addspace(inline.output, inline))
|
||||
write(io, addlines(inline.output, inline))
|
||||
end
|
||||
end
|
||||
ioformat!(io, out, fun)
|
||||
return String(take!(out))
|
||||
clear_buffer_and_format!(io, out, WeaveMarkdown.html)
|
||||
return take2string!(out)
|
||||
end
|
||||
|
||||
function format_chunk(chunk::CodeChunk, formatdict, docformat)
|
||||
function clear_buffer_and_format!(io::IOBuffer, out::IOBuffer, render_function)
|
||||
text = take2string!(io)
|
||||
m = Markdown.parse(text, flavor = WeaveMarkdown.weavemd)
|
||||
write(out, string(render_function(m)))
|
||||
end
|
||||
|
||||
addlines(op, inline) = inline.ctype === :line ? string('\n', op, '\n') : op
|
||||
|
||||
function format_chunk(chunk::CodeChunk, docformat)
|
||||
formatdict = docformat.formatdict
|
||||
|
||||
# Fill undefined options with format specific defaults
|
||||
chunk.options[:out_width] == nothing &&
|
||||
(chunk.options[:out_width] = formatdict[:out_width])
|
||||
chunk.options[:fig_pos] == nothing && (chunk.options[:fig_pos] = formatdict[:fig_pos])
|
||||
isnothing(chunk.options[:out_width]) && (chunk.options[:out_width] = formatdict[:out_width])
|
||||
isnothing(chunk.options[:fig_pos]) && (chunk.options[:fig_pos] = formatdict[:fig_pos])
|
||||
|
||||
# Only use floats if chunk has caption or sets fig_env
|
||||
if chunk.options[:fig_cap] != nothing && chunk.options[:fig_env] == nothing
|
||||
if !isnothing(chunk.options[:fig_cap]) && isnothing(chunk.options[:fig_env])
|
||||
(chunk.options[:fig_env] = formatdict[:fig_env])
|
||||
end
|
||||
|
||||
if haskey(formatdict, :indent)
|
||||
chunk.content = indent(chunk.content, formatdict[:indent])
|
||||
end
|
||||
haskey(formatdict, :indent) && (chunk.content = indent(chunk.content, formatdict[:indent]))
|
||||
|
||||
chunk.content = format_code(chunk.content, docformat)
|
||||
|
||||
if !chunk.options[:eval]
|
||||
if chunk.options[:echo]
|
||||
result = "$(formatdict[:codestart])\n$(chunk.content)$(formatdict[:codeend])"
|
||||
return result
|
||||
return if chunk.options[:echo]
|
||||
string(formatdict[:codestart], '\n', chunk.content, formatdict[:codeend])
|
||||
else
|
||||
r = ""
|
||||
return r
|
||||
""
|
||||
end
|
||||
end
|
||||
|
||||
if chunk.options[:term]
|
||||
result = format_termchunk(chunk, formatdict, docformat)
|
||||
result = format_termchunk(chunk, docformat)
|
||||
else
|
||||
|
||||
if chunk.options[:echo]
|
||||
result = if chunk.options[:echo]
|
||||
# Convert to output format and highlight (html, tex...) if needed
|
||||
result = "$(formatdict[:codestart])$(chunk.content)$(formatdict[:codeend])\n"
|
||||
string(formatdict[:codestart], chunk.content, formatdict[:codeend], '\n')
|
||||
else
|
||||
result = ""
|
||||
""
|
||||
end
|
||||
|
||||
if (strip(chunk.output) != "" || strip(chunk.rich_output) != "") &&
|
||||
(chunk.options[:results] != "hidden")
|
||||
if chunk.options[:results] != "markup" && chunk.options[:results] != "hold"
|
||||
if (strip(chunk.output) ≠ "" || strip(chunk.rich_output) ≠ "") &&
|
||||
(chunk.options[:results] ≠ "hidden")
|
||||
if chunk.options[:results] ≠ "markup" && chunk.options[:results] ≠ "hold"
|
||||
strip(chunk.output) ≠ "" && (result *= "$(chunk.output)\n")
|
||||
strip(chunk.rich_output) ≠ "" && (result *= "$(chunk.rich_output)\n")
|
||||
else
|
||||
if chunk.options[:wrap]
|
||||
chunk.output =
|
||||
"\n" * wraplines(chunk.output, chunk.options[:line_width])
|
||||
'\n' * wraplines(chunk.output, chunk.options[:line_width])
|
||||
chunk.output = format_output(chunk.output, docformat)
|
||||
else
|
||||
chunk.output = "\n" * rstrip(chunk.output)
|
||||
chunk.output = '\n' * rstrip(chunk.output)
|
||||
chunk.output = format_output(chunk.output, docformat)
|
||||
end
|
||||
|
||||
|
@ -228,10 +207,9 @@ function format_chunk(chunk::CodeChunk, formatdict, docformat)
|
|||
strip(chunk.output) ≠ "" && (
|
||||
result *= "$(formatdict[:outputstart])$(chunk.output)\n$(formatdict[:outputend])\n"
|
||||
)
|
||||
strip(chunk.rich_output) ≠ "" && (result *= chunk.rich_output * "\n")
|
||||
strip(chunk.rich_output) ≠ "" && (result *= chunk.rich_output * '\n')
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
# Handle figures
|
||||
|
@ -259,18 +237,13 @@ function format_output(result, docformat::JMarkdown2tex)
|
|||
return result_escaped
|
||||
end
|
||||
|
||||
format_code(result, docformat) = result
|
||||
format_code(code, docformat) = code
|
||||
|
||||
function format_code(result, docformat::JMarkdown2tex)
|
||||
highlighted = highlight(
|
||||
MIME("text/latex"),
|
||||
strip(result),
|
||||
Highlights.Lexers.JuliaLexer,
|
||||
docformat.formatdict[:theme],
|
||||
)
|
||||
docformat.formatdict[:keep_unicode] || return uc2tex(highlighted)
|
||||
return highlighted
|
||||
# return "\\begin{minted}[mathescape, fontsize=\\small, xleftmargin=0.5em]{julia}\n$result\n\\end{minted}\n"
|
||||
# return "\\begin{minted}[mathescape, fontsize=\\small, xleftmargin=0.5em]{julia}\n$result\n\\end{minted}\n"
|
||||
function format_code(code, docformat::JMarkdown2tex)
|
||||
ret = highlight_code(MIME("text/latex"), code, get_highlight_theme(docformat))
|
||||
docformat.formatdict[:keep_unicode] || return uc2tex(ret)
|
||||
return ret
|
||||
end
|
||||
|
||||
# Convert unicode to tex, escape listings if needed
|
||||
|
@ -287,105 +260,58 @@ end
|
|||
|
||||
# Make julia symbols (\bf* etc.) valid latex
|
||||
function texify(s)
|
||||
ts = ""
|
||||
if occursin(r"^\\bf[A-Z]$", s)
|
||||
ts = replace(s, "\\bf" => "\\bm{\\mathrm{") * "}}"
|
||||
return if occursin(r"^\\bf[A-Z]$", s)
|
||||
replace(s, "\\bf" => "\\bm{\\mathrm{") * "}}"
|
||||
elseif startswith(s, "\\bfrak")
|
||||
ts = replace(s, "\\bfrak" => "\\bm{\\mathfrak{") * "}}"
|
||||
replace(s, "\\bfrak" => "\\bm{\\mathfrak{") * "}}"
|
||||
elseif startswith(s, "\\bf")
|
||||
ts = replace(s, "\\bf" => "\\bm{\\") * "}"
|
||||
replace(s, "\\bf" => "\\bm{\\") * "}"
|
||||
elseif startswith(s, "\\frak")
|
||||
ts = replace(s, "\\frak" => "\\mathfrak{") * "}"
|
||||
replace(s, "\\frak" => "\\mathfrak{") * "}"
|
||||
else
|
||||
ts = s
|
||||
s
|
||||
end
|
||||
return ts
|
||||
end
|
||||
|
||||
function format_code(result, docformat::JMarkdown2HTML)
|
||||
return highlight(
|
||||
MIME("text/html"),
|
||||
strip(result),
|
||||
Highlights.Lexers.JuliaLexer,
|
||||
docformat.formatdict[:theme],
|
||||
)
|
||||
end
|
||||
format_code(code, docformat::JMarkdown2HTML) =
|
||||
highlight_code(MIME("text/html"), code, get_highlight_theme(docformat))
|
||||
|
||||
function format_code(result, docformat::Pandoc2HTML)
|
||||
return highlight(
|
||||
MIME("text/html"),
|
||||
strip(result),
|
||||
Highlights.Lexers.JuliaLexer,
|
||||
docformat.formatdict[:theme],
|
||||
)
|
||||
end
|
||||
format_code(code, docformat::Pandoc2HTML) =
|
||||
highlight_code(MIME("text/html"), code, get_highlight_theme(docformat))
|
||||
|
||||
function format_termchunk(chunk, formatdict, docformat)
|
||||
if chunk.options[:echo] && chunk.options[:results] != "hidden"
|
||||
result = "$(formatdict[:termstart])$(chunk.output)\n" * "$(formatdict[:termend])\n"
|
||||
function format_termchunk(chunk, docformat)
|
||||
return if should_render(chunk)
|
||||
fd = docformat.formatdict
|
||||
string(fd[:termstart], chunk.output, '\n', fd[:termend], '\n')
|
||||
else
|
||||
result = ""
|
||||
""
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
function format_termchunk(chunk, formatdict, docformat::JMarkdown2HTML)
|
||||
if chunk.options[:echo] && chunk.options[:results] != "hidden"
|
||||
result = highlight(
|
||||
MIME("text/html"),
|
||||
strip(chunk.output),
|
||||
Highlights.Lexers.JuliaConsoleLexer,
|
||||
docformat.formatdict[:theme],
|
||||
)
|
||||
else
|
||||
result = ""
|
||||
end
|
||||
return result
|
||||
end
|
||||
format_termchunk(chunk, docformat::JMarkdown2HTML) =
|
||||
should_render(chunk) ? highlight_term(MIME("text/html"), chunk.output, get_highlight_theme(docformat)) : ""
|
||||
|
||||
function format_termchunk(chunk, formatdict, docformat::Pandoc2HTML)
|
||||
if chunk.options[:echo] && chunk.options[:results] != "hidden"
|
||||
result = highlight(
|
||||
MIME("text/html"),
|
||||
strip(chunk.output),
|
||||
Highlights.Lexers.JuliaConsoleLexer,
|
||||
docformat.formatdict[:theme],
|
||||
)
|
||||
else
|
||||
result = ""
|
||||
end
|
||||
return result
|
||||
end
|
||||
format_termchunk(chunk, docformat::Pandoc2HTML) =
|
||||
should_render(chunk) ? highlight_term(MIME("text/html"), chunk.output, get_highlight_theme(docformat)) : ""
|
||||
|
||||
function format_termchunk(chunk, formatdict, docformat::JMarkdown2tex)
|
||||
if chunk.options[:echo] && chunk.options[:results] != "hidden"
|
||||
result = highlight(
|
||||
MIME("text/latex"),
|
||||
strip(chunk.output),
|
||||
Highlights.Lexers.JuliaConsoleLexer,
|
||||
docformat.formatdict[:theme],
|
||||
)
|
||||
# return "\\begin{minted}[mathescape, fontsize=\\small, xleftmargin=0.5em]{julia}\n$result\n\\end{minted}\n"
|
||||
else
|
||||
result = ""
|
||||
end
|
||||
return result
|
||||
end
|
||||
# return "\\begin{minted}[mathescape, fontsize=\\small, xleftmargin=0.5em]{julia}\n$result\n\\end{minted}\n"
|
||||
format_termchunk(chunk, docformat::JMarkdown2tex) =
|
||||
should_render(chunk) ? highlight_term(MIME("text/latex"), chunk.output, get_highlight_theme(docformat)) : ""
|
||||
|
||||
function highlight(
|
||||
mime::MIME,
|
||||
output,
|
||||
lexer,
|
||||
theme = Highlights.Themes.DefaultTheme,
|
||||
)
|
||||
return sprint((io, x) -> Highlights.highlight(io, mime, x, lexer, theme), output)
|
||||
end
|
||||
should_render(chunk) = chunk.options[:echo] && chunk.options[:results] ≠ "hidden"
|
||||
|
||||
highlight_code(mime, code, highlight_theme) =
|
||||
highlight(mime, strip(code), Highlights.Lexers.JuliaLexer, highlight_theme)
|
||||
highlight_term(mime, output, highlight_theme) =
|
||||
highlight(mime, strip(output), Highlights.Lexers.JuliaConsoleLexer, highlight_theme)
|
||||
highlight(mime, output, lexer, theme = Highlights.Themes.DefaultTheme) =
|
||||
sprint((io, x) -> Highlights.highlight(io, mime, x, lexer, theme), output)
|
||||
|
||||
indent(text, nindent) = join(map(x -> string(repeat(' ', nindent), x), split(text, '\n')), '\n')
|
||||
|
||||
function wraplines(text, line_width = 75)
|
||||
result = AbstractString[]
|
||||
lines = split(text, "\n")
|
||||
lines = split(text, '\n')
|
||||
for line in lines
|
||||
if length(line) > line_width
|
||||
push!(result, wrapline(line, line_width))
|
||||
|
@ -394,13 +320,13 @@ function wraplines(text, line_width = 75)
|
|||
end
|
||||
end
|
||||
|
||||
return strip(join(result, "\n"))
|
||||
return strip(join(result, '\n'))
|
||||
end
|
||||
|
||||
function wrapline(text, line_width = 75)
|
||||
result = ""
|
||||
while length(text) > line_width
|
||||
result *= first(text, line_width) * "\n"
|
||||
result *= first(text, line_width) * '\n'
|
||||
text = chop(text, head = line_width, tail = 0)
|
||||
end
|
||||
result *= text
|
||||
|
|
|
@ -1,21 +1,10 @@
|
|||
"""
|
||||
pandoc2html(formatted::AbstractString, doc::WeaveDoc)
|
||||
function pandoc2html(rendered, doc, highlight_theme, outname, pandoc_options)
|
||||
template_path = normpath(PKG_DIR, "templates/pandoc_skeleton.html")
|
||||
stylesheet_path = normpath(PKG_DIR, "templates/pandoc_skeleton.css")
|
||||
highlight_stylesheet = get_highlight_stylesheet(MIME("text/html"), highlight_theme)
|
||||
|
||||
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)
|
||||
|
||||
path, wsource = splitdir(abspath(doc.source))
|
||||
wversion, wdate = weave_info()
|
||||
_, weave_source = splitdir(abspath(doc.source))
|
||||
weave_version, weave_date = weave_info()
|
||||
|
||||
# Header is inserted from displayed plots
|
||||
header_script = doc.header_script
|
||||
|
@ -37,13 +26,17 @@ 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
|
||||
-V highlightcss=$highlightcss
|
||||
-V headerscript=$header_script
|
||||
-o $outname`
|
||||
--template $template_path
|
||||
-H $stylesheet_path
|
||||
$self_contained
|
||||
-V highlight_stylesheet=$highlight_stylesheet
|
||||
-V weave_version=$weave_version
|
||||
-V weave_date=$weave_date
|
||||
-V weave_source=$weave_source
|
||||
-V headerscript=$header_script
|
||||
-o $outname`
|
||||
proc = open(cmd, "r+")
|
||||
println(proc.in, formatted)
|
||||
println(proc.in, rendered)
|
||||
close(proc.in)
|
||||
proc_output = read(proc.out, String)
|
||||
catch
|
||||
|
@ -54,21 +47,10 @@ 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(rendered, doc, outname, pandoc_options)
|
||||
weavedir = dirname(@__FILE__)
|
||||
header_template = joinpath(weavedir, "../templates/pandoc_header.txt")
|
||||
|
||||
path, wsource = splitdir(abspath(doc.source))
|
||||
outname = basename(outname)
|
||||
|
||||
# Change path for pandoc
|
||||
|
@ -90,7 +72,7 @@ function pandoc2pdf(
|
|||
--include-in-header=$header_template
|
||||
-V fontsize=12pt -o $outname`
|
||||
proc = open(cmd, "r+")
|
||||
println(proc.in, formatted)
|
||||
println(proc.in, rendered)
|
||||
close(proc.in)
|
||||
proc_output = read(proc.out, String)
|
||||
catch
|
||||
|
|
|
@ -36,10 +36,6 @@ function WeaveDoc(source, informat = nothing)
|
|||
"",
|
||||
"",
|
||||
header,
|
||||
"",
|
||||
"",
|
||||
Highlights.Themes.DefaultTheme,
|
||||
"",
|
||||
chunk_defaults,
|
||||
)
|
||||
end
|
||||
|
|
|
@ -58,7 +58,6 @@ function run_doc(
|
|||
# This is needed for latex and should work on all output formats
|
||||
@static Sys.iswindows() && (fig_path = replace(fig_path, "\\" => "/"))
|
||||
|
||||
doc.fig_path = fig_path
|
||||
set_rc_params(doc, fig_path, fig_ext)
|
||||
|
||||
# New sandbox for each document with args exposed
|
||||
|
|
|
@ -12,10 +12,6 @@ mutable struct WeaveDoc
|
|||
doctype::String
|
||||
header_script::String
|
||||
header::Dict
|
||||
template::Union{AbstractString,Mustache.MustacheTokens}
|
||||
css::AbstractString
|
||||
highlight_theme::Type{<:Highlights.AbstractTheme}
|
||||
fig_path::AbstractString
|
||||
chunk_defaults::Dict{Symbol,Any}
|
||||
end
|
||||
|
||||
|
|
|
@ -16,10 +16,10 @@
|
|||
<script type="text/javascript" async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
|
||||
</script>
|
||||
|
||||
{{{ :highlightcss }}}
|
||||
{{{ :highlight_stylesheet }}}
|
||||
|
||||
<style type="text/css">
|
||||
{{{ :themecss }}}
|
||||
{{{ :stylesheet }}}
|
||||
</style>
|
||||
</HEAD>
|
||||
|
||||
|
@ -38,8 +38,8 @@
|
|||
<HR/>
|
||||
<div class="footer">
|
||||
<p>
|
||||
Published from <a href="{{{:source}}}">{{{:source}}}</a>
|
||||
using <a href="http://github.com/JunoLab/Weave.jl">Weave.jl</a> {{:wversion}} on {{:wdate}}.
|
||||
Published from <a href="{{{:weave_source}}}">{{{:weave_source}}}</a>
|
||||
using <a href="http://github.com/JunoLab/Weave.jl">Weave.jl</a> {{:weave_version}} on {{:weave_date}}.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -32,7 +32,7 @@ $for(header-includes)$
|
|||
$header-includes$
|
||||
$endfor$
|
||||
|
||||
$highlightcss$
|
||||
$highlight_stylesheet$
|
||||
|
||||
$if(highlighting-css)$
|
||||
<style type="text/css">
|
||||
|
@ -85,8 +85,8 @@ $endfor$
|
|||
|
||||
<HR/>
|
||||
<div class="footer"><p>
|
||||
Published from <a href="$wsource$">$wsource$</a> using
|
||||
<a href="http://github.com/mpastell/Weave.jl">Weave.jl</a> $wversion$ on $wdate$.
|
||||
Published from <a href="$source$">$source$</a> using
|
||||
<a href="http://github.com/mpastell/Weave.jl">Weave.jl</a> $weave_version$ on $weave_date$.
|
||||
<p></div>
|
||||
|
||||
</div>
|
||||
|
|
|
@ -19,4 +19,3 @@ Testing output
|
|||
|
||||
|
||||
</div>
|
||||
|
||||
|
|
|
@ -19,4 +19,3 @@ Testing output
|
|||
|
||||
|
||||
\end{frame}
|
||||
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
|
||||
|
||||
<pre class='hljl'>
|
||||
<span class='hljl-k'>import</span><span class='hljl-t'> </span><span class='hljl-n'>Base</span><span class='hljl-t'>
|
||||
</span><span class='hljl-k'>function</span><span class='hljl-t'> </span><span class='hljl-n'>Base</span><span class='hljl-oB'>.</span><span class='hljl-nf'>show</span><span class='hljl-p'>(</span><span class='hljl-n'>io</span><span class='hljl-oB'>::</span><span class='hljl-n'>IO</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-n'>m</span><span class='hljl-oB'>::</span><span class='hljl-so'>MIME"text/html"</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-n'>x</span><span class='hljl-oB'>::</span><span class='hljl-n'>Array</span><span class='hljl-p'>)</span><span class='hljl-t'>
|
||||
|
@ -81,4 +80,3 @@
|
|||
|
||||
<div class="markdown"><p><strong>Some Markdown</strong></p>
|
||||
</div>
|
||||
|
||||
|
|
|
@ -1,3 +1,7 @@
|
|||
# TODO: this test is horrible, refactor
|
||||
|
||||
using Weave: Highlights.Themes.DefaultTheme
|
||||
|
||||
# Test rendering of doc chunks
|
||||
content = """
|
||||
# Test chunk
|
||||
|
@ -8,12 +12,12 @@ Test rendering \$\alpha\$
|
|||
dchunk = Weave.DocChunk(content, 1, 1)
|
||||
|
||||
pformat = Weave.formats["github"]
|
||||
f = Weave.format_chunk(dchunk, pformat.formatdict, pformat)
|
||||
f = Weave.format_chunk(dchunk, pformat)
|
||||
@test f == content
|
||||
|
||||
docformat = Weave.formats["md2html"]
|
||||
f_check = "<h1>Test chunk</h1>\n<p>Test rendering <span class=\"math\">\$\alpha\$</span></p>\n"
|
||||
f = Weave.format_chunk(dchunk, docformat.formatdict, docformat)
|
||||
f = Weave.format_chunk(dchunk, docformat)
|
||||
@test f_check == f
|
||||
|
||||
# Test with actual doc
|
||||
|
@ -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[:highlight_theme] = DefaultTheme
|
||||
c = Weave.format_code(doc.chunks[3].content, doc.format)
|
||||
@test c_check == c
|
||||
|
||||
|
@ -30,16 +34,12 @@ o_check = "\nprintln(x)\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[:highlight_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)
|
||||
|
@ -93,13 +89,12 @@ content = """
|
|||
"""
|
||||
chunk = Weave.DocChunk(content, 1, 1)
|
||||
fmt = deepcopy(Weave.formats["md2tex"])
|
||||
fmtdict = fmt.formatdict
|
||||
|
||||
f = Weave.format_chunk(chunk, fmtdict, fmt)
|
||||
f = Weave.format_chunk(chunk, fmt)
|
||||
@test f == "\\section{Test chunk}\n\\ensuremath{\\alpha}\n\n"
|
||||
|
||||
fmtdict[:keep_unicode] = true
|
||||
f = Weave.format_chunk(chunk, fmtdict, fmt)
|
||||
fmt.formatdict[:keep_unicode] = true
|
||||
f = Weave.format_chunk(chunk, fmt)
|
||||
@test f == "\\section{Test chunk}\nα\n\n"
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue