Skip to content

Commit

Permalink
fix(shyamd#173): prevent overwriting footnotes with namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
daylinmorgan committed Feb 14, 2023
1 parent a425c0e commit 5013b34
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 45 deletions.
4 changes: 2 additions & 2 deletions src/mkdocs_bibtex/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def format_citations(self, cite_keys):

# 4. Construct quads
quads = [
(cite_block, key, numbers[key], self.all_references[key])
(cite_block, key, f"{numbers[key]}-citation", self.all_references[key])
for cite_block, key in pairs
]

Expand All @@ -204,7 +204,7 @@ def full_bibliography(self):

bibliography = []
for number, (key, citation) in enumerate(self.all_references.items()):
bibliography_text = "[^{}]: {}".format(number, citation)
bibliography_text = "[^{}-citation]: {}".format(number, citation)
bibliography.append(bibliography_text)

return "\n".join(bibliography)
86 changes: 43 additions & 43 deletions test_files/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ def test_format_citations(plugin):
assert (
"[@test]",
"test",
"1",
"1-citation",
"First Author and Second Author. Test title. *Testing Journal*, 2019.",
) == plugin.format_citations(["[@test]"])[0]

assert (
"[@test2]",
"test2",
"1",
"1-citation",
"First Author and Second Author. Test Title (TT). *Testing Journal (TJ)*, 2019.",
) == plugin.format_citations(["[@test2]"])[0]

Expand All @@ -83,13 +83,13 @@ def test_format_citations(plugin):
(
"[@test; @test2]",
"test",
"1",
"1-citation",
"First Author and Second Author. Test title. *Testing Journal*, 2019.",
),
(
"[@test; @test2]",
"test2",
"2",
"2-citation",
"First Author and Second Author. Test Title (TT). *Testing Journal (TJ)*, 2019.",
),
] == plugin.format_citations(["[@test; @test2]"])
Expand All @@ -98,15 +98,15 @@ def test_format_citations(plugin):
assert (
"[@Bivort2016]",
"Bivort2016",
"1",
"1-citation",
"Benjamin L. De Bivort and Bruno Van Swinderen. Evidence for selective attention in the insect brain. *Current Opinion in Insect Science*, 15:1–7, 2016. [doi:10.1016/j.cois.2016.02.007](https://doi.org/10.1016/j.cois.2016.02.007).", # noqa: E501
) == plugin.format_citations(["[@Bivort2016]"])[0]

# Test \url embedding
assert (
"[@test_citavi]",
"test_citavi",
"1",
"1-citation",
"First Author and Second Author. Test Title (TT). *Testing Journal (TJ)*, 2019. URL: [\\\\url\\{https://doi.org/10.21577/0103\\-5053.20190253\\}](\\url{https://doi.org/10.21577/0103-5053.20190253}).", # noqa: E501
) == plugin.format_citations(["[@test_citavi]"])[0]

Expand All @@ -115,14 +115,14 @@ def test_format_citations(plugin):
assert (
"[@test]",
"test",
"1",
"1-citation",
"First Author and Second Author. Test title. *Testing Journal*, 2019.",
) == plugin.format_citations(["[@test]"])[0]

assert (
"[@Bivort2016]",
"Bivort2016",
"1",
"1-citation",
"Benjamin L. De Bivort and Bruno Van Swinderen. Evidence for selective attention in the insect brain. *Current Opinion in Insect Science*, 15:1–7, 2016. [doi:10.1016/j.cois.2016.02.007](https://doi.org/10.1016/j.cois.2016.02.007).", # noqa: E501
) == plugin.format_citations(["[@Bivort2016]"])[0]

Expand All @@ -131,14 +131,14 @@ def test_format_citations(plugin):
assert (
"[@test]",
"test",
"1",
"1-citation",
"First Author and Second Author. Test title. *Testing Journal*, 2019.",
) == plugin.format_citations(["[@test]"])[0]

assert (
"[@test_citavi]",
"test_citavi",
"1",
"1-citation",
"First Author and Second Author. Test Title (TT). *Testing Journal (TJ)*, 2019. URL: [\\\\url\\{https://doi.org/10.21577/0103\\-5053.20190253\\}](\\url{https://doi.org/10.21577/0103-5053.20190253}).", # noqa: E501
) == plugin.format_citations(["[@test_citavi]"])[0]

Expand Down Expand Up @@ -170,13 +170,13 @@ def test_insert_citation_keys():
(
"[@test]",
"@test",
"1",
"1-citation",
"First Author and Second Author",
)
],
"[@test]",
)
== "[^1]"
== "[^1-citation]"
)

assert (
Expand All @@ -185,19 +185,19 @@ def test_insert_citation_keys():
(
"[@test; @test2]",
"@test",
"1",
"1-citation",
"First Author and Second Author",
),
(
"[@test; @test2]",
"@test2",
"2",
"2-citation",
"First Author and Second Author. Test Title (TT). *Testing Journal (TJ)*, 2019", # noqa: E501
),
],
"[@test; @test2]",
)
== "[^1][^2]"
== "[^1-citation][^2-citation]"
)


Expand All @@ -206,22 +206,22 @@ def test_format_bibliography():
(
"[@test; @test2]",
"@test",
"1",
"1-citation",
"First Author and Second Author",
),
(
"[@test; @test2]",
"@test2",
"2",
"2-citation",
"First Author and Second Author. Test Title (TT). *Testing Journal (TJ)*, 2019",
),
]

bib = format_bibliography(quads)

assert "[^1]: First Author and Second Author" in bib
assert "[^1-citation]: First Author and Second Author" in bib
assert (
"[^2]: First Author and Second Author. Test Title (TT). *Testing Journal (TJ)*, 2019"
"[^2-citation]: First Author and Second Author. Test Title (TT). *Testing Journal (TJ)*, 2019"
in bib
)

Expand Down Expand Up @@ -266,20 +266,20 @@ def test_on_page_markdown(plugin):
test_markdown = "This is a citation. [@test]\n\n \\bibliography"

assert (
"[^1]: First Author and Second Author. Test title. *Testing Journal*, 2019."
"[^1-citation]: First Author and Second Author. Test title. *Testing Journal*, 2019."
in plugin.on_page_markdown(test_markdown, None, None, None)
)

# ensure there are two items in bibliography
test_markdown = "This is a citation. [@test2] This is another citation [@test]\n\n \\bibliography"

assert "[^2]:" in plugin.on_page_markdown(test_markdown, None, None, None)
assert "[^2-citation]:" in plugin.on_page_markdown(test_markdown, None, None, None)

# ensure bib_by_default is working
plugin.config["bib_by_default"] = True
test_markdown = "This is a citation. [@test]"

assert "[^1]:" in plugin.on_page_markdown(test_markdown, None, None, None)
assert "[^1-citation]:" in plugin.on_page_markdown(test_markdown, None, None, None)
plugin.config["bib_by_default"] = False

# ensure nonexistant citekeys are removed correctly (not replaced)
Expand All @@ -292,12 +292,12 @@ def test_on_page_markdown(plugin):
# Ensure if an item is referenced multiple times, it only shows up as one reference
test_markdown = "This is a citation. [@test] This is another citation [@test]\n\n \\bibliography"

assert "[^2]" not in plugin.on_page_markdown(test_markdown, None, None, None)
assert "[^2-citation]" not in plugin.on_page_markdown(test_markdown, None, None, None)

# Ensure item only shows up once even if used in multiple places as both a compound and lone cite key
test_markdown = "This is a citation. [@test; @test2] This is another citation [@test]\n\n \\bibliography"

assert "[^3]" not in plugin.on_page_markdown(test_markdown, None, None, None)
assert "[^3-citation]" not in plugin.on_page_markdown(test_markdown, None, None, None)


def test_inline_citations(plugin):
Expand All @@ -323,74 +323,74 @@ def test_inline_citations(plugin):
)

# Ensure suppressed authors works
quads = [("[-@test]", None, "1", None)]
quads = [("[-@test]", None, "1-citation", None)]
test_markdown = "Suppressed [-@test]"
result = "Suppressed (2019)[^1]"
result = "Suppressed (2019)[^1-citation]"
assert result == insert_citation_keys(
quads, test_markdown, plugin.csl_file, plugin.bib_data.to_string("bibtex")
)

# Ensure affixes work
quads = [("[see @test]", None, "1", None)]
quads = [("[see @test]", None, "1-citation", None)]
test_markdown = "Hello[see @test]"
result = "Hello (see Author and Author 2019)[^1]"
result = "Hello (see Author and Author 2019)[^1-citation]"
assert result == insert_citation_keys(
quads, test_markdown, plugin.csl_file, plugin.bib_data.to_string("bibtex")
)

quads = [("[@test, p. 123]", None, "1", None)]
quads = [("[@test, p. 123]", None, "1-citation", None)]
test_markdown = "[@test, p. 123]"
result = " (Author and Author 2019, p. 123)[^1]"
result = " (Author and Author 2019, p. 123)[^1-citation]"
assert result == insert_citation_keys(
quads, test_markdown, plugin.csl_file, plugin.bib_data.to_string("bibtex")
)

# Combined
quads = [("[see @test, p. 123]", None, "1", None)]
quads = [("[see @test, p. 123]", None, "1-citation", None)]
test_markdown = "Hello[see @test, p. 123]"
result = "Hello (see Author and Author 2019, p. 123)[^1]"
result = "Hello (see Author and Author 2019, p. 123)[^1-citation]"
assert result == insert_citation_keys(
quads, test_markdown, plugin.csl_file, plugin.bib_data.to_string("bibtex")
)

# Combined, suppressed author
quads = [("[see -@test, p. 123]", None, "1", None)]
quads = [("[see -@test, p. 123]", None, "1-citation", None)]
test_markdown = "Suppressed [see -@test, p. 123]"
result = "Suppressed (see 2019, p. 123)[^1]"
result = "Suppressed (see 2019, p. 123)[^1-citation]"
assert result == insert_citation_keys(
quads, test_markdown, plugin.csl_file, plugin.bib_data.to_string("bibtex")
)

# Ensure multi references work
quads = [
("[@test; @Bivort2016]", None, "1", None),
("[@test; @Bivort2016]", None, "2", None),
("[@test; @Bivort2016]", None, "1-citation", None),
("[@test; @Bivort2016]", None, "2-citation", None),
]
test_markdown = "[@test; @Bivort2016]"
# CSL defines the order, this ordering is therefore expected with springer.csl
result = " (De Bivort and Van Swinderen 2016; Author and Author 2019)[^1][^2]"
result = " (De Bivort and Van Swinderen 2016; Author and Author 2019)[^1-citation][^2-citation]"
assert result == insert_citation_keys(
quads, test_markdown, plugin.csl_file, plugin.bib_data.to_string("bibtex")
)

quads = [
("[@test, p. 12; @Bivort2016, p. 15]", None, "1", None),
("[@test, p. 12; @Bivort2016, p. 15]", None, "2", None),
("[@test, p. 12; @Bivort2016, p. 15]", None, "1-citation", None),
("[@test, p. 12; @Bivort2016, p. 15]", None, "2-citation", None),
]
test_markdown = "[@test, p. 12; @Bivort2016, p. 15]"
# CSL defines the order, this ordering is therefore expected with springer.csl
result = " (De Bivort and Van Swinderen 2016, p. 15; Author and Author 2019, p. 12)[^1][^2]"
result = " (De Bivort and Van Swinderen 2016, p. 15; Author and Author 2019, p. 12)[^1-citation][^2-citation]"
assert result == insert_citation_keys(
quads, test_markdown, plugin.csl_file, plugin.bib_data.to_string("bibtex")
)

# Ensure multiple inline references works
quads = [
("[@test]", None, "1", None),
("[see @Bivort2016, p. 123]", None, "2", None),
("[@test]", None, "1-citation", None),
("[see @Bivort2016, p. 123]", None, "2-citation", None),
]
test_markdown = "Hello[@test] World [see @Bivort2016, p. 123]"
result = "Hello (Author and Author 2019)[^1] World (see De Bivort and Van Swinderen 2016, p. 123)[^2]"
result = "Hello (Author and Author 2019)[^1-citation] World (see De Bivort and Van Swinderen 2016, p. 123)[^2-citation]"
assert result == insert_citation_keys(
quads, test_markdown, plugin.csl_file, plugin.bib_data.to_string("bibtex")
)

0 comments on commit 5013b34

Please sign in to comment.