From 8a4186a5b5bffbfed254f5e7f59d3aeb14394452 Mon Sep 17 00:00:00 2001 From: Thibaut Lienart Date: Wed, 29 May 2019 17:02:33 +1000 Subject: [PATCH] closes #130 --- src/converter/lx.jl | 2 +- src/converter/md_utils.jl | 7 ++++--- src/misc_utils.jl | 12 ++++++++++-- test/converter/markdown.jl | 15 +++++++++++++++ test/misc.jl | 11 +++++++++++ test/runtests.jl | 2 +- 6 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/converter/lx.jl b/src/converter/lx.jl index 1ca9ac32e..5fb43a87a 100644 --- a/src/converter/lx.jl +++ b/src/converter/lx.jl @@ -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) diff --git a/src/converter/md_utils.jl b/src/converter/md_utils.jl index 9004695ac..14a09ee32 100644 --- a/src/converter/md_utils.jl +++ b/src/converter/md_utils.jl @@ -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

...

\n which we might not want stripp || return partial @@ -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) write(io, subs(h, head:m.offset-1)) level = m.captures[1] name = m.captures[2] - write(io, "$(name)") + ref = refstring(name) + write(io, "$name") head = m.offset + lastindex(m.match) end write(io, subs(h, head:lastindex(h))) diff --git a/src/misc_utils.jl b/src/misc_utils.jl index c616a6828..74cb9c370 100644 --- a/src/misc_utils.jl +++ b/src/misc_utils.jl @@ -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 diff --git a/test/converter/markdown.jl b/test/converter/markdown.jl index 4d7ba7f34..752ff4064 100644 --- a/test/converter/markdown.jl +++ b/test/converter/markdown.jl @@ -111,3 +111,18 @@ end hstring = J.convert_inter_html(inter_html, blocks2insert, lxcontext) @test hstring == "

text A1 text A2 blah and \nescape B1\n text C1 \\(\\mathrm{ b}\\) text C2 then part1: AA and part2: BB.

\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("

Title

", r) + @test occursin("

Subtitle cool!

", r) + @test occursin("

done

", r) +end diff --git a/test/misc.jl b/test/misc.jl index ef5856882..d1eb2a263 100644 --- a/test/misc.jl +++ b/test/misc.jl @@ -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!") == "blah" +end diff --git a/test/runtests.jl b/test/runtests.jl index 276a7477b..5eaab2c79 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,4 +1,4 @@ -using JuDoc, Test +using JuDoc, Test, Markdown const J = JuDoc const D = joinpath(dirname(dirname(pathof(JuDoc))), "test", "_dummies")