Skip to content

Commit

Permalink
refactors: almost just about code styles, doesn't change any behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
aviatesk committed May 23, 2020
1 parent 9ca328f commit 1fb4064
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 202 deletions.
1 change: 1 addition & 0 deletions src/Weave.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ end
isnothing(::Any) = false
isnothing(::Nothing) = true
end
take2string!(io) = String(take!(io))

"""
list_out_formats()
Expand Down
41 changes: 11 additions & 30 deletions src/WeaveMarkdown/html.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# module Markdown2HTML
# Markdown to HTML writer, Modified from Julia Base.Markdown html writer

using Markdown:
MD,
Header,
Expand All @@ -19,23 +19,18 @@ 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,""")
print(io, stringmime(m, 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

Expand Down Expand Up @@ -168,19 +163,15 @@ 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
write(io, string("\\[\n", tex.formula, "\n\\]"))
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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
9 changes: 3 additions & 6 deletions src/WeaveMarkdown/latex.jl
Original file line number Diff line number Diff line change
@@ -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, "")
13 changes: 9 additions & 4 deletions src/WeaveMarkdown/markdown.jl
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -88,6 +91,8 @@ for key in keys(Markdown.julia.inner)
end
end


include("html.jl")
include("latex.jl")
end

end # module
4 changes: 2 additions & 2 deletions src/display_methods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"""
Expand Down
Loading

0 comments on commit 1fb4064

Please sign in to comment.