Skip to content

Commit 60e5618

Browse files
authored
Merge pull request #51 from oreillymedia/TOOLSDEV-71
Convert <span>s to <code>s inside <pre> blocks when keeping highlighting
2 parents ad479c3 + 8b1803d commit 60e5618

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

jupyter_book_to_htmlbook/code_processing.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,21 @@ def example_get_name_and_title_r(pre_block):
228228
"Missing first two line comments for uuid and title." +
229229
f"Unable to apply example formatting: {error}.")
230230
return None, None
231+
232+
233+
def pre_spans_to_code_tags(chapter):
234+
"""
235+
If we are preserving highlighting provided by Jupyter Book but want those
236+
styles to show up correctly in Atlas, we need to turn the <span> tags
237+
inside the Jupyter <pre> tags into <code> tags
238+
"""
239+
highlight_divs = chapter.find_all(class_="highlight")
240+
241+
for div in highlight_divs:
242+
pre_tag = div.pre
243+
if pre_tag:
244+
spans = pre_tag.find_all("span")
245+
for span in spans:
246+
span.name = "code"
247+
248+
return chapter

jupyter_book_to_htmlbook/file_processing.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
from .code_processing import (
1818
process_code,
1919
process_code_examples,
20-
process_inline_code
20+
process_inline_code,
21+
pre_spans_to_code_tags
2122
)
2223
from .text_processing import (
2324
clean_chapter,
@@ -254,6 +255,8 @@ def process_chapter(toc_element,
254255
chapter = process_code_examples(chapter)
255256
if not keep_highlighting:
256257
chapter = process_code(chapter, skip_cell_numbering)
258+
else:
259+
chapter = pre_spans_to_code_tags(chapter)
257260
chapter = process_inline_code(chapter)
258261
chapter = move_span_ids_to_sections(chapter)
259262
chapter = process_sidebars(chapter)

tests/test_code_processing.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import shutil
55
from bs4 import BeautifulSoup # type: ignore
66
from jupyter_book_to_htmlbook.code_processing import (
7+
pre_spans_to_code_tags,
78
process_code,
89
process_inline_code,
910
number_codeblock,
@@ -583,3 +584,21 @@ def test_unwrap_inline_spans_does_not_affect_pre(self,
583584
expected = str(code_example_data_type)
584585
result = process_inline_code(code_example_data_type)
585586
assert str(result) == expected
587+
588+
589+
class TestKeepHighlighting:
590+
"""
591+
Tests around preserving highlighting provided by Jupyter Book, but doing
592+
enough to ensure they appear correctly inside Atlas
593+
"""
594+
595+
def test_pre_spans_to_code_tags(self):
596+
"""
597+
Smoke test that we're correctly converting <span>s into <code>s inside
598+
pre tags with highlighting
599+
"""
600+
chapter = BeautifulSoup("""<div class="highlight"><pre>
601+
<span class="preserved">some code</span></pre></div>
602+
""", "html.parser")
603+
result = pre_spans_to_code_tags(chapter)
604+
assert result.find("pre").find("code") # type: ignore

tests/test_file_processing.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,9 @@ def test_keep_highlighting(self, tmp_book_path):
351351
"""
352352
We want to be able to optionally pass Jupyter's highlighting through,
353353
e.g., in cases where we're not explicitly providing language support
354+
355+
In this case, the <span> tags inside the <pre> block need to be
356+
converted into <code> tags so Atlas's css rules pick them up
354357
"""
355358
test_out = tmp_book_path / "output"
356359
test_out.mkdir()
@@ -363,4 +366,5 @@ def test_keep_highlighting(self, tmp_book_path):
363366

364367
assert text.find('data-type="programlisting"') == -1
365368
assert text.find('data-code-language="') == -1
366-
assert text.find('<span class="nb"') > 0
369+
# spans should be converted to code tags for highlighting in Atlas
370+
assert text.find('<code class="nb"') > 0

0 commit comments

Comments
 (0)