Skip to content

Commit ab71621

Browse files
committed
Convert colons, slashes, and periods in anchors to underscores
1 parent 64d0db1 commit ab71621

File tree

6 files changed

+63
-22
lines changed

6 files changed

+63
-22
lines changed

src/expand_bibliography.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -462,9 +462,10 @@ end
462462

463463

464464
# Transform an arbitrary string `s` into a normalized string containing only
465-
# ASCII letters, numbers, and the symbols `_` and `-`, i.e., matching the regex
466-
# `r"^[A-Za-z0-9_-]+$"`. Letters with diacritics are normalized into their
467-
# ASCII equivalents, and all other characters are dropped.
465+
# ASCII letters, numbers, and the symbols `_` and `-`, i.e., matching the
466+
# regex `r"^[A-Za-z0-9._-]+$"`. Letters with diacritics are normalized into
467+
# their ASCII equivalents, `:` and `/` are converted to `_`, and all other
468+
# characters are dropped.
468469
function normalize_anchor(s::AbstractString)
469470
s_norm = Unicode.normalize(s, :NFKD) # decompose diacritics
470471
chars = Char[]
@@ -475,6 +476,8 @@ function normalize_anchor(s::AbstractString)
475476
c == '_' ||
476477
c == '-'
477478
push!(chars, c)
479+
elseif (c == ':') || (c == '/') || (c == '.')
480+
push!(chars, '_')
478481
end
479482
end
480483
return String(chars)

test/test_anchor_keys.jl

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,52 @@ using IOCapture: IOCapture
77

88
include("run_makedocs.jl")
99

10+
11+
@testset "Documenter.DOM anchors" begin
12+
Documenter.DOM.@tags div
13+
@test string(div["#anchor"]("")) == "<div id=\"anchor\"></div>"
14+
@test string(div["#anchor-suffix"]("")) == "<div id=\"anchor-suffix\"></div>"
15+
@test string(div["#anchor_suffix"]("")) == "<div id=\"anchor_suffix\"></div>"
16+
# The way that Documenter.DOM eats `:` and `.` is the reason we have to
17+
# strip those characters from citation keys. If that behavior ever changes,
18+
# we can reconsider.
19+
@test_broken string(div["#anchor:suffix"]("")) == "<div id=\"anchor:suffix\"></div>"
20+
@test_broken string(div["#anchor.suffix"]("")) == "<div id=\"anchor.suffix\"></div>"
21+
end
22+
23+
1024
@testset "anchor key ambiguity" begin
1125

1226
cache = Bijections.Bijection{String,String}()
1327

1428
anchor_key = get_anchor_key("AbsilMahonySepulchre:2008", cache)
15-
@test anchor_key == "AbsilMahonySepulchre2008"
29+
@test anchor_key == "AbsilMahonySepulchre_2008"
1630

1731
# cache hit
18-
@test cache("AbsilMahonySepulchre2008") == "AbsilMahonySepulchre:2008"
32+
@test cache("AbsilMahonySepulchre_2008") == "AbsilMahonySepulchre:2008"
1933
anchor_key = get_anchor_key("AbsilMahonySepulchre:2008", cache)
20-
@test anchor_key == "AbsilMahonySepulchre2008"
34+
@test anchor_key == "AbsilMahonySepulchre_2008"
2135

36+
# Invalid: starts with number
2237
anchor_key = get_anchor_key("2008_AbsilMahonySepulchre", cache)
2338
@test anchor_key == "cit-2008_AbsilMahonySepulchre"
2439

40+
# Ambiguous: periods are not allowed allowed (substituted with '_')
2541
c = IOCapture.capture(rethrow=Union{}) do
2642
get_anchor_key("AbsilMahonySepulchre.2008", cache)
2743
end
28-
@test c.value == "AbsilMahonySepulchre2008-2"
29-
msg = "Warning: HTML anchor for citation key \"AbsilMahonySepulchre.2008\" normalizes to ambiguous \"AbsilMahonySepulchre2008\" conflicting with citation key \"AbsilMahonySepulchre:2008\". Disambiguating with suffix \"-2\""
44+
@test c.value == "AbsilMahonySepulchre_2008-2"
45+
msg = "Warning: HTML anchor for citation key \"AbsilMahonySepulchre.2008\" normalizes to ambiguous \"AbsilMahonySepulchre_2008\" conflicting with citation key \"AbsilMahonySepulchre:2008\". Disambiguating with suffix \"-2\""
46+
@test contains(c.output, msg)
47+
48+
# Ambiguous key: `=` is not allowed (dropped)
49+
c = IOCapture.capture(rethrow=Union{}) do
50+
get_anchor_key("AbsilMahonySepulchre_=2008", cache)
51+
end
52+
@test c.value == "AbsilMahonySepulchre_2008-3"
53+
msg = "Warning: HTML anchor for citation key \"AbsilMahonySepulchre_=2008\" normalizes to ambiguous \"AbsilMahonySepulchre_2008\" conflicting with citation key \"AbsilMahonySepulchre:2008\". Disambiguating with suffix \"-2\""
54+
@test contains(c.output, msg)
55+
msg = "Warning: HTML anchor for citation key \"AbsilMahonySepulchre_=2008\" normalizes to ambiguous \"AbsilMahonySepulchre_2008-2\" conflicting with citation key \"AbsilMahonySepulchre.2008\". Disambiguating with suffix \"-3\""
3056
@test contains(c.output, msg)
3157

3258
end
@@ -52,19 +78,19 @@ end
5278

5379
@test success
5480

55-
@test bib.anchor_keys["Chirikjian:2012"] == "Chirikjian2012"
56-
@test bib.anchor_keys["Chirikjian2012"] == "Chirikjian2012-2"
81+
@test bib.anchor_keys["Chirikjian:2012"] == "Chirikjian_2012"
82+
@test bib.anchor_keys["Chirikjian_2012"] == "Chirikjian_2012-2"
5783

58-
@test contains(output, "normalizes to ambiguous \"Chirikjian2012\"")
84+
@test contains(output, "normalizes to ambiguous \"Chirikjian_2012\"")
5985

6086
#! format: off
6187
index_html = read(joinpath(dir, "build", "index.html"), String)
62-
@Test contains(index_html, "<a href=\"references/#Chirikjian2012\">")
63-
@Test contains(index_html, "<a href=\"references/#Chirikjian2012-2\">")
88+
@Test contains(index_html, "<a href=\"references/#Chirikjian_2012\">")
89+
@Test contains(index_html, "<a href=\"references/#Chirikjian_2012-2\">")
6490

6591
references_html = read(joinpath(dir, "build", "references", "index.html"), String)
66-
@Test contains(references_html, "<div id=\"Chirikjian2012\">")
67-
@Test contains(references_html, "<div id=\"Chirikjian2012-2\">")
92+
@Test contains(references_html, "<div id=\"Chirikjian_2012\">")
93+
@Test contains(references_html, "<div id=\"Chirikjian_2012-2\">")
6894
#! format: on
6995

7096
end

test/test_anchor_keys/src/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
You can read more about the theory of Lie groups for example in [Chirikjian:2012](@cite).
44

5-
Note the ambiguous citations keys, as for Ref. [Chirikjian2012](@cite) are automatically disambiguated.
5+
Note the ambiguous citations keys, as for Ref. [Chirikjian_2012](@cite) are automatically disambiguated.

test/test_anchor_keys/src/refs.bib

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ @book{Chirikjian:2012
280280

281281

282282
% duplicate with ambiguous key
283-
@book{Chirikjian2012,
283+
@book{Chirikjian_2012,
284284
edition = {1},
285285
series = {Applied and {Numerical} {Harmonic} {Analysis}},
286286
title = {Stochastic {Models}, {Information} {Theory}, and {Lie} {Groups}, {Volume} 2},

test/test_integration.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ using DocumenterCitations
22
using Documenter
33
using Bijections
44
using Test
5+
using TestingUtilities: @Test # much better at comparing strings
56

67
include("run_makedocs.jl")
78

@@ -109,7 +110,18 @@ end
109110
],
110111
check_success=true
111112
) do dir, result, success, backtrace, output
113+
112114
@test success
115+
116+
# ArXiV references were (unnoticed) broken prior to PR #95
117+
118+
references_html = read(joinpath(dir, "build", "references", "index.html"), String)
119+
@test !contains(references_html, "<div id=\"Wilhelm2003\">") # pre-#95
120+
@Test contains(references_html, "<div id=\"Wilhelm2003_10132\">")
121+
122+
syntax_html = read(joinpath(dir, "build", "syntax", "index.html"), String)
123+
@test contains(syntax_html, "<a href=\"../references/#Wilhelm2003_10132\">")
124+
113125
end
114126

115127
end

test/test_undefined_citations.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ include("run_makedocs.jl")
3636
index_html = read(index_html_file, String)
3737
@Test occursin("and a non-existing key [?]", index_html)
3838
@Test occursin("[?, ?, ?, <a href=\"references/#BrumerShapiro2003\">2</a>–<a href=\"references/#KochEPJQT2022\">6</a>, and references therein]", index_html)
39-
@Test occursin("<a href=\"references/#BrumerShapiro2003\">Brumer and Shapiro [2]</a>, <a href=\"references/#BrifNJP2010\">Brif <em>et al.</em> [3]</a>, [?], [?], <a href=\"references/#SolaAAMOP2018\">Sola <em>et al.</em> [4]</a>, [?], <a href=\"references/#Wilhelm200310132\">Wilhelm <em>et al.</em> [5]</a>, <a href=\"references/#KochEPJQT2022\">Koch <em>et al.</em> [6]</a>, and references therein", index_html)
39+
@Test occursin("<a href=\"references/#BrumerShapiro2003\">Brumer and Shapiro [2]</a>, <a href=\"references/#BrifNJP2010\">Brif <em>et al.</em> [3]</a>, [?], [?], <a href=\"references/#SolaAAMOP2018\">Sola <em>et al.</em> [4]</a>, [?], <a href=\"references/#Wilhelm2003_10132\">Wilhelm <em>et al.</em> [5]</a>, <a href=\"references/#KochEPJQT2022\">Koch <em>et al.</em> [6]</a>, and references therein", index_html)
4040
@Test occursin("Lastly, we cite a key [7]", index_html)
4141
#! format: on
4242
end
@@ -92,8 +92,8 @@ end
9292
#! format: off
9393
index_html = read(index_html_file, String)
9494
@Test occursin("and a non-existing key [?]", index_html)
95-
@Test occursin("[?], [?], <a href=\"references/#SolaAAMOP2018\">Sola <em>et al.</em> [SCMM18]</a>, [?], <a href=\"references/#Wilhelm200310132\">Wilhelm <em>et al.</em> [WKM+20]</a>, <a href=\"references/#KochEPJQT2022\">Koch <em>et al.</em> [KBC+22]</a>, and references therein", index_html)
96-
@Test occursin("<a href=\"references/#BrumerShapiro2003\">Brumer and Shapiro [BS03]</a>, <a href=\"references/#BrifNJP2010\">Brif <em>et al.</em> [BCR10]</a>, [?], [?], <a href=\"references/#SolaAAMOP2018\">Sola <em>et al.</em> [SCMM18]</a>, [?], <a href=\"references/#Wilhelm200310132\">Wilhelm <em>et al.</em> [WKM+20]</a>, <a href=\"references/#KochEPJQT2022\">Koch <em>et al.</em> [KBC+22]</a>, and references therein", index_html)
95+
@Test occursin("[?], [?], <a href=\"references/#SolaAAMOP2018\">Sola <em>et al.</em> [SCMM18]</a>, [?], <a href=\"references/#Wilhelm2003_10132\">Wilhelm <em>et al.</em> [WKM+20]</a>, <a href=\"references/#KochEPJQT2022\">Koch <em>et al.</em> [KBC+22]</a>, and references therein", index_html)
96+
@Test occursin("<a href=\"references/#BrumerShapiro2003\">Brumer and Shapiro [BS03]</a>, <a href=\"references/#BrifNJP2010\">Brif <em>et al.</em> [BCR10]</a>, [?], [?], <a href=\"references/#SolaAAMOP2018\">Sola <em>et al.</em> [SCMM18]</a>, [?], <a href=\"references/#Wilhelm2003_10132\">Wilhelm <em>et al.</em> [WKM+20]</a>, <a href=\"references/#KochEPJQT2022\">Koch <em>et al.</em> [KBC+22]</a>, and references therein", index_html)
9797
#! format: on
9898
end
9999

@@ -126,8 +126,8 @@ end
126126
#! format: off
127127
index_html = read(index_html_file, String)
128128
@Test occursin("and a non-existing key (???)", index_html)
129-
@Test occursin("(<a href=\"references/#BrumerShapiro2003\">Brumer and Shapiro, 2003</a>; <a href=\"references/#BrifNJP2010\">Brif <em>et al.</em>, 2010</a>; ???; ???; <a href=\"references/#SolaAAMOP2018\">Sola <em>et al.</em>, 2018</a>; ???; <a href=\"references/#Wilhelm200310132\">Wilhelm <em>et al.</em>, 2020</a>; <a href=\"references/#KochEPJQT2022\">Koch <em>et al.</em>, 2022</a>; and references therein)", index_html)
130-
@Test occursin("<a href=\"references/#BrumerShapiro2003\">Brumer and Shapiro (2003)</a>, <a href=\"references/#BrifNJP2010\">Brif <em>et al.</em> (2010)</a>, ???, ???, <a href=\"references/#SolaAAMOP2018\">Sola <em>et al.</em> (2018)</a>, ???, <a href=\"references/#Wilhelm200310132\">Wilhelm <em>et al.</em> (2020)</a>, <a href=\"references/#KochEPJQT2022\">Koch <em>et al.</em> (2022)</a>, and references therein", index_html)
129+
@Test occursin("(<a href=\"references/#BrumerShapiro2003\">Brumer and Shapiro, 2003</a>; <a href=\"references/#BrifNJP2010\">Brif <em>et al.</em>, 2010</a>; ???; ???; <a href=\"references/#SolaAAMOP2018\">Sola <em>et al.</em>, 2018</a>; ???; <a href=\"references/#Wilhelm2003_10132\">Wilhelm <em>et al.</em>, 2020</a>; <a href=\"references/#KochEPJQT2022\">Koch <em>et al.</em>, 2022</a>; and references therein)", index_html)
130+
@Test occursin("<a href=\"references/#BrumerShapiro2003\">Brumer and Shapiro (2003)</a>, <a href=\"references/#BrifNJP2010\">Brif <em>et al.</em> (2010)</a>, ???, ???, <a href=\"references/#SolaAAMOP2018\">Sola <em>et al.</em> (2018)</a>, ???, <a href=\"references/#Wilhelm2003_10132\">Wilhelm <em>et al.</em> (2020)</a>, <a href=\"references/#KochEPJQT2022\">Koch <em>et al.</em> (2022)</a>, and references therein", index_html)
131131
@Test occursin("Lastly, we cite a key (Tannor, 2007)", index_html)
132132
#! format: on
133133
end

0 commit comments

Comments
 (0)