mirror of https://github.com/mpastell/Weave.jl
Add new syntax for inline code and fix output in md2* formats
parent
371cec474f
commit
37aa0237a8
|
@ -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
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -1,9 +0,0 @@
|
|||
---
|
||||
options:
|
||||
echo : false
|
||||
out_width : 30%
|
||||
---
|
||||
|
||||
```julia
|
||||
repeat("🐐", 10)
|
||||
```
|
|
@ -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])
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue