Merge pull request #341 from JunoLab/avi/preprefactor

pre #338
pull/344/head
Shuhei Kadowaki 2020-05-24 03:14:11 +09:00 committed by GitHub
commit 60fc450c88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 193 additions and 323 deletions

View File

@ -22,6 +22,7 @@ end
isnothing(::Any) = false isnothing(::Any) = false
isnothing(::Nothing) = true isnothing(::Nothing) = true
end end
take2string!(io) = String(take!(io))
""" """
list_out_formats() list_out_formats()
@ -116,7 +117,7 @@ function weave(
cache::Symbol = :off, cache::Symbol = :off,
throw_errors::Bool = false, throw_errors::Bool = false,
template::Union{Nothing,AbstractString,Mustache.MustacheTokens} = nothing, 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, highlight_theme::Union{Nothing,Type{<:Highlights.AbstractTheme}} = nothing,
pandoc_options::Vector{<:AbstractString} = String[], pandoc_options::Vector{<:AbstractString} = String[],
latex_cmd::AbstractString = "xelatex", latex_cmd::AbstractString = "xelatex",
@ -192,28 +193,24 @@ function weave(
keep_unicode = get(weave_options, "keep_unicode", keep_unicode) keep_unicode = get(weave_options, "keep_unicode", keep_unicode)
end 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) 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) 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 # document generation via external programs
doctype = doc.doctype doctype = doc.doctype
if doctype == "pandoc2html" if doctype == "pandoc2html"
mdname = outname mdname = outname
outname = get_outname(out_path, doc, ext = "html") outname = get_outname(out_path, doc, ext = "html")
pandoc2html(formatted, doc, outname, pandoc_options) pandoc2html(rendered, doc, highlight_theme, outname, pandoc_options)
rm(mdname) rm(mdname)
elseif doctype == "pandoc2pdf" elseif doctype == "pandoc2pdf"
mdname = outname mdname = outname
outname = get_outname(out_path, doc, ext = "pdf") outname = get_outname(out_path, doc, ext = "pdf")
pandoc2pdf(formatted, doc, outname, pandoc_options) pandoc2pdf(rendered, doc, outname, pandoc_options)
rm(mdname) rm(mdname)
elseif doctype == "md2pdf" elseif doctype == "md2pdf"
run_latex(doc, outname, latex_cmd) run_latex(doc, outname, latex_cmd)

View File

@ -1,5 +1,5 @@
# module Markdown2HTML
# Markdown to HTML writer, Modified from Julia Base.Markdown html writer # Markdown to HTML writer, Modified from Julia Base.Markdown html writer
using Markdown: using Markdown:
MD, MD,
Header, Header,
@ -19,13 +19,10 @@ using Markdown:
LaTeX, LaTeX,
isordered isordered
function tohtml(io::IO, m::MIME"text/html", x)
show(io, m, x)
end
function tohtml(io::IO, m::MIME"text/plain", x) tohtml(io::IO, m::MIME"text/html", x) = show(io, m, x)
htmlesc(io, sprint(show, m, x))
end tohtml(io::IO, m::MIME"text/plain", x) = htmlesc(io, sprint(show, m, x))
function tohtml(io::IO, m::MIME"image/png", img) function tohtml(io::IO, m::MIME"image/png", img)
print(io, """<img src="data:image/png;base64,""") print(io, """<img src="data:image/png;base64,""")
@ -33,9 +30,7 @@ function tohtml(io::IO, m::MIME"image/png", img)
print(io, "\" />") print(io, "\" />")
end end
function tohtml(m::MIME"image/svg+xml", img) tohtml(m::MIME"image/svg+xml", img) = show(io, m, img)
show(io, m, img)
end
# AbstractDisplay infrastructure # AbstractDisplay infrastructure
@ -168,9 +163,7 @@ function html(io::IO, md::List)
end end
end end
function html(io::IO, md::HorizontalRule) html(io::IO, md::HorizontalRule) = tag(io, :hr)
tag(io, :hr)
end
function html(io::IO, tex::LaTeX) function html(io::IO, tex::LaTeX)
withtag(io, :p, :class => "math") do withtag(io, :p, :class => "math") do
@ -178,9 +171,7 @@ function html(io::IO, tex::LaTeX)
end end
end end
function html(io::IO, comment::Comment) html(io::IO, comment::Comment) = write(io, "\n<!-- $(comment.text) -->\n")
write(io, "\n<!-- $(comment.text) -->\n")
end
function html(io::IO, md::Table) function html(io::IO, md::Table)
withtag(io, :table) do withtag(io, :table) do
@ -218,9 +209,7 @@ function htmlinline(io::IO, tex::LaTeX)
end end
end end
function htmlinline(io::IO, md::Union{Symbol,AbstractString}) htmlinline(io::IO, md::Union{Symbol,AbstractString}) = htmlesc(io, md)
htmlesc(io, md)
end
function htmlinline(io::IO, md::Bold) function htmlinline(io::IO, md::Bold)
withtag(io, :strong) do withtag(io, :strong) do
@ -234,9 +223,7 @@ function htmlinline(io::IO, md::Italic)
end end
end end
function htmlinline(io::IO, md::Image) htmlinline(io::IO, md::Image) = tag(io, :img, :src => md.url, :alt => md.alt)
tag(io, :img, :src => md.url, :alt => md.alt)
end
function htmlinline(io::IO, f::Footnote) function htmlinline(io::IO, f::Footnote)
withtag(io, :a, :href => "#footnote-$(f.id)", :class => "footnote") do withtag(io, :a, :href => "#footnote-$(f.id)", :class => "footnote") do
@ -250,18 +237,12 @@ function htmlinline(io::IO, link::Link)
end end
end end
function htmlinline(io::IO, br::LineBreak) htmlinline(io::IO, br::LineBreak) = tag(io, :br)
tag(io, :br)
end
function htmlinline(io::IO, comment::Comment) htmlinline(io::IO, comment::Comment) = write(io, "<!-- $(comment.text) -->")
write(io, "<!-- $(comment.text) -->")
end
htmlinline(io::IO, x) = tohtml(io, x) htmlinline(io::IO, x) = tohtml(io, x)
# API # API
html(md) = sprint(html, md) html(md) = sprint(html, md)
# end

View File

@ -1,12 +1,9 @@
import Markdown: latex, latexinline import Markdown: latex, latexinline
# Remove comments that can occur inside a line function latex(io::IO, comment::Comment)
function latexinline(io, comment::WeaveMarkdown.Comment)
write(io, "")
end
function latex(io::IO, comment::WeaveMarkdown.Comment)
for line in split(comment.text, r"\r\n|\n") for line in split(comment.text, r"\r\n|\n")
write(io, "% $line\n") write(io, "% $line\n")
end end
end end
latexinline(io, comment::Comment) = write(io, "")

View File

@ -1,8 +1,11 @@
# This module extends the julia markdown parser to improve compatibility with Jupyter, Pandoc etc. # This module extends the julia markdown parser to improve compatibility with Jupyter, Pandoc etc.
module WeaveMarkdown module WeaveMarkdown
using ..Weave: isnothing, take2string!
using Markdown using Markdown
import Markdown: @trigger, @breaking, Code, MD, withstream, startswith, LaTeX import Markdown: @trigger, @breaking, Code, MD, withstream, startswith, LaTeX
function __init__() function __init__()
# NOTE: # NOTE:
# overwriting `Markdown.latex` function should be done here in order to allow # overwriting `Markdown.latex` function should be done here in order to allow
@ -37,7 +40,7 @@ end
if !isempty(estr) if !isempty(estr)
estr = Markdown.startswith(stream, r"^\$\$$"m) estr = Markdown.startswith(stream, r"^\$\$$"m)
if isempty(estr) if isempty(estr)
push!(block, LaTeX(String(take!(buffer)) |> chomp)) push!(block, LaTeX(take2string!(buffer) |> chomp))
end end
return true return true
else else
@ -58,7 +61,7 @@ end
line = readline(stream, keep = true) line = readline(stream, keep = true)
write(buffer, line) write(buffer, line)
if occursin(r"-->$", line) if occursin(r"-->$", line)
s = replace(String(take!(buffer)) |> chomp, r"-->$" => "") s = replace(take2string!(buffer) |> chomp, r"-->$" => "")
push!(block, Comment(s)) push!(block, Comment(s))
return true return true
end end
@ -71,7 +74,7 @@ end
withstream(stream) do withstream(stream) do
Markdown.startswith(stream, "<!--") || return Markdown.startswith(stream, "<!--") || return
text = Markdown.readuntil(stream, "-->") text = Markdown.readuntil(stream, "-->")
text nothing && return isnothing(text) && return
return Comment(text) return Comment(text)
end end
end end
@ -88,6 +91,8 @@ for key in keys(Markdown.julia.inner)
end end
end end
include("html.jl") include("html.jl")
include("latex.jl") include("latex.jl")
end
end # module

View File

@ -111,7 +111,7 @@ end
function Base.display(report::Report, m::MIME"text/html", data) function Base.display(report::Report, m::MIME"text/html", data)
io = IOBuffer() io = IOBuffer()
show(IOContext(io, :limit => true), m, data) show(IOContext(io, :limit => true), m, data)
report.rich_output *= "\n" * String(take!(io)) report.rich_output *= string('\n', take2string!(io))
end end
# Catch "rich_output" # Catch "rich_output"
@ -131,7 +131,7 @@ end
function Base.display(report::Report, m::MIME"text/latex", data) function Base.display(report::Report, m::MIME"text/latex", data)
s = repr(m, data) s = repr(m, data)
report.rich_output *= "\n" * s report.rich_output *= string('\n', s)
end end
"""Add saved figure name to results and return the name""" """Add saved figure name to results and return the name"""

View File

@ -1,86 +1,72 @@
# TODO: reorganize this file into multiple files corresponding to each output format
using Mustache, Highlights, .WeaveMarkdown, Markdown, Dates, Pkg using Mustache, Highlights, .WeaveMarkdown, Markdown, Dates, Pkg
using REPL.REPLCompletions: latex_symbols using REPL.REPLCompletions: latex_symbols
function format(doc) function format(doc, template = nothing, highlight_theme = nothing; css = nothing)
format = doc.format docformat = doc.format
# Complete format dictionaries with defaults # Complete format dictionaries with defaults
formatdict = format.formatdict get!(docformat.formatdict, :termstart, docformat.formatdict[:codestart])
get!(formatdict, :termstart, formatdict[:codestart]) get!(docformat.formatdict, :termend, docformat.formatdict[:codeend])
get!(formatdict, :termend, formatdict[:codeend]) get!(docformat.formatdict, :out_width, nothing)
get!(formatdict, :out_width, nothing) get!(docformat.formatdict, :out_height, nothing)
get!(formatdict, :out_height, nothing) get!(docformat.formatdict, :fig_pos, nothing)
get!(formatdict, :fig_pos, nothing) get!(docformat.formatdict, :fig_env, nothing)
get!(formatdict, :fig_env, nothing) get_highlight_theme(docformat) = highlight_theme = get_highlight_theme(highlight_theme)
formatdict[:cwd] = doc.cwd # pass wd to figure formatters
formatdict[:theme] = doc.highlight_theme
restore_header!(doc) restore_header!(doc)
formatted = String[] lines = map(copy(doc.chunks)) do chunk
for chunk in copy(doc.chunks) format_chunk(chunk, docformat)
result = format_chunk(chunk, formatdict, format)
push!(formatted, result)
end end
formatted = join(formatted, "\n") body = join(lines, '\n')
# Render using a template if needed return format isa JMarkdown2HTML ? render2html(body, doc, template, css, highlight_theme) :
return render_doc(formatted, doc) format isa JMarkdown2tex ? render2tex(body, doc, template, highlight_theme) :
body
end end
render_doc(formatted, doc) = render_doc(formatted, doc, doc.format) function render2html(body, doc, template, css, highlight_theme)
_, weave_source = splitdir(abspath(doc.source))
render_doc(formatted, doc, format) = formatted weave_version, weave_date = weave_info()
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()
return Mustache.render( return Mustache.render(
template; get_template(template, false);
body = formatted, body = body,
themecss = themecss, stylesheet = get_stylesheet(css),
highlightcss = highlightcss, highlight_stylesheet = get_highlight_stylesheet(MIME("text/html"), highlight_theme),
header_script = doc.header_script, header_script = doc.header_script,
source = source, weave_source = weave_source,
wversion = wversion, weave_version = weave_version,
wdate = wdate, weave_date = weave_date,
[Pair(Symbol(k), v) for (k, v) in doc.header]..., [Pair(Symbol(k), v) for (k, v) in doc.header]...,
) )
end end
function render_doc(formatted, doc, format::JMarkdown2tex) function render2tex(body, doc, template, highlight_theme)
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)
return Mustache.render( return Mustache.render(
template; get_template(template, true);
body = formatted, body = body,
highlight = highlight, highlight = get_highlight_stylesheet(MIME("text/latex"), highlight_theme),
[Pair(Symbol(k), v) for (k, v) in doc.header]..., [Pair(Symbol(k), v) for (k, v) in doc.header]...,
) )
end 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 const WEAVE_VERSION = try
'v' * Pkg.TOML.parsefile(normpath(PKG_DIR, "Project.toml"))["version"] '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)) pushfirst!(doc.chunks, DocChunk(header_text, 0, 0))
end 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 format_inline(inline::InlineText) = inline.content
@ -119,106 +105,99 @@ function format_inline(inline::InlineCode)
return inline.output return inline.output
end end
function ioformat!(io::IOBuffer, out::IOBuffer, fun = WeaveMarkdown.latex) function format_chunk(chunk::DocChunk, docformat::JMarkdown2tex)
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)
out = IOBuffer() out = IOBuffer()
io = IOBuffer() io = IOBuffer()
for inline in chunk.content for inline in chunk.content
if isa(inline, InlineText) if isa(inline, InlineText)
write(io, inline.content) write(io, inline.content)
elseif !isempty(inline.rich_output) elseif !isempty(inline.rich_output)
ioformat!(io, out) clear_buffer_and_format!(io, out, WeaveMarkdown.latex)
write(out, addspace(inline.rich_output, inline)) write(out, addlines(inline.rich_output, inline))
elseif !isempty(inline.figures) elseif !isempty(inline.figures)
write(io, inline.figures[end], inline) write(io, inline.figures[end], inline)
elseif !isempty(inline.output) elseif !isempty(inline.output)
write(io, addspace(inline.output, inline)) write(io, addlines(inline.output, inline))
end end
end end
ioformat!(io, out) clear_buffer_and_format!(io, out, WeaveMarkdown.latex)
formatdict[:keep_unicode] || return uc2tex(String(take!(out))) out = take2string!(out)
return String(take!(out)) return docformat.formatdict[:keep_unicode] ? out : uc2tex(out)
end end
function format_chunk(chunk::DocChunk, formatdict, docformat::JMarkdown2HTML) function format_chunk(chunk::DocChunk, docformat::JMarkdown2HTML)
out = IOBuffer() out = IOBuffer()
io = IOBuffer() io = IOBuffer()
fun = WeaveMarkdown.html
for inline in chunk.content for inline in chunk.content
if isa(inline, InlineText) if isa(inline, InlineText)
write(io, inline.content) write(io, inline.content)
elseif !isempty(inline.rich_output) elseif !isempty(inline.rich_output)
ioformat!(io, out, fun) clear_buffer_and_format!(io, out, WeaveMarkdown.html)
write(out, addspace(inline.rich_output, inline)) write(out, addlines(inline.rich_output, inline))
elseif !isempty(inline.figures) elseif !isempty(inline.figures)
write(io, inline.figures[end]) write(io, inline.figures[end])
elseif !isempty(inline.output) elseif !isempty(inline.output)
write(io, addspace(inline.output, inline)) write(io, addlines(inline.output, inline))
end end
end end
ioformat!(io, out, fun) clear_buffer_and_format!(io, out, WeaveMarkdown.html)
return String(take!(out)) return take2string!(out)
end 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 # Fill undefined options with format specific defaults
chunk.options[:out_width] == nothing && isnothing(chunk.options[:out_width]) && (chunk.options[:out_width] = formatdict[:out_width])
(chunk.options[:out_width] = formatdict[:out_width]) isnothing(chunk.options[:fig_pos]) && (chunk.options[:fig_pos] = formatdict[:fig_pos])
chunk.options[:fig_pos] == nothing && (chunk.options[:fig_pos] = formatdict[:fig_pos])
# Only use floats if chunk has caption or sets fig_env # 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]) (chunk.options[:fig_env] = formatdict[:fig_env])
end end
if haskey(formatdict, :indent) haskey(formatdict, :indent) && (chunk.content = indent(chunk.content, formatdict[:indent]))
chunk.content = indent(chunk.content, formatdict[:indent])
end
chunk.content = format_code(chunk.content, docformat) chunk.content = format_code(chunk.content, docformat)
if !chunk.options[:eval] if !chunk.options[:eval]
if chunk.options[:echo] return if chunk.options[:echo]
result = "$(formatdict[:codestart])\n$(chunk.content)$(formatdict[:codeend])" string(formatdict[:codestart], '\n', chunk.content, formatdict[:codeend])
return result
else else
r = "" ""
return r
end end
end end
if chunk.options[:term] if chunk.options[:term]
result = format_termchunk(chunk, formatdict, docformat) result = format_termchunk(chunk, docformat)
else else
result = if chunk.options[:echo]
if chunk.options[:echo]
# Convert to output format and highlight (html, tex...) if needed # 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 else
result = "" ""
end end
if (strip(chunk.output) != "" || strip(chunk.rich_output) != "") && if (strip(chunk.output) "" || strip(chunk.rich_output) "") &&
(chunk.options[:results] != "hidden") (chunk.options[:results] "hidden")
if chunk.options[:results] != "markup" && chunk.options[:results] != "hold" if chunk.options[:results] "markup" && chunk.options[:results] "hold"
strip(chunk.output) "" && (result *= "$(chunk.output)\n") strip(chunk.output) "" && (result *= "$(chunk.output)\n")
strip(chunk.rich_output) "" && (result *= "$(chunk.rich_output)\n") strip(chunk.rich_output) "" && (result *= "$(chunk.rich_output)\n")
else else
if chunk.options[:wrap] if chunk.options[:wrap]
chunk.output = 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) chunk.output = format_output(chunk.output, docformat)
else else
chunk.output = "\n" * rstrip(chunk.output) chunk.output = '\n' * rstrip(chunk.output)
chunk.output = format_output(chunk.output, docformat) chunk.output = format_output(chunk.output, docformat)
end end
@ -228,10 +207,9 @@ function format_chunk(chunk::CodeChunk, formatdict, docformat)
strip(chunk.output) "" && ( strip(chunk.output) "" && (
result *= "$(formatdict[:outputstart])$(chunk.output)\n$(formatdict[:outputend])\n" 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 end
end end
# Handle figures # Handle figures
@ -259,18 +237,13 @@ function format_output(result, docformat::JMarkdown2tex)
return result_escaped return result_escaped
end end
format_code(result, docformat) = result format_code(code, docformat) = code
function format_code(result, docformat::JMarkdown2tex) # return "\\begin{minted}[mathescape, fontsize=\\small, xleftmargin=0.5em]{julia}\n$result\n\\end{minted}\n"
highlighted = highlight( function format_code(code, docformat::JMarkdown2tex)
MIME("text/latex"), ret = highlight_code(MIME("text/latex"), code, get_highlight_theme(docformat))
strip(result), docformat.formatdict[:keep_unicode] || return uc2tex(ret)
Highlights.Lexers.JuliaLexer, return ret
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"
end end
# Convert unicode to tex, escape listings if needed # Convert unicode to tex, escape listings if needed
@ -287,105 +260,58 @@ end
# Make julia symbols (\bf* etc.) valid latex # Make julia symbols (\bf* etc.) valid latex
function texify(s) function texify(s)
ts = "" return if occursin(r"^\\bf[A-Z]$", s)
if occursin(r"^\\bf[A-Z]$", s) replace(s, "\\bf" => "\\bm{\\mathrm{") * "}}"
ts = replace(s, "\\bf" => "\\bm{\\mathrm{") * "}}"
elseif startswith(s, "\\bfrak") elseif startswith(s, "\\bfrak")
ts = replace(s, "\\bfrak" => "\\bm{\\mathfrak{") * "}}" replace(s, "\\bfrak" => "\\bm{\\mathfrak{") * "}}"
elseif startswith(s, "\\bf") elseif startswith(s, "\\bf")
ts = replace(s, "\\bf" => "\\bm{\\") * "}" replace(s, "\\bf" => "\\bm{\\") * "}"
elseif startswith(s, "\\frak") elseif startswith(s, "\\frak")
ts = replace(s, "\\frak" => "\\mathfrak{") * "}" replace(s, "\\frak" => "\\mathfrak{") * "}"
else else
ts = s s
end end
return ts
end end
function format_code(result, docformat::JMarkdown2HTML) format_code(code, docformat::JMarkdown2HTML) =
return highlight( highlight_code(MIME("text/html"), code, get_highlight_theme(docformat))
MIME("text/html"),
strip(result),
Highlights.Lexers.JuliaLexer,
docformat.formatdict[:theme],
)
end
function format_code(result, docformat::Pandoc2HTML) format_code(code, docformat::Pandoc2HTML) =
return highlight( highlight_code(MIME("text/html"), code, get_highlight_theme(docformat))
MIME("text/html"),
strip(result),
Highlights.Lexers.JuliaLexer,
docformat.formatdict[:theme],
)
end
function format_termchunk(chunk, formatdict, docformat) function format_termchunk(chunk, docformat)
if chunk.options[:echo] && chunk.options[:results] != "hidden" return if should_render(chunk)
result = "$(formatdict[:termstart])$(chunk.output)\n" * "$(formatdict[:termend])\n" fd = docformat.formatdict
string(fd[:termstart], chunk.output, '\n', fd[:termend], '\n')
else else
result = "" ""
end end
return result
end end
function format_termchunk(chunk, formatdict, docformat::JMarkdown2HTML) format_termchunk(chunk, docformat::JMarkdown2HTML) =
if chunk.options[:echo] && chunk.options[:results] != "hidden" should_render(chunk) ? highlight_term(MIME("text/html"), chunk.output, get_highlight_theme(docformat)) : ""
result = highlight(
MIME("text/html"),
strip(chunk.output),
Highlights.Lexers.JuliaConsoleLexer,
docformat.formatdict[:theme],
)
else
result = ""
end
return result
end
function format_termchunk(chunk, formatdict, docformat::Pandoc2HTML) format_termchunk(chunk, docformat::Pandoc2HTML) =
if chunk.options[:echo] && chunk.options[:results] != "hidden" should_render(chunk) ? highlight_term(MIME("text/html"), chunk.output, get_highlight_theme(docformat)) : ""
result = highlight(
MIME("text/html"),
strip(chunk.output),
Highlights.Lexers.JuliaConsoleLexer,
docformat.formatdict[:theme],
)
else
result = ""
end
return result
end
function format_termchunk(chunk, formatdict, docformat::JMarkdown2tex) # return "\\begin{minted}[mathescape, fontsize=\\small, xleftmargin=0.5em]{julia}\n$result\n\\end{minted}\n"
if chunk.options[:echo] && chunk.options[:results] != "hidden" format_termchunk(chunk, docformat::JMarkdown2tex) =
result = highlight( should_render(chunk) ? highlight_term(MIME("text/latex"), chunk.output, get_highlight_theme(docformat)) : ""
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
function highlight( should_render(chunk) = chunk.options[:echo] && chunk.options[:results] "hidden"
mime::MIME,
output, highlight_code(mime, code, highlight_theme) =
lexer, highlight(mime, strip(code), Highlights.Lexers.JuliaLexer, highlight_theme)
theme = Highlights.Themes.DefaultTheme, highlight_term(mime, output, highlight_theme) =
) highlight(mime, strip(output), Highlights.Lexers.JuliaConsoleLexer, highlight_theme)
return sprint((io, x) -> Highlights.highlight(io, mime, x, lexer, theme), output) highlight(mime, output, lexer, theme = Highlights.Themes.DefaultTheme) =
end 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') indent(text, nindent) = join(map(x -> string(repeat(' ', nindent), x), split(text, '\n')), '\n')
function wraplines(text, line_width = 75) function wraplines(text, line_width = 75)
result = AbstractString[] result = AbstractString[]
lines = split(text, "\n") lines = split(text, '\n')
for line in lines for line in lines
if length(line) > line_width if length(line) > line_width
push!(result, wrapline(line, line_width)) push!(result, wrapline(line, line_width))
@ -394,13 +320,13 @@ function wraplines(text, line_width = 75)
end end
end end
return strip(join(result, "\n")) return strip(join(result, '\n'))
end end
function wrapline(text, line_width = 75) function wrapline(text, line_width = 75)
result = "" result = ""
while length(text) > line_width 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) text = chop(text, head = line_width, tail = 0)
end end
result *= text result *= text

View File

@ -1,21 +1,10 @@
""" function pandoc2html(rendered, doc, highlight_theme, outname, pandoc_options)
pandoc2html(formatted::AbstractString, doc::WeaveDoc) 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 _, weave_source = splitdir(abspath(doc.source))
""" weave_version, weave_date = weave_info()
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()
# Header is inserted from displayed plots # Header is inserted from displayed plots
header_script = doc.header_script header_script = doc.header_script
@ -37,13 +26,17 @@ function pandoc2html(
try try
cmd = `pandoc -f markdown+raw_html -s --mathjax="" cmd = `pandoc -f markdown+raw_html -s --mathjax=""
$filt $citeproc $pandoc_options $filt $citeproc $pandoc_options
--template $html_template -H $css_template $self_contained --template $template_path
-V wversion=$wversion -V wdate=$wdate -V wsource=$wsource -H $stylesheet_path
-V highlightcss=$highlightcss $self_contained
-V headerscript=$header_script -V highlight_stylesheet=$highlight_stylesheet
-o $outname` -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+") proc = open(cmd, "r+")
println(proc.in, formatted) println(proc.in, rendered)
close(proc.in) close(proc.in)
proc_output = read(proc.out, String) proc_output = read(proc.out, String)
catch catch
@ -54,21 +47,10 @@ function pandoc2html(
end end
end end
""" function pandoc2pdf(rendered, doc, outname, pandoc_options)
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,
)
weavedir = dirname(@__FILE__) weavedir = dirname(@__FILE__)
header_template = joinpath(weavedir, "../templates/pandoc_header.txt") header_template = joinpath(weavedir, "../templates/pandoc_header.txt")
path, wsource = splitdir(abspath(doc.source))
outname = basename(outname) outname = basename(outname)
# Change path for pandoc # Change path for pandoc
@ -90,7 +72,7 @@ function pandoc2pdf(
--include-in-header=$header_template --include-in-header=$header_template
-V fontsize=12pt -o $outname` -V fontsize=12pt -o $outname`
proc = open(cmd, "r+") proc = open(cmd, "r+")
println(proc.in, formatted) println(proc.in, rendered)
close(proc.in) close(proc.in)
proc_output = read(proc.out, String) proc_output = read(proc.out, String)
catch catch

View File

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

View File

@ -58,7 +58,6 @@ function run_doc(
# This is needed for latex and should work on all output formats # This is needed for latex and should work on all output formats
@static Sys.iswindows() && (fig_path = replace(fig_path, "\\" => "/")) @static Sys.iswindows() && (fig_path = replace(fig_path, "\\" => "/"))
doc.fig_path = fig_path
set_rc_params(doc, fig_path, fig_ext) set_rc_params(doc, fig_path, fig_ext)
# New sandbox for each document with args exposed # New sandbox for each document with args exposed

View File

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

View File

@ -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 type="text/javascript" async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script> </script>
{{{ :highlightcss }}} {{{ :highlight_stylesheet }}}
<style type="text/css"> <style type="text/css">
{{{ :themecss }}} {{{ :stylesheet }}}
</style> </style>
</HEAD> </HEAD>
@ -38,8 +38,8 @@
<HR/> <HR/>
<div class="footer"> <div class="footer">
<p> <p>
Published from <a href="{{{:source}}}">{{{:source}}}</a> Published from <a href="{{{:weave_source}}}">{{{:weave_source}}}</a>
using <a href="http://github.com/JunoLab/Weave.jl">Weave.jl</a> {{:wversion}} on {{:wdate}}. using <a href="http://github.com/JunoLab/Weave.jl">Weave.jl</a> {{:weave_version}} on {{:weave_date}}.
</p> </p>
</div> </div>
</div> </div>

View File

@ -32,7 +32,7 @@ $for(header-includes)$
$header-includes$ $header-includes$
$endfor$ $endfor$
$highlightcss$ $highlight_stylesheet$
$if(highlighting-css)$ $if(highlighting-css)$
<style type="text/css"> <style type="text/css">
@ -85,8 +85,8 @@ $endfor$
<HR/> <HR/>
<div class="footer"><p> <div class="footer"><p>
Published from <a href="$wsource$">$wsource$</a> using Published from <a href="$source$">$source$</a> using
<a href="http://github.com/mpastell/Weave.jl">Weave.jl</a> $wversion$ on $wdate$. <a href="http://github.com/mpastell/Weave.jl">Weave.jl</a> $weave_version$ on $weave_date$.
<p></div> <p></div>
</div> </div>

View File

@ -19,4 +19,3 @@ Testing output
</div> </div>

View File

@ -19,4 +19,3 @@ Testing output
\end{frame} \end{frame}

View File

@ -1,5 +1,4 @@
<pre class='hljl'> <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 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&quot;text/html&quot;</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'> </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&quot;text/html&quot;</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 class="markdown"><p><strong>Some Markdown</strong></p>
</div> </div>

View File

@ -1,3 +1,7 @@
# TODO: this test is horrible, refactor
using Weave: Highlights.Themes.DefaultTheme
# Test rendering of doc chunks # Test rendering of doc chunks
content = """ content = """
# Test chunk # Test chunk
@ -8,12 +12,12 @@ Test rendering \$\alpha\$
dchunk = Weave.DocChunk(content, 1, 1) dchunk = Weave.DocChunk(content, 1, 1)
pformat = Weave.formats["github"] pformat = Weave.formats["github"]
f = Weave.format_chunk(dchunk, pformat.formatdict, pformat) f = Weave.format_chunk(dchunk, pformat)
@test f == content @test f == content
docformat = Weave.formats["md2html"] docformat = Weave.formats["md2html"]
f_check = "<h1>Test chunk</h1>\n<p>Test rendering <span class=\"math\">\$\alpha\$</span></p>\n" 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 f_check == f
# Test with actual doc # Test with actual doc
@ -22,7 +26,7 @@ parsed = Weave.WeaveDoc("documents/chunk_options.noweb")
doc = run_doc(parsed, doctype = "md2html") 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" 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) c = Weave.format_code(doc.chunks[3].content, doc.format)
@test c_check == c @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) o = Weave.format_output(doc.chunks[4].content, doc.format)
@test o_check == o @test o_check == o
doc.template = "templates/mini.tpl"
rendered = Weave.render_doc("Hello", doc)
@test rendered == "\nHello\n"
# Tex format # Tex format
parsed = Weave.WeaveDoc("documents/chunk_options.noweb") parsed = Weave.WeaveDoc("documents/chunk_options.noweb")
doc = run_doc(parsed, doctype = "md2tex") doc = run_doc(parsed, doctype = "md2tex")
c_check = "\\begin{lstlisting}\n(*@\\HLJLnf{println}@*)(*@\\HLJLp{(}@*)(*@\\HLJLn{x}@*)(*@\\HLJLp{)}@*)\n\\end{lstlisting}\n" 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) c = Weave.format_code(doc.chunks[4].content, doc.format)
@test c_check == c @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) o = Weave.format_output(doc.chunks[3].content, doc.format)
@test o_check == o @test o_check == o
doc.template = "templates/mini.tpl"
rendered = Weave.render_doc("Hello", doc)
@test rendered == "\nHello\n"
# Test wrapping # Test wrapping
cows = repeat("🐄", 100) cows = repeat("🐄", 100)
@ -93,13 +89,12 @@ content = """
""" """
chunk = Weave.DocChunk(content, 1, 1) chunk = Weave.DocChunk(content, 1, 1)
fmt = deepcopy(Weave.formats["md2tex"]) 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" @test f == "\\section{Test chunk}\n\\ensuremath{\\alpha}\n\n"
fmtdict[:keep_unicode] = true fmt.formatdict[:keep_unicode] = true
f = Weave.format_chunk(chunk, fmtdict, fmt) f = Weave.format_chunk(chunk, fmt)
@test f == "\\section{Test chunk}\nα\n\n" @test f == "\\section{Test chunk}\nα\n\n"