Skip to content

Commit

Permalink
closes #130
Browse files Browse the repository at this point in the history
  • Loading branch information
tlienart committed May 29, 2019
1 parent 8934a0f commit 8a4186a
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/converter/lx.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ JD_LOC_EQDICT_COUNTER
Counter to keep track of equation numbers as they appear along the page, this helps with equation
referencing.
"""
const JD_LOC_EQDICT_COUNTER = "COUNTER_" * randstring(JD_LEN_RANDSTRING)
const JD_LOC_EQDICT_COUNTER = "COUNTER_XC0q"

"""
$(SIGNATURES)
Expand Down
7 changes: 4 additions & 3 deletions src/converter/md_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function md2html(ss::AbstractString, stripp::Bool=false)::AbstractString
isempty(ss) && return ss

# Use the base Markdown -> Html converter and post process headers
partial = ss |> Markdown.parse |> Markdown.html |> make_headers_links
partial = ss |> Markdown.parse |> Markdown.html |> make_header_refs

# In some cases, base converter adds <p>...</p>\n which we might not want
stripp || return partial
Expand Down Expand Up @@ -56,14 +56,15 @@ By default the Base Markdown to HTML converter simply converts `## ...` into hea
linkable ones; this is annoying for generation of table of contents etc (and references in
general) so this function does just that.
"""
function header_ref(h::String)::String
function make_header_refs(h::String)::String
io = IOBuffer()
head = 1
for m eachmatch(r"<h([1-6])>(.*?)</h[1-6]>", h)
write(io, subs(h, head:m.offset-1))
level = m.captures[1]
name = m.captures[2]
write(io, "<h$(level)><a id=\"$(refstring(name))\"></a>$(name)</h$(level)>")
ref = refstring(name)
write(io, "<h$level><a id=\"$ref\" href=\"#$ref\">$name</a></h$level>")
head = m.offset + lastindex(m.match)
end
write(io, subs(h, head:lastindex(h)))
Expand Down
12 changes: 10 additions & 2 deletions src/misc_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,13 @@ $(SIGNATURES)
Takes a string `s` and replace spaces by underscores so that that we can use it
for hyper-references. So for instance `"aa bb"` will become `aa-bb`.
"""
refstring(s::AbstractString)::String = replace(lowercase(strip(s)), r"\s+" => "-")
It also defensively removes any non-word character so for instance `"aa bb !"` will be `"aa-bb"`
"""
function refstring(s::AbstractString)::String
# remove non-word characters
st = replace(s, r"&#[0-9]+;" => "")
st = replace(st, r"[^a-zA-Z0-9_\-\s]" => "")
st = replace(lowercase(strip(st)), r"\s+" => "-")
isempty(st) && return string(hash(s))
return st
end
15 changes: 15 additions & 0 deletions test/converter/markdown.jl
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,18 @@ end
hstring = J.convert_inter_html(inter_html, blocks2insert, lxcontext)
@test hstring == "<p>text A1 text A2 blah and \nescape B1\n text C1 \\(\\mathrm{ b}\\) text C2 then part1: AA and part2: BB.</p>\n"
end


@testset "headers" begin
st = """
# Title
and then
## Subtitle cool!
done
"""
h = st |> Markdown.parse |> Markdown.html
r = J.make_header_refs(h)
@test occursin("<h1><a id=\"title\" href=\"#title\">Title</a></h1>", r)
@test occursin("<h2><a id=\"subtitle-cool\" href=\"#subtitle-cool\">Subtitle cool&#33;</a></h2>", r)
@test occursin("<p>done</p>", r)
end
11 changes: 11 additions & 0 deletions test/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,14 @@ end
m = match(r"\[done\s*(.*?)ms\]", r)
@test parse(Float64, m.captures[1]) 500
end


@testset "refstring" begin
@test J.refstring("aa bb") == "aa-bb"
@test J.refstring("aa bb !") == "aa-bb"
@test J.refstring("aa-bb-!") == "aa-bb-"
@test J.refstring("aa 🔺 bb") == "aa-bb"
@test J.refstring("aaa 0 bb s:2 df") == "aaa-0-bb-s2-df"
@test J.refstring("🔺🔺") == string(hash("🔺🔺"))
@test J.refstring("blah&#33;") == "blah"
end
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using JuDoc, Test
using JuDoc, Test, Markdown
const J = JuDoc
const D = joinpath(dirname(dirname(pathof(JuDoc))), "test", "_dummies")

Expand Down

0 comments on commit 8a4186a

Please sign in to comment.