Skip to content

Commit

Permalink
close 151
Browse files Browse the repository at this point in the history
  • Loading branch information
tlienart committed May 20, 2019
1 parent 5028e1a commit 9813500
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 5 deletions.
4 changes: 2 additions & 2 deletions docs/src/lib/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ julia> JuDoc.from(t[1])
julia> ocb, _ = JuDoc.find_all_ocblocks(t, JuDoc.MD_OCB_ALL);
julia> ocb
1-element Array{JuDoc.OCBlock,1}:
JuDoc.OCBlock(:MATH_A, JuDoc.Token(:MATH_A, "\$") => JuDoc.Token(:MATH_A, "\$"), "\$x=5\$")
JuDoc.OCBlock(:MATH_A, JuDoc.Token(:MATH_A, "\$") => JuDoc.Token(:MATH_A, "\$"), "\$x=5\$", false)
julia> JuDoc.from(ocb[1])
8
Expand Down Expand Up @@ -185,7 +185,7 @@ JuDoc.LxCom(
"\\foo{hello}",
Base.RefArray{...}(...),
JuDoc.OCBlock[JuDoc.OCBlock(:LXB, JuDoc.Token(:LXB_OPEN, "{") => JuDoc.Token(:LXB_CLOSE, "}"),
"{hello}")])
"{hello}", true)])

```

Expand Down
37 changes: 35 additions & 2 deletions src/parser/ocblocks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,44 @@ function find_all_ocblocks(tokens::Vector{Token},
inmath=false) where S <: Symbol

ocbs_all = Vector{OCBlock}()
for (name, (ocpair, nest)) ocblist
for (name, (ocpair, nestable)) ocblist
ocbs, tokens = find_ocblocks(tokens, name, ocpair;
nestable=nest, inmath=inmath)
nestable=nestable, inmath=inmath)
append!(ocbs_all, ocbs)
end

# it may happen that a block is contained in a larger escape block.
# For instance this can happen if there is a code block in an escape block (see e.g. #151).
# To fix this, we browse the escape blocks in backwards order and check if there is any other
# block within it.
i = length(ocbs_all)
ignore = Int[]
while i > 1
cur_ocb = ocbs_all[i]
if cur_ocb.name MD_OCB_ESC
cur_head, cur_tail = from(cur_ocb), to(cur_ocb)
# find the first block before the ith one which has its head after the current head
# this will often be nothing but if it itsn't, then it should be ignored
kf = findfirst(k -> cur_head < from(ocbs_all[k]) < cur_tail, 1:i-1)
if !isnothing(kf)
append!(ignore, kf:i-1)
i = kf - 1
else
i -= 1
end
else
i -= 1
end
end
# for (i,ocbi) ∈ enumerate(ocbs_all)
# @show i
# @show ocbi
# @show from(ocbi)
# @show to(ocbi)
# end
# @show ignore
deleteat!(ocbs_all, ignore)

return ocbs_all, tokens
end

Expand Down
2 changes: 1 addition & 1 deletion src/parser/tokens.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ $(SIGNATURES)
Shorthand constructor to instantiate an `OCBlock` inferring the associated substring from the
`ocpair` (since it's the substring in between the tokens).
"""
OCBlock::Symbol, ω::Pair{Token,Token}) =
OCBlock::Symbol, ω::Pair{Token,Token}, nestable::Bool=false) =
OCBlock(η, ω, subs(str.first), from.first), to.second)))


Expand Down
8 changes: 8 additions & 0 deletions src/parser/tokens_md.jl
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ def are allowed.
* ordering matters!
=#

"""
MD_OCB_ESC
Blocks that will be escaped (their content will not be further processed).
Corresponds to the non-nestable elements of `MD_OCB`.
"""
const MD_OCB_ESC = [e.first for e MD_OCB if !e.second[2]]


"""
MD_OCB_MATH
Expand Down
19 changes: 19 additions & 0 deletions test/_dummies/151.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
aaa
~~~
<pre><code class="julia">
"""
A
B
# C
```jldoctest
D
```
"""
function bar(x, y)
E
end
</code></pre>
~~~
bbb
5 changes: 5 additions & 0 deletions test/converter/integration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,8 @@ end
""" * J.EOS
st |> conv == "<div class=\"emptydiv\"><div class=\"emptycore\"></div>\n</div>\n"
end

@testset "HTML escape" begin # see #151
st = read(joinpath(D, "151.md"), String)
st |> conv == "<p>aaa \n<pre><code class=\"julia\">\n\"\"\"\n A\n\nB\n\n# C\n```jldoctest\nD\n```\n\"\"\"\nfunction bar(x, y)\nE\nend\n</code></pre>\n bbb</p>\n"
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using JuDoc, Random, Test
const J = JuDoc
const D = joinpath(dirname(dirname(pathof(JuDoc))), "test", "_dummies")

# NOTE this first file MUST be included before running the rest of the tests
# otherwise you may get an error like "key 0x099191234..." was not found or
Expand Down

0 comments on commit 9813500

Please sign in to comment.