Skip to content

Commit 25d9fbb

Browse files
committed
Update citation handling to reflect new bibliography formats
Luckily, most of the code to handle this was already written, however we were targeting the <=0.11 version of Jupyter Book's way of handling cross references so now we're up-to-date. Basically we check if it's a bracketed reference (which appears to be always and only the case for bibliographical citations as far as I can tell) and handle it like we used to from there. This commit updated the test for this and the code to pass it. Update: made the if-check for it being a citation better.
1 parent 4476522 commit 25d9fbb

File tree

3 files changed

+21
-20
lines changed

3 files changed

+21
-20
lines changed

jupyter_book_to_htmlbook/reference_processing.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,21 @@ def process_interal_refs(chapter):
1010
references to valid htmlbook xrefs. Currently opinionated towards CMS
1111
author-date.
1212
"""
13-
xrefs = chapter.find_all(class_='internal')
13+
xrefs = chapter.find_all("a", class_='internal')
1414
for ref in xrefs:
15-
# handle bib references, be opinionated!
16-
if ref['href'].find('references.html') > -1:
15+
# handle bib references
16+
if (
17+
ref.parent.name == "span" and
18+
ref.parent.contents[0] == "[" and
19+
ref.parent.contents[-1] == "]"
20+
):
1721
ref.name = 'span'
1822
del ref['href']
1923
# remove any internal tags
2024
inner_str = ''
2125
for part in ref.contents:
2226
inner_str += part.string
23-
# remove last comma per CMS
27+
# remove last comma (before year/date) per CMS
2428
inner_str = ','.join(inner_str.split(',')[0:-1]) + \
2529
inner_str.split(',')[-1]
2630
ref.string = f'({inner_str})'

tests/test_bibliography.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,15 @@
66
)
77
from jupyter_book_to_htmlbook.reference_processing import process_citations
88

9-
"""
10-
[[bibliography]]
11-
[appendix]
12-
[role="bibliography"]
13-
== Bibliography
14-
15-
Bibliography content starts here.
16-
"""
17-
189

1910
class TestBibliography:
2011
"""
2112
Tests targeting "bibliography" content included in the book, including:
2213
* End of book "bibliography" appendix
2314
* End-of-file / chapter bibliographies
15+
16+
NOTE: Tests against citations/references should be found in the
17+
test_references.py file.
2418
"""
2519

2620
def test_bib_appx_datatype(self, tmp_path):

tests/test_reference_processing.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,18 @@ def test_process_internal_refs_bibliograpy(self):
2020
Should convert bib refs to spans containing CMS author-date style
2121
citations
2222
"""
23-
text = '<p>For a more detailed breakdown, see ' + \
24-
'<span id="1">[<a class="reference internal" href="../../' + \
25-
'references.html#id35">Leek and Peng, 2015</a>]</span>.</p>' + \
26-
'<p>In the next section, we’ll talk about...</p>'
23+
text = """
24+
<p>Finally, here is a citation <span id="id1">[<a class="reference internal"
25+
href="#id6" title="Terry Baruch...">Baruch, 1993</a>]</span>.
26+
And then some others <span id="id2">[<a class="reference internal"
27+
href="#id5" title="Terry Aadams...">Aadams, 1993</a>]</span>,
28+
<span id="id3">[<a class="reference internal" href="#id7"
29+
title="Terry Carver...">Carver, 1993</a>]</span>.</p>
30+
"""
2731
chapter = BeautifulSoup(text, 'html.parser')
2832
result = process_interal_refs(chapter)
29-
assert str(result) == ("<p>For a more detailed breakdown, see <span>" +
30-
"(Leek and Peng 2015)</span>.</p><p>In the " +
31-
"next section, we’ll talk about...</p>")
33+
assert not result.find("a")
34+
assert "(Baruch 1993)" in result.find("span").contents
3235

3336
def test_alert_on_external_images(self, caplog):
3437
"""

0 commit comments

Comments
 (0)