From 37aa0237a8430a1b5342dfdc88ac21643078d15e Mon Sep 17 00:00:00 2001 From: Matti Pastell Date: Wed, 6 Mar 2019 22:27:47 +0200 Subject: [PATCH] Add new syntax for inline code and fix output in md2* formats --- src/chunks.jl | 5 ++-- src/format.jl | 59 ++++++++++++++++++++++++++++++++++++-------- src/readers.jl | 11 ++++++--- test/header_test.jmd | 9 ------- test/inline_test.jl | 26 +++++++++++++++++++ test/runtests.jl | 14 ++++++++--- 6 files changed, 95 insertions(+), 29 deletions(-) delete mode 100644 test/header_test.jmd create mode 100644 test/inline_test.jl diff --git a/src/chunks.jl b/src/chunks.jl index 9e9570b..6060564 100644 --- a/src/chunks.jl +++ b/src/chunks.jl @@ -72,11 +72,12 @@ mutable struct InlineCode <: Inline si::Int ei::Int number::Int + ctype::Symbol output::AbstractString rich_output::AbstractString figures::Array{AbstractString} - function InlineCode(content, si, ei, number) - new(content, si, ei, number, "", "", AbstractString[]) + function InlineCode(content, si, ei, number, ctype) + new(content, si, ei, number, ctype, "", "", AbstractString[]) end end diff --git a/src/format.jl b/src/format.jl index c202635..c7e132d 100644 --- a/src/format.jl +++ b/src/format.jl @@ -123,19 +123,58 @@ function format_inline(inline::InlineCode) isempty(inline.output) || return inline.output end -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 = Markdown.parse(text, flavor=WeaveMarkdown.weavemd) - return string(WeaveMarkdown.html(m)) +function ioformat!(io::IOBuffer, out::IOBuffer, fun=WeaveMarkdown.latex) + text = String(take!(io)) + if !isempty(text) + m = Markdown.parse(text, flavor=WeaveMarkdown.weavemd) + write(out, string(fun(m))) + end +end + +function addspace(op, inline) + inline.ctype == :line && (op = "\n$op\n") + return op end function format_chunk(chunk::DocChunk, formatdict, docformat::JMarkdown2tex) - text = format_chunk(chunk, formatdict, nothing) - m = Markdown.parse(text, flavor=WeaveMarkdown.weavemd) - return uc2tex(Markdown.latex(m)) + out = IOBuffer() + io = IOBuffer() + for inline in chunk.content + if isa(inline, InlineText) + write(io, inline.content) + elseif !isempty(inline.rich_output) + ioformat!(io, out) + write(out, addspace(inline.rich_output, inline)) + elseif !isempty(inline.figures) + ioformat!(io, out) + write(out, addspace(inline.figures[end], inline)) + elseif !isempty(inline.output) + write(io, addspace(inline.output, inline)) + end + end + ioformat!(io, out) + return uc2tex(String(take!(out))) +end + +function format_chunk(chunk::DocChunk, formatdict, docformat::JMarkdown2HTML) + out = IOBuffer() + io = IOBuffer() + fun = WeaveMarkdown.html + for inline in chunk.content + if isa(inline, InlineText) + write(io, inline.content) + elseif !isempty(inline.rich_output) + ioformat!(io, out, fun) + write(out, addspace(inline.rich_output, inline)) + elseif !isempty(inline.figures) + ioformat!(io, out, fun) + write(out, addspace(inline.figures[end], inline)) + elseif !isempty(inline.output) + write(io, addspace(inline.output, inline)) + end + end + ioformat!(io, out, fun) + return String(take!(out)) end function format_chunk(chunk::CodeChunk, formatdict, docformat) diff --git a/src/readers.jl b/src/readers.jl index 9a3f958..dfb9110 100644 --- a/src/readers.jl +++ b/src/readers.jl @@ -23,18 +23,18 @@ end const input_formats = Dict{AbstractString, Any}( "noweb" => MarkupInput(r"^<<(.*?)>>=\s*$", r"^@\s*$", - r"`j\s+(.*?)`"s + r"`j\s+(.*?)`|^!\s(.*)$"m ), "markdown" => MarkupInput( r"^[`~]{3,}(?:\{|\{\.|)julia(?:;|)\s*(.*?)(\}|\s*)$", r"^[`~]{3,}\s*$", - r"`j\s+(.*?)`"s), + r"`j\s+(.*?)`|^!\s(.*)$"m), "script" => ScriptInput( r"(^#'.*)|(^#%%.*)|(^# %%.*)", r"(^#')|(^#%%)|(^# %%)", r"(^#\+.*$)|(^#%%\+.*$)|(^# %%\+.*$)", r"(^#\+)|(^#%%\+)|(^# %%\+)", - r"`j\s+(.*?)`"s), + r"`j\s+(.*?)`|^!\s(.*)$"m), "notebook" => NotebookInput(nothing) #Don't parse inline code from notebooks ) @@ -303,7 +303,10 @@ function parse_inline(text::AbstractString, inline_ex::Regex) textno += 1 push!(res, doc) e = s + lastindex(ic.match) - push!(res, InlineCode(ic.captures[1], s, e, codeno)) + !isnothing(ic.captures[1]) && (ctype = :inline) + !isnothing(ic.captures[2]) && (ctype = :line) + cap = filter(!isnothing, ic.captures)[1] + push!(res, InlineCode(cap, s, e, codeno, ctype)) codeno += 1 end push!(res, InlineText(text[e:end], e, length(text), textno)) diff --git a/test/header_test.jmd b/test/header_test.jmd deleted file mode 100644 index 3e770c5..0000000 --- a/test/header_test.jmd +++ /dev/null @@ -1,9 +0,0 @@ ---- -options: - echo : false - out_width : 30% ---- - -```julia -repeat("🐐", 10) -``` diff --git a/test/inline_test.jl b/test/inline_test.jl new file mode 100644 index 0000000..2ba134f --- /dev/null +++ b/test/inline_test.jl @@ -0,0 +1,26 @@ +using Weave, Test + +doc = """ + +! println("Something") + +Some markdown with inline stuff and `j code` + + ! Not julia code but `j show("is")` + +""" + +pat = Weave.input_formats["markdown"].inline +ms = collect(eachmatch(pat, doc)) +@test ms[1][2] == "println(\"Something\")" +@test ms[2][1] == "code" +@test ms[3][1] == "show(\"is\")" + +chunk = Weave.parse_doc(doc, Weave.input_formats["markdown"])[1] +@test length(chunk.content) == 7 +@test chunk.content[2].content == ms[1][2] +@test chunk.content[4].content == ms[2][1] +@test chunk.content[6].content == ms[3][1] + +chunknw = Weave.parse_doc(doc, Weave.input_formats["noweb"])[1] +@test all([chunknw.content[i].content == chunk.content[i].content for i in 1:7]) diff --git a/test/runtests.jl b/test/runtests.jl index 5c7efad..32eaebf 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -43,8 +43,14 @@ using Test @info("Test: Weaving with Gadfly.jl") include("cache_test.jl") end -end -#@info("Test: Weaving with Plots.jl") -#include("plotsjl_test.jl") -#include("publish_test.jl") + @testset "Header options" begin + @info("Testing header options") + include("options_test.jl") + end + + @testset "Inline code" begin + @info("Testing inline code") + include("inline_test.jl") + end +end