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
|
si::Int
|
||||||
ei::Int
|
ei::Int
|
||||||
number::Int
|
number::Int
|
||||||
|
ctype::Symbol
|
||||||
output::AbstractString
|
output::AbstractString
|
||||||
rich_output::AbstractString
|
rich_output::AbstractString
|
||||||
figures::Array{AbstractString}
|
figures::Array{AbstractString}
|
||||||
function InlineCode(content, si, ei, number)
|
function InlineCode(content, si, ei, number, ctype)
|
||||||
new(content, si, ei, number, "", "", AbstractString[])
|
new(content, si, ei, number, ctype, "", "", AbstractString[])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -123,19 +123,58 @@ function format_inline(inline::InlineCode)
|
||||||
isempty(inline.output) || return inline.output
|
isempty(inline.output) || return inline.output
|
||||||
end
|
end
|
||||||
|
|
||||||
function format_chunk(chunk::DocChunk, formatdict, docformat::JMarkdown2HTML)
|
function ioformat!(io::IOBuffer, out::IOBuffer, fun=WeaveMarkdown.latex)
|
||||||
text = format_chunk(chunk, formatdict, nothing)
|
text = String(take!(io))
|
||||||
#invokelatest seems to be needed here
|
if !isempty(text)
|
||||||
#to fix "invalid age range" on 0.6 #21653
|
m = Markdown.parse(text, flavor=WeaveMarkdown.weavemd)
|
||||||
#m = Compat.invokelatest(Markdown.parse, text)
|
write(out, string(fun(m)))
|
||||||
m = Markdown.parse(text, flavor=WeaveMarkdown.weavemd)
|
end
|
||||||
return string(WeaveMarkdown.html(m))
|
end
|
||||||
|
|
||||||
|
function addspace(op, inline)
|
||||||
|
inline.ctype == :line && (op = "\n$op\n")
|
||||||
|
return op
|
||||||
end
|
end
|
||||||
|
|
||||||
function format_chunk(chunk::DocChunk, formatdict, docformat::JMarkdown2tex)
|
function format_chunk(chunk::DocChunk, formatdict, docformat::JMarkdown2tex)
|
||||||
text = format_chunk(chunk, formatdict, nothing)
|
out = IOBuffer()
|
||||||
m = Markdown.parse(text, flavor=WeaveMarkdown.weavemd)
|
io = IOBuffer()
|
||||||
return uc2tex(Markdown.latex(m))
|
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
|
end
|
||||||
|
|
||||||
function format_chunk(chunk::CodeChunk, formatdict, docformat)
|
function format_chunk(chunk::CodeChunk, formatdict, docformat)
|
||||||
|
|
|
@ -23,18 +23,18 @@ end
|
||||||
const input_formats = Dict{AbstractString, Any}(
|
const input_formats = Dict{AbstractString, Any}(
|
||||||
"noweb" => MarkupInput(r"^<<(.*?)>>=\s*$",
|
"noweb" => MarkupInput(r"^<<(.*?)>>=\s*$",
|
||||||
r"^@\s*$",
|
r"^@\s*$",
|
||||||
r"`j\s+(.*?)`"s
|
r"`j\s+(.*?)`|^!\s(.*)$"m
|
||||||
),
|
),
|
||||||
"markdown" => MarkupInput(
|
"markdown" => MarkupInput(
|
||||||
r"^[`~]{3,}(?:\{|\{\.|)julia(?:;|)\s*(.*?)(\}|\s*)$",
|
r"^[`~]{3,}(?:\{|\{\.|)julia(?:;|)\s*(.*?)(\}|\s*)$",
|
||||||
r"^[`~]{3,}\s*$",
|
r"^[`~]{3,}\s*$",
|
||||||
r"`j\s+(.*?)`"s),
|
r"`j\s+(.*?)`|^!\s(.*)$"m),
|
||||||
"script" => ScriptInput(
|
"script" => ScriptInput(
|
||||||
r"(^#'.*)|(^#%%.*)|(^# %%.*)",
|
r"(^#'.*)|(^#%%.*)|(^# %%.*)",
|
||||||
r"(^#')|(^#%%)|(^# %%)",
|
r"(^#')|(^#%%)|(^# %%)",
|
||||||
r"(^#\+.*$)|(^#%%\+.*$)|(^# %%\+.*$)",
|
r"(^#\+.*$)|(^#%%\+.*$)|(^# %%\+.*$)",
|
||||||
r"(^#\+)|(^#%%\+)|(^# %%\+)",
|
r"(^#\+)|(^#%%\+)|(^# %%\+)",
|
||||||
r"`j\s+(.*?)`"s),
|
r"`j\s+(.*?)`|^!\s(.*)$"m),
|
||||||
"notebook" => NotebookInput(nothing) #Don't parse inline code from notebooks
|
"notebook" => NotebookInput(nothing) #Don't parse inline code from notebooks
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -303,7 +303,10 @@ function parse_inline(text::AbstractString, inline_ex::Regex)
|
||||||
textno += 1
|
textno += 1
|
||||||
push!(res, doc)
|
push!(res, doc)
|
||||||
e = s + lastindex(ic.match)
|
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
|
codeno += 1
|
||||||
end
|
end
|
||||||
push!(res, InlineText(text[e:end], e, length(text), textno))
|
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")
|
@info("Test: Weaving with Gadfly.jl")
|
||||||
include("cache_test.jl")
|
include("cache_test.jl")
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
#@info("Test: Weaving with Plots.jl")
|
@testset "Header options" begin
|
||||||
#include("plotsjl_test.jl")
|
@info("Testing header options")
|
||||||
#include("publish_test.jl")
|
include("options_test.jl")
|
||||||
|
end
|
||||||
|
|
||||||
|
@testset "Inline code" begin
|
||||||
|
@info("Testing inline code")
|
||||||
|
include("inline_test.jl")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
Loading…
Reference in New Issue