-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
interp.jl
48 lines (41 loc) · 1.2 KB
/
interp.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# This file is a part of Julia. License is MIT: https://julialang.org/license
function Base.parse(stream::IO; greedy::Bool = true, raise::Bool = true)
pos = position(stream)
ex, Δ = Base.parse(readstring(stream), 1, greedy = greedy, raise = raise)
seek(stream, pos + Δ - 1)
return ex
end
function interpinner(stream::IO, greedy = false)
startswith(stream, '$') || return
(eof(stream) || Char(peek(stream)) in whitespace) && return
try
return Base.parse(stream::IOBuffer, greedy = greedy)
catch e
return
end
end
@trigger '$' ->
function interp(stream::IO, md::MD)
withstream(stream) do
ex = interpinner(stream)
return ex
end
end
function blockinterp(stream::IO, md::MD)
withstream(stream) do
ex = interpinner(stream)
if ex ≡ nothing
return false
else
push!(md, ex)
return true
end
end
end
toexpr(x) = x
toexpr(xs::Vector{Any}) = Expr(:call, GlobalRef(Base,:vector_any), map(toexpr, xs)...)
for T in Any[MD, Paragraph, Header, Link, Bold, Italic]
@eval function toexpr(md::$T)
Expr(:call, typeof(md), $(map(x->:(toexpr(md.$x)), fieldnames(T))...))
end
end