refactors: almost just about code styles, doesn't change any behaviour

pull/341/head
Shuhei Kadowaki 2020-05-23 20:07:59 +09:00
parent 9ca328f468
commit 1fb40645ed
10 changed files with 120 additions and 202 deletions

View File

@ -21,6 +21,7 @@ end
isnothing(::Any) = false
isnothing(::Nothing) = true
end
take2string!(io) = String(take!(io))
"""
list_out_formats()

View File

@ -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

View File

@ -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, "")

View File

@ -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

View File

@ -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"""

View File

@ -4,23 +4,21 @@ using Mustache, Highlights, .WeaveMarkdown, Markdown, Dates, Pkg
using REPL.REPLCompletions: latex_symbols
function format(doc, template = nothing, highlight_theme = nothing; css = nothing)
format = doc.format
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[:theme] = highlight_theme = get_highlight_theme(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)
lines = map(copy(doc.chunks)) do chunk
format_chunk(chunk, formatdict, format)
format_chunk(chunk, docformat)
end
body = join(lines, '\n')
@ -57,6 +55,7 @@ end
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"))
@ -96,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
@ -106,24 +105,14 @@ 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
addlines(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)
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)
@ -131,20 +120,19 @@ function format_chunk(chunk::DocChunk, formatdict, docformat::JMarkdown2tex)
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)
clear_buffer_and_format!(io, out, WeaveMarkdown.html)
write(out, addlines(inline.rich_output, inline))
elseif !isempty(inline.figures)
write(io, inline.figures[end])
@ -152,60 +140,64 @@ function format_chunk(chunk::DocChunk, formatdict, docformat::JMarkdown2HTML)
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
@ -215,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
@ -246,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
@ -274,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))
@ -381,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

View File

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

View File

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

View File

@ -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&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>

View File

@ -12,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
@ -26,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] = DefaultTheme
doc.format.formatdict[:highlight_theme] = DefaultTheme
c = Weave.format_code(doc.chunks[3].content, doc.format)
@test c_check == c
@ -39,7 +39,7 @@ 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] = DefaultTheme
doc.format.formatdict[:highlight_theme] = DefaultTheme
c = Weave.format_code(doc.chunks[4].content, doc.format)
@test c_check == c
@ -89,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"