File tree Expand file tree Collapse file tree 2 files changed +46
-0
lines changed Expand file tree Collapse file tree 2 files changed +46
-0
lines changed Original file line number Diff line number Diff line change @@ -60,6 +60,25 @@ def process_code(chapter, skip_numbering=False):
60
60
return chapter
61
61
62
62
63
+ def process_inline_code (chapter ):
64
+ """
65
+ Because the platform occasionally has unexpected class styling, we want to
66
+ make sure that inline code in particular is clean. Note that the current
67
+ target version of Jupyter Book doesn't put <code> inside <pre> tags, so our
68
+ "dumb" inline searcher should work. There is a test to confirm this.
69
+
70
+ NOTE: This is a temporary fix; we should really clean out all classes
71
+ except those we explicitly want from the chapter files
72
+ """
73
+ inline_codes = chapter .find_all ("code" )
74
+
75
+ for code in inline_codes :
76
+ if code .find ("span" ):
77
+ code .span .unwrap ()
78
+
79
+ return chapter
80
+
81
+
63
82
def number_codeblock (pre_block , cell_number ):
64
83
"""
65
84
Adds numbering markers (`In [##]: ` or `Out[##]: `) to cell blocks
Original file line number Diff line number Diff line change 5
5
from bs4 import BeautifulSoup # type: ignore
6
6
from jupyter_book_to_htmlbook .code_processing import (
7
7
process_code ,
8
+ process_inline_code ,
8
9
number_codeblock ,
9
10
process_code_examples
10
11
)
@@ -557,3 +558,29 @@ def test_examples_malformed_r(self, caplog):
557
558
assert not example_div .find ("h5" , string = "A formal R example" )
558
559
assert result == malformed_example
559
560
assert "Missing first two line comments for" in log
561
+
562
+
563
+ class TestInlineCode :
564
+ """
565
+ Smoke tests around the translation of inline code
566
+ """
567
+
568
+ def test_unwrap_inline_spans (self ):
569
+ """
570
+ We should not allow spans inside inline code
571
+ """
572
+
573
+ html = BeautifulSoup ("""<p>Some text with
574
+ <code><span class="pre">code</span></code>.</p>""" ,
575
+ "html.parser" )
576
+ result = process_inline_code (html )
577
+
578
+ assert not result .find ("span" )
579
+ assert str (result .find ("code" )) == "<code>code</code>"
580
+
581
+ def test_unwrap_inline_spans_does_not_affect_pre (self ,
582
+ code_example_data_type ):
583
+ expected = str (code_example_data_type )
584
+ result = process_inline_code (code_example_data_type )
585
+ assert str (result ) == expected
586
+
You can’t perform that action at this time.
0 commit comments