Skip to content

Commit

Permalink
Add markdown flavor for improved math support. Closes #182
Browse files Browse the repository at this point in the history
  • Loading branch information
mpastell committed Feb 27, 2019
1 parent f277337 commit 710b67c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Weave.jl
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ pop_postexecute_hook(f::Function) = splice!(postexecute_hooks, findfirst(postexe

include("config.jl")
include("chunks.jl")
include("WeaveMarkdown/markdown.jl")
include("display_methods.jl")
include("readers.jl")
include("run.jl")
Expand All @@ -224,7 +225,6 @@ include("pandoc.jl")
include("writers.jl")



export weave, list_out_formats, tangle, convert_doc, notebook,
set_chunk_defaults, get_chunk_defaults, restore_chunk_defaults,
include_weave
Expand Down
37 changes: 37 additions & 0 deletions src/WeaveMarkdown/markdown.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#This module extends the julia markdown parser to improve compatibility with Jupyter, Pandoc etc.
module WeaveMarkdown
using Markdown
import Markdown: @trigger, @breaking, Code, MD, withstream, startswith, LaTeX

@breaking true ->
function dollarmath(stream::IO, block::MD)
withstream(stream) do
str = Markdown.startswith(stream, r"^\$\$$"m)
isempty(str) && return false
trailing = strip(readline(stream))
buffer = IOBuffer()
while !eof(stream)
line_start = position(stream)
estr = Markdown.startswith(stream, r"^\$\$$"m)
if !isempty(estr)
estr = Markdown.startswith(stream, r"^\$\$$"m)
if isempty(estr)
push!(block, LaTeX(String(take!(buffer)) |> chomp))
end
return true
else
seek(stream, line_start)
end
write(buffer, readline(stream, keep=true))
end
return false
end
end

# Create own flavor and copy all the features from julia flavor
Markdown.@flavor weavemd [dollarmath]
weavemd.breaking = [weavemd.breaking; Markdown.julia.breaking]
weavemd.regular = Markdown.julia.regular
weavemd.inner = Markdown.julia.inner

end
3 changes: 2 additions & 1 deletion src/display_methods.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Compat
using Markdown
import .WeaveMarkdown

#Contains report global properties
mutable struct Report <: AbstractDisplay
Expand Down Expand Up @@ -109,7 +110,7 @@ function Base.display(report::Report, m::MIME"text/markdown", data)
# Convert to "richer" type of possible
for m in report.mimetypes
if m == "text/html" || m == "text/latex"
display(Markdown.parse(s))
display(Markdown.parse(s, flavor=WeaveMarkdown.weavemd))
break
elseif m == "text/markdown"
report.rich_output *= "\n" * s
Expand Down
7 changes: 4 additions & 3 deletions src/format.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Mustache, Highlights
import .Markdown2HTML
import .WeaveMarkdown
using Compat
using Dates
using Markdown
Expand Down Expand Up @@ -127,14 +128,14 @@ function format_chunk(chunk::DocChunk, formatdict, docformat::JMarkdown2HTML)
text = format_chunk(chunk, formatdict, nothing)
#invokelatest seems to be needed here
#to fix "invalid age range" on 0.6 #21653
m = Compat.invokelatest(Markdown.parse, text)

#m = Compat.invokelatest(Markdown.parse, text)
m = Markdown.parse(text, flavor=WeaveMarkdown.weavemd)
return string(Markdown2HTML.html(m))
end

function format_chunk(chunk::DocChunk, formatdict, docformat::JMarkdown2tex)
text = format_chunk(chunk, formatdict, nothing)
m = Markdown.parse(text)
m = Markdown.parse(text, flavor=WeaveMarkdown.weavemd)
return uc2tex(Markdown.latex(m))
end

Expand Down

0 comments on commit 710b67c

Please sign in to comment.