From edd558de0e7028be9975910ee9c9c0049d22341f Mon Sep 17 00:00:00 2001 From: Matti Pastell Date: Thu, 28 Feb 2019 16:17:03 +0200 Subject: [PATCH] Add support for comments in Markdown. Closes #97 --- src/Weave.jl | 1 - .../html.jl} | 14 ++++- src/WeaveMarkdown/latex.jl | 20 +++++++ src/WeaveMarkdown/markdown.jl | 53 ++++++++++++++++--- src/format.jl | 13 +---- test/markdown_test.jl | 33 ++++++++++-- 6 files changed, 111 insertions(+), 23 deletions(-) rename src/{Markdown2HTML.jl => WeaveMarkdown/html.jl} (96%) create mode 100644 src/WeaveMarkdown/latex.jl diff --git a/src/Weave.jl b/src/Weave.jl index aeaaa7d..fe8cd92 100644 --- a/src/Weave.jl +++ b/src/Weave.jl @@ -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") diff --git a/src/Markdown2HTML.jl b/src/WeaveMarkdown/html.jl similarity index 96% rename from src/Markdown2HTML.jl rename to src/WeaveMarkdown/html.jl index f28c69d..470ff7f 100644 --- a/src/Markdown2HTML.jl +++ b/src/WeaveMarkdown/html.jl @@ -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\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, "") +end + htmlinline(io::IO, x) = tohtml(io, x) # API html(md) = sprint(html, md) -end +#end diff --git a/src/WeaveMarkdown/latex.jl b/src/WeaveMarkdown/latex.jl new file mode 100644 index 0000000..45fcf64 --- /dev/null +++ b/src/WeaveMarkdown/latex.jl @@ -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 diff --git a/src/WeaveMarkdown/markdown.jl b/src/WeaveMarkdown/markdown.jl index d77c721..5a70fa8 100644 --- a/src/WeaveMarkdown/markdown.jl +++ b/src/WeaveMarkdown/markdown.jl @@ -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"^$", 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, "") + 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 diff --git a/src/format.jl b/src/format.jl index c545740..c202635 100644 --- a/src/format.jl +++ b/src/format.jl @@ -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 diff --git a/test/markdown_test.jl b/test/markdown_test.jl index 5daf4a9..5a05112 100644 --- a/test/markdown_test.jl +++ b/test/markdown_test.jl @@ -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

H2

@@ -73,3 +73,30 @@ more math """ @test html == ref_html + +#Test Weave additions +md = Markdown.parse(""" + +Multiline equations + +\$\$ +x = 2 +\$\$ + +And comments + + +""", 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]) == "

\\[\nx = 2\n\\]

" +@test WeaveMarkdown.html(md.content[4]) == "\n\n"