Skip to content

Commit

Permalink
Fix string interpolation into Markdown.Table and Markdown.List in md"…
Browse files Browse the repository at this point in the history
…..." strings, fixes JuliaLang#16194. (JuliaLang#37130)
  • Loading branch information
fredrikekre authored Aug 20, 2020
1 parent ec63d88 commit d446c27
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
19 changes: 16 additions & 3 deletions stdlib/Markdown/src/Julia/interp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ function interpinner(stream::IO, greedy = false)
startswith(stream, '$') || return
(eof(stream) || peek(stream, Char) in whitespace) && return
try
return _parse(stream::IOBuffer, greedy = greedy)
return _parse(stream, greedy = greedy)
catch e
return
if isa(e, Meta.ParseError)
return nothing
else
rethrow()
end
end
end

Expand All @@ -39,10 +43,19 @@ end

toexpr(x) = x

toexpr(xs::Vector{Any}) = Expr(:call, GlobalRef(Base,:getindex), Any, mapany(toexpr, xs)...)
toexpr(xs::Union{Vector{Any},Vector{Vector{Any}}}) =
Expr(:call, GlobalRef(Base,:getindex), Any, mapany(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(Base.unwrap_unionall(T)))...))
end
end

function toexpr(md::Table)
Expr(:call, Table, toexpr(md.rows), md.align)
end

function toexpr(md::List)
Expr(:call, List, toexpr(md.items), md.ordered, md.loose)
end
20 changes: 20 additions & 0 deletions stdlib/Markdown/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1138,3 +1138,23 @@ let m = Markdown.parse("---"), io = IOBuffer()
show(io, "text/latex", m)
@test String(take!(io)) == "\\rule{\\textwidth}{1pt}\n"
end

# issue #16194: interpolation in md"..." strings
@testset "issue #16194: interpolation in md\"...\" strings" begin
x = "X"
contains_X(md) = occursin(x, sprint(show, MIME("text/plain"), md))
@test contains_X(md"# $x") # H1
@test contains_X(md"## $x") # H2
@test contains_X(md"### $x") # H3
@test contains_X(md"x = $x") # Paragraph
@test contains_X(md"- $x") # List
@test contains_X(md"[$x](..)") # Link
@test contains_X(md"**$x**") # Bold
@test contains_X(md"*$x*") # Italic
@test contains_X( # Table
md"""
| name |
|------|
| $x |
""")
end

0 comments on commit d446c27

Please sign in to comment.