1
1
import logging
2
+ import shutil
2
3
from bs4 import BeautifulSoup # type: ignore
3
- from jupyter_book_to_htmlbook .reference_processing import process_interal_refs
4
+ from jupyter_book_to_htmlbook .file_processing import process_chapter
5
+ from jupyter_book_to_htmlbook .reference_processing import (
6
+ process_internal_refs ,
7
+ process_remaining_refs
8
+ )
4
9
5
10
6
11
class TestInternalRefs :
@@ -11,7 +16,7 @@ def test_process_internal_refs_reg_xrefs(self):
11
16
chapter_text = """<a class="reference internal" href="example.html">
12
17
cross reference text</a>"""
13
18
chapter = BeautifulSoup (chapter_text , 'html.parser' )
14
- result = process_interal_refs (chapter )
19
+ result = process_internal_refs (chapter )
15
20
assert str (result ) == '<a class="reference internal" data-type=' + \
16
21
'"xref" href="#example.html">#example.html</a>'
17
22
@@ -29,7 +34,7 @@ def test_process_internal_refs_bibliograpy(self):
29
34
title="Terry Carver...">Carver, 1993</a>]</span>.</p>
30
35
"""
31
36
chapter = BeautifulSoup (text , 'html.parser' )
32
- result = process_interal_refs (chapter )
37
+ result = process_internal_refs (chapter )
33
38
assert not result .find ("a" )
34
39
assert "(Baruch 1993)" in result .find ("span" ).contents
35
40
@@ -42,7 +47,72 @@ def test_alert_on_external_images(self, caplog):
42
47
href="http://example.com/example.png"><img alt="example"
43
48
src="http://example.com/example.png" style="width:100px" /></a>"""
44
49
chapter = BeautifulSoup (chapter_text , 'html.parser' )
45
- result = process_interal_refs (chapter )
50
+ result = process_internal_refs (chapter )
46
51
assert result == chapter
47
52
caplog .set_level (logging .DEBUG )
48
53
assert "External image reference:" in caplog .text
54
+
55
+
56
+ class TestStandardRefs :
57
+ """
58
+ Tests around "std-ref" references, which appear as spans (in the case
59
+ where Jupyter Book can't find the actual reference).
60
+ """
61
+ def test_process_xref_spans (self ):
62
+ """
63
+ It appears that when an xref doesn't have a target jupyter knows about
64
+ (e.g., in the case of examples), it puts them into spans. We should
65
+ check for these and then convert them appropriately.
66
+ """
67
+ chapter = BeautifulSoup ("""<p>And here follows a formal code example
68
+ (<span class="xref std std-ref">code_example</span>).
69
+ Note that the cell has an “example” tag added to its metadata.</p>""" ,
70
+ "html.parser" )
71
+ result = process_remaining_refs (chapter )
72
+ xref = result .find ("a" , class_ = "xref" )
73
+ assert xref
74
+ assert xref .get ('data-type' ) == "xref"
75
+ assert xref .get ('href' ) == "#code_example"
76
+ assert xref .string == "#code_example"
77
+
78
+ def test_process_xref_spans_bad_ref (self , caplog ):
79
+ """
80
+ In the unlikely case wherein we get a bad xref (i.e., one with
81
+ spaces or code in it), we log that failure and do nothing
82
+ """
83
+ chapter = BeautifulSoup ("""<p>And here follows a formal code example
84
+ (<span class="xref std std-ref">code example</span>). Another is
85
+ <span class="xref std std-ref"><span>some_</span><em>code_example</em></span>.
86
+ Note that the cell has an “example” tag added to its metadata.</p>""" ,
87
+ "html.parser" )
88
+ process_remaining_refs (chapter )
89
+ caplog .set_level (logging .DEBUG )
90
+ log = caplog .text
91
+ assert "Failed to apply" in log
92
+ assert "code example" in log
93
+ assert "<em>code_example</em>" in log
94
+
95
+ def test_examples_refs_in_chapter_processing (self , tmp_path ):
96
+ """
97
+ More an integration test, ensuring that when we process a chapter
98
+ the examples are data-typed as such, and that they still get their
99
+ highlighting
100
+ """
101
+ test_env = tmp_path / 'tmp'
102
+ test_out = test_env / 'output'
103
+ test_env .mkdir ()
104
+ test_out .mkdir ()
105
+ shutil .copytree ('tests/example_book/_build/html/notebooks' ,
106
+ test_env , dirs_exist_ok = True )
107
+
108
+ process_chapter (test_env / "code_py.html" ,
109
+ test_env , test_out )
110
+ with open (test_out / 'code_py.html' ) as f :
111
+ soup = BeautifulSoup (f .read (), "html.parser" )
112
+
113
+ xref = soup .find ("a" , class_ = "xref" )
114
+ assert xref
115
+ assert xref .get ("href" ) == "#hello_tim"
116
+ assert xref .get ("data-type" ) == "xref"
117
+ assert xref .get ("href" ) == "#hello_tim"
118
+ assert xref .string == "#hello_tim"
0 commit comments