Add support for comments in Markdown. Closes #97

pull/185/merge
Matti Pastell 2019-02-28 16:17:03 +02:00
parent badaaa7dea
commit edd558de0e
6 changed files with 111 additions and 23 deletions

View File

@ -219,7 +219,6 @@ include("readers.jl")
include("run.jl")
include("cache.jl")
include("formatters.jl")
include("Markdown2HTML.jl")
include("format.jl")
include("pandoc.jl")
include("writers.jl")

View File

@ -1,4 +1,4 @@
module Markdown2HTML
#module Markdown2HTML
# Markdown to HTML writer, Modified from Julia Base.Markdown html writer
using Markdown: MD, Header, Code, Paragraph, BlockQuote, Footnote,
Admonition, List, HorizontalRule, Bold, Italic, Image, Link, LineBreak,
@ -22,6 +22,8 @@ function tohtml(m::MIME"image/svg+xml", img)
show(io, m, img)
end
# AbstractDisplay infrastructure
function bestmime(val)
@ -161,6 +163,10 @@ function html(io::IO, tex::LaTeX)
end
end
function html(io::IO, comment::Comment)
write(io, "\n<!-- $(comment.text) -->\n")
end
html(io::IO, x) = tohtml(io, x)
# Inline elements
@ -220,10 +226,14 @@ function htmlinline(io::IO, br::LineBreak)
tag(io, :br)
end
function htmlinline(io::IO, comment::Comment)
write(io, "<!-- $(comment.text) -->")
end
htmlinline(io::IO, x) = tohtml(io, x)
# API
html(md) = sprint(html, md)
end
#end

View File

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

View File

@ -3,6 +3,10 @@ module WeaveMarkdown
using Markdown
import Markdown: @trigger, @breaking, Code, MD, withstream, startswith, LaTeX
mutable struct Comment
text::String
end
@breaking true ->
function dollarmath(stream::IO, block::MD)
withstream(stream) do
@ -28,10 +32,47 @@ function dollarmath(stream::IO, block::MD)
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
@breaking true ->
function topcomment(stream::IO, block::MD)
buffer = IOBuffer()
withstream(stream) do
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

View File

@ -1,11 +1,10 @@
import Mustache, Highlights
import .Markdown2HTML
import .WeaveMarkdown
using Compat
using Dates
using Markdown
using REPL.REPLCompletions: latex_symbols
import Markdown.latex
function format(doc::WeaveDoc)
formatted = AbstractString[]
@ -130,7 +129,7 @@ function format_chunk(chunk::DocChunk, formatdict, docformat::JMarkdown2HTML)
#to fix "invalid age range" on 0.6 #21653
#m = Compat.invokelatest(Markdown.parse, text)
m = Markdown.parse(text, flavor=WeaveMarkdown.weavemd)
return string(Markdown2HTML.html(m))
return string(WeaveMarkdown.html(m))
end
function format_chunk(chunk::DocChunk, formatdict, docformat::JMarkdown2tex)
@ -342,11 +341,3 @@ function wrapline(text, line_width=75)
end
result *= text
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

View File

@ -1,10 +1,10 @@
using Test
import Weave: Markdown2HTML
import Weave: WeaveMarkdown
import Markdown
# Test markdown2html writer
html = Markdown2HTML.html(Markdown.parse("""
html = WeaveMarkdown.html(Markdown.parse("""
# H1
@ -39,7 +39,7 @@ x = 3
> Some important quote
"""))
""", flavor = WeaveMarkdown.weavemd))
ref_html = """<h1>H1</h1>
<h2>H2</h2>
@ -73,3 +73,30 @@ more math
"""
@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"