mirror of https://github.com/mpastell/Weave.jl
Add support for comments in Markdown. Closes #97
parent
badaaa7dea
commit
edd558de0e
|
@ -219,7 +219,6 @@ include("readers.jl")
|
||||||
include("run.jl")
|
include("run.jl")
|
||||||
include("cache.jl")
|
include("cache.jl")
|
||||||
include("formatters.jl")
|
include("formatters.jl")
|
||||||
include("Markdown2HTML.jl")
|
|
||||||
include("format.jl")
|
include("format.jl")
|
||||||
include("pandoc.jl")
|
include("pandoc.jl")
|
||||||
include("writers.jl")
|
include("writers.jl")
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
module Markdown2HTML
|
#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: MD, Header, Code, Paragraph, BlockQuote, Footnote,
|
using Markdown: MD, Header, Code, Paragraph, BlockQuote, Footnote,
|
||||||
Admonition, List, HorizontalRule, Bold, Italic, Image, Link, LineBreak,
|
Admonition, List, HorizontalRule, Bold, Italic, Image, Link, LineBreak,
|
||||||
|
@ -22,6 +22,8 @@ function tohtml(m::MIME"image/svg+xml", img)
|
||||||
show(io, m, img)
|
show(io, m, img)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# AbstractDisplay infrastructure
|
# AbstractDisplay infrastructure
|
||||||
|
|
||||||
function bestmime(val)
|
function bestmime(val)
|
||||||
|
@ -161,6 +163,10 @@ function html(io::IO, tex::LaTeX)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function html(io::IO, comment::Comment)
|
||||||
|
write(io, "\n<!-- $(comment.text) -->\n")
|
||||||
|
end
|
||||||
|
|
||||||
html(io::IO, x) = tohtml(io, x)
|
html(io::IO, x) = tohtml(io, x)
|
||||||
|
|
||||||
# Inline elements
|
# Inline elements
|
||||||
|
@ -220,10 +226,14 @@ function htmlinline(io::IO, br::LineBreak)
|
||||||
tag(io, :br)
|
tag(io, :br)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function htmlinline(io::IO, comment::Comment)
|
||||||
|
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
|
#end
|
|
@ -0,0 +1,20 @@
|
||||||
|
import Markdown: latex, latexinline
|
||||||
|
|
||||||
|
function latex(io::IO, tex::Markdown.LaTeX)
|
||||||
|
math_envs = ["align", "equation", "eqnarray"]
|
||||||
|
use_dollars = !any([occursin("\\begin{$me", tex.formula) for me in math_envs])
|
||||||
|
use_dollars && write(io, "\\[")
|
||||||
|
write(io, string("\n", tex.formula, "\n"))
|
||||||
|
use_dollars && write(io, "\\]\n")
|
||||||
|
end
|
||||||
|
|
||||||
|
#Remove comments that can occur inside a line
|
||||||
|
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")
|
||||||
|
write(io, "% $line\n")
|
||||||
|
end
|
||||||
|
end
|
|
@ -3,6 +3,10 @@ module WeaveMarkdown
|
||||||
using Markdown
|
using Markdown
|
||||||
import Markdown: @trigger, @breaking, Code, MD, withstream, startswith, LaTeX
|
import Markdown: @trigger, @breaking, Code, MD, withstream, startswith, LaTeX
|
||||||
|
|
||||||
|
mutable struct Comment
|
||||||
|
text::String
|
||||||
|
end
|
||||||
|
|
||||||
@breaking true ->
|
@breaking true ->
|
||||||
function dollarmath(stream::IO, block::MD)
|
function dollarmath(stream::IO, block::MD)
|
||||||
withstream(stream) do
|
withstream(stream) do
|
||||||
|
@ -28,10 +32,47 @@ function dollarmath(stream::IO, block::MD)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Create own flavor and copy all the features from julia flavor
|
@breaking true ->
|
||||||
Markdown.@flavor weavemd [dollarmath]
|
function topcomment(stream::IO, block::MD)
|
||||||
weavemd.breaking = [weavemd.breaking; Markdown.julia.breaking]
|
buffer = IOBuffer()
|
||||||
weavemd.regular = Markdown.julia.regular
|
withstream(stream) do
|
||||||
weavemd.inner = Markdown.julia.inner
|
str = Markdown.startswith(stream, r"^<!--")
|
||||||
|
isempty(str) && return false
|
||||||
|
while !eof(stream)
|
||||||
|
line = readline(stream, keep=true)
|
||||||
|
write(buffer, line)
|
||||||
|
if occursin(r"-->$", line)
|
||||||
|
s = replace(String(take!(buffer)) |> chomp, r"-->$" => "")
|
||||||
|
push!(block, Comment(s))
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@trigger '<' ->
|
||||||
|
function comment(stream::IO, md::MD)
|
||||||
|
withstream(stream) do
|
||||||
|
Markdown.startswith(stream, "<!--") || return
|
||||||
|
text = Markdown.readuntil(stream, "-->")
|
||||||
|
text ≡ nothing && return
|
||||||
|
return Comment(text)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Create own flavor and copy all the features from julia flavor
|
||||||
|
Markdown.@flavor weavemd [dollarmath, comment, topcomment]
|
||||||
|
weavemd.breaking = [weavemd.breaking; Markdown.julia.breaking]
|
||||||
|
weavemd.regular = [weavemd.regular; Markdown.julia.regular]
|
||||||
|
for key in keys(Markdown.julia.inner)
|
||||||
|
if haskey(weavemd.inner, key)
|
||||||
|
weavemd.inner[key] = [weavemd.inner[key]; Markdown.julia.inner[key]]
|
||||||
|
else
|
||||||
|
weavemd.inner[key] = Markdown.julia.inner[key]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
include("html.jl")
|
||||||
|
include("latex.jl")
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,11 +1,10 @@
|
||||||
import Mustache, Highlights
|
import Mustache, Highlights
|
||||||
import .Markdown2HTML
|
|
||||||
import .WeaveMarkdown
|
import .WeaveMarkdown
|
||||||
using Compat
|
using Compat
|
||||||
using Dates
|
using Dates
|
||||||
using Markdown
|
using Markdown
|
||||||
using REPL.REPLCompletions: latex_symbols
|
using REPL.REPLCompletions: latex_symbols
|
||||||
import Markdown.latex
|
|
||||||
|
|
||||||
function format(doc::WeaveDoc)
|
function format(doc::WeaveDoc)
|
||||||
formatted = AbstractString[]
|
formatted = AbstractString[]
|
||||||
|
@ -130,7 +129,7 @@ function format_chunk(chunk::DocChunk, formatdict, docformat::JMarkdown2HTML)
|
||||||
#to fix "invalid age range" on 0.6 #21653
|
#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)
|
m = Markdown.parse(text, flavor=WeaveMarkdown.weavemd)
|
||||||
return string(Markdown2HTML.html(m))
|
return string(WeaveMarkdown.html(m))
|
||||||
end
|
end
|
||||||
|
|
||||||
function format_chunk(chunk::DocChunk, formatdict, docformat::JMarkdown2tex)
|
function format_chunk(chunk::DocChunk, formatdict, docformat::JMarkdown2tex)
|
||||||
|
@ -342,11 +341,3 @@ function wrapline(text, line_width=75)
|
||||||
end
|
end
|
||||||
result *= text
|
result *= text
|
||||||
end
|
end
|
||||||
|
|
||||||
function latex(io::IO, tex::Markdown.LaTeX)
|
|
||||||
math_envs = ["align", "equation", "eqnarray"]
|
|
||||||
use_dollars = !any([occursin("\\begin{$me", tex.formula) for me in math_envs])
|
|
||||||
use_dollars && write(io, "\\[")
|
|
||||||
write(io, string("\n", tex.formula, "\n"))
|
|
||||||
use_dollars && write(io, "\\]\n")
|
|
||||||
end
|
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
using Test
|
using Test
|
||||||
import Weave: Markdown2HTML
|
import Weave: WeaveMarkdown
|
||||||
import Markdown
|
import Markdown
|
||||||
|
|
||||||
# Test markdown2html writer
|
# Test markdown2html writer
|
||||||
|
|
||||||
html = Markdown2HTML.html(Markdown.parse("""
|
html = WeaveMarkdown.html(Markdown.parse("""
|
||||||
|
|
||||||
# H1
|
# H1
|
||||||
|
|
||||||
|
@ -39,7 +39,7 @@ x = 3
|
||||||
|
|
||||||
> Some important quote
|
> Some important quote
|
||||||
|
|
||||||
"""))
|
""", flavor = WeaveMarkdown.weavemd))
|
||||||
|
|
||||||
ref_html = """<h1>H1</h1>
|
ref_html = """<h1>H1</h1>
|
||||||
<h2>H2</h2>
|
<h2>H2</h2>
|
||||||
|
@ -73,3 +73,30 @@ more math
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@test html == ref_html
|
@test html == ref_html
|
||||||
|
|
||||||
|
#Test Weave additions
|
||||||
|
md = Markdown.parse("""
|
||||||
|
|
||||||
|
Multiline equations
|
||||||
|
|
||||||
|
\$\$
|
||||||
|
x = 2
|
||||||
|
\$\$
|
||||||
|
|
||||||
|
And comments <!-- inline -->
|
||||||
|
|
||||||
|
<!--
|
||||||
|
Multiple lines
|
||||||
|
-->
|
||||||
|
""", flavor = WeaveMarkdown.weavemd);
|
||||||
|
|
||||||
|
@test md.content[2].formula == "x = 2"
|
||||||
|
@test typeof(md.content[3].content[2]) == WeaveMarkdown.Comment
|
||||||
|
@test md.content[3].content[2].text == " inline "
|
||||||
|
@test md.content[4].text == "\nMultiple lines\n "
|
||||||
|
|
||||||
|
@test WeaveMarkdown.latex(md.content[2]) == "\\[\nx = 2\n\\]\n"
|
||||||
|
@test WeaveMarkdown.latex(md.content[4]) == "% \n% Multiple lines\n% \n"
|
||||||
|
|
||||||
|
@test WeaveMarkdown.html(md.content[2]) == "<p class=\"math\">\\[\nx = 2\n\\]</p>"
|
||||||
|
@test WeaveMarkdown.html(md.content[4]) == "\n<!-- \nMultiple lines\n -->\n"
|
||||||
|
|
Loading…
Reference in New Issue