Skip to content

Commit 122eee1

Browse files
committed
Bug fix for platform surprise class styling
One of the PEs discovered that having a `pre` class on a span inside a `code` tag displays oddly on the platform. This is a "quick fix" to solve for this, while the actual fix -- cleaning out all classes save those we explicitly want -- will come as a part of TOOLSDEV-60. Update: typo fix
1 parent ad32732 commit 122eee1

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

jupyter_book_to_htmlbook/code_processing.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,25 @@ def process_code(chapter, skip_numbering=False):
6060
return chapter
6161

6262

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+
6382
def number_codeblock(pre_block, cell_number):
6483
"""
6584
Adds numbering markers (`In [##]: ` or `Out[##]: `) to cell blocks

tests/test_code_processing.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from bs4 import BeautifulSoup # type: ignore
66
from jupyter_book_to_htmlbook.code_processing import (
77
process_code,
8+
process_inline_code,
89
number_codeblock,
910
process_code_examples
1011
)
@@ -557,3 +558,29 @@ def test_examples_malformed_r(self, caplog):
557558
assert not example_div.find("h5", string="A formal R example")
558559
assert result == malformed_example
559560
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+

0 commit comments

Comments
 (0)