Skip to content

Commit

Permalink
fixing [name][id] links and formatted links (#267)
Browse files Browse the repository at this point in the history
* fixing [name][id] links

* closes #266
  • Loading branch information
tlienart authored Oct 11, 2019
1 parent 3ebe463 commit db53456
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 21 deletions.
33 changes: 18 additions & 15 deletions src/converter/link_fixer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@ function find_and_fix_md_links(hs::String)::String
# 1 > (!)? == either ! or nothing
# 2 > [(.*?)] == [...] inside of the brackets
# 3 > (?:[(.*?)])? == [...] inside of second brackets if there is such
m_link_refs = collect(eachmatch(r"(!)?[(.*?)](?!:)(?:[(.*?)])?", hs))
rx = r"(!)?[(.*?)](?!:)(?:[(.*?)])?"
m_link_refs = collect(eachmatch(rx, hs))

# recuperate the appropriate name which has a chance to match def_names
# recuperate the appropriate id which has a chance to match def_names
ref_names = [
# no second bracket or empty second bracket ?
# >> true then the id is in the first bracket
# >> false then the id is in the second bracket
# >> true then the id is in the first bracket A --> [id] or [id][]
# >> false then the id is in the second bracket B --> [...][id]
ifelse(isnothing(ref.captures[3]) || isempty(ref.captures[3]),
ref.captures[2], # first bracket
ref.captures[3]) # second bracket
ref.captures[2], # A. first bracket
ref.captures[3]) # B. second bracket
for ref in m_link_refs]

# reconstruct the text
Expand All @@ -41,17 +42,19 @@ function find_and_fix_md_links(hs::String)::String
# no def found --> just leave it as it was
write(h, m.match)
else
if !isnothing(m.captures[3]) && isempty(m.captures[3])
# [link text][] indicating that the link text is the title
write(h, html_ahref(def, refn; title=refn))
if !isnothing(m.captures[1])
# CASE: ![alt][id] --> image
write(h, html_img(def, refn))
else
if !isnothing(m.captures[1])
# ![alt][id]
write(h, html_img(def, refn))
else
# either [link text] and [link text]: ... elsewhere or
# [link text][id] and [id]: ... later
# It's a link
if isnothing(m.captures[3]) || isempty(m.captures[3])
# CASE: [id] or [id][] the id is also the link text
write(h, html_ahref(def, refn))
else
# It's got a second, non-empty bracket
# CASE: [name][id]
name = m.captures[2]
write(h, html_ahref(def, name))
end
end
end
Expand Down
7 changes: 6 additions & 1 deletion src/parser/md_validate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function validate_header_block(β::OCBlock)::Bool
return false
end

postprocess_link(s::String) = replace(r"")

"""
$(SIGNATURES)
Expand All @@ -84,7 +85,11 @@ function validate_and_store_link_defs!(blocks::Vector{OCBlock})::Nothing
# redefine the full block
ftk = Token(:FOO,subs(""))
# we have a [id]: lk add it to PAGE_LINK_DEFS
id = subs(parent, nextind(parent, k), ini) |> htmlesc
id = subs(parent, nextind(parent, k), ini)
# issue #266 in case there's formatting in the link
id = jd2html(id, internal=true)
id = replace(id, r"^<p>"=>"")
id = replace(id, r"<\/p>\n$"=>"")
lk = β |> content |> strip |> string
PAGE_LINK_DEFS[id] = lk
# replace the block by a full one so that it can be fully
Expand Down
15 changes: 13 additions & 2 deletions test/converter/markdown3.jl
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ end
<p>
A <a href="https://julialang.org/">link</a> and
B <a href="https://www.mozilla.org/">link 2</a> and
C <a href="https://www.python.org/" title="Python">Python</a> and
D <a href="http://slashdot.org/">1</a> and blah end</p>""")
C <a href="https://www.python.org/">Python</a> and
D <a href="http://slashdot.org/">a link</a> and blah end</p>""")
end

@testset "fixlinks2" begin
Expand Down Expand Up @@ -319,3 +319,14 @@ end
""" * J.EOS
@test isapproxstr(st |> seval, """<p>A <code>blah</code>.</p>""")
end

@testset "Issue 266" begin
s = """Blah [`hello`] and later
[`hello`]: https://github.com/cormullion/
""" |> jd2html_td
@test isapproxstr(s, """
<p>Blah
<a href="https://github.com/cormullion/"><code>hello</code></a>
and later </p>
""")
end
6 changes: 3 additions & 3 deletions test/coverage/extras1.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
@testset "Converter-lx" begin
@testset "Conv-lx" begin
cd(td)
# Exception instead of ArgumentError as may fail with system error
@test_throws Exception J.check_input_rpath("aldjfk")
end

@testset "Converter-html" begin
@testset "Conv-html" begin
@test_throws J.HTMLFunctionError J.convert_html("{{fill bb cc}}", J.PageVars())
@test_throws J.HTMLFunctionError J.convert_html("{{insert bb cc}}", J.PageVars())
@test_throws J.HTMLFunctionError J.convert_html("{{href aa}}", J.PageVars())
Expand All @@ -15,7 +15,7 @@ end
@test_throws J.HTMLBlockError J.convert_html("{{ispage asdf}}", J.PageVars())
end

@testset "Converter-md" begin
@testset "Conv-md" begin
s = """
@def blah
"""
Expand Down

0 comments on commit db53456

Please sign in to comment.