Skip to content

Commit aeedb82

Browse files
authored
Merge pull request #54 from oreillymedia/allow-svg-styles
Allow styles on SVG elements
2 parents 330d3b8 + 5eaf5c0 commit aeedb82

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ Options:
7474

7575
## Release Notes
7676

77+
### 1.1.2
78+
79+
Bug fix:
80+
- Allow "style" attributes to remain inside SVGs
81+
7782
### 1.1.1
7883

7984
Bug fix:

jupyter_book_to_htmlbook/text_processing.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,18 @@ def clean_chapter(chapter, rm_numbering=True):
1818

1919
for attr in remove_attrs:
2020
for tag in chapter.find_all(attrs={attr: True}):
21-
del tag[attr]
21+
# we need to allow styles on svg elements
22+
in_svg = False
23+
if attr == "style":
24+
25+
if tag.name == "svg":
26+
in_svg = True
27+
28+
for parent in tag.parents:
29+
if parent.name == "svg":
30+
in_svg = True
31+
if not in_svg:
32+
del tag[attr]
2233

2334
# (optionally) remove numbering
2435
if rm_numbering:

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "jupyter-book-to-htmlbook"
3-
version = "1.1.1"
3+
version = "1.1.2"
44
description = "A script to convert jupyter book html files to htmlbook for consumption in Atlas"
55
authors = ["delfanbaum"]
66

tests/test_text_processing.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,25 @@ def test_hidden_output_is_removed():
206206
clean_chapter(chapter_text, False)
207207
assert not chapter_text.find("details")
208208
assert not chapter_text.find("div", class_="output")
209+
210+
211+
def test_svg_retains_attrs():
212+
"""
213+
This is to get around styles applied to SVGs, which seems like
214+
standard practice, for better or worse.
215+
"""
216+
svg_ch = BeautifulSoup("""
217+
<div class="cell_output docutils container">
218+
<div class="output text_html">
219+
<svg width="250" height="150" style="color:blue">
220+
<rect width="100%" height="100%" fill="white"/>
221+
<line x1="125" y1="75" x2="225.0" y2="75.0" stroke-linecap="round" style="stroke:#663399;stroke-width:2"/>
222+
<g visibility="visible" transform="rotate(-90,225.0,75.0) translate(225.0, 75.0)">
223+
<circle stroke="gray" stroke-width="2" fill="transparent" r="5.5" cx="0" cy="0"/>
224+
<polygon points="0,12 2,9 -2,9" style="fill:gray;stroke:gray;stroke-width:2"/>
225+
</g>
226+
</svg>
227+
</div></div>""", "html.parser")
228+
clean_chapter(svg_ch, False)
229+
assert "stroke" in svg_ch.find("line").get('style') # type:ignore
230+
assert "blue" in svg_ch.find("svg").get('style') # type:ignore

0 commit comments

Comments
 (0)