Skip to content

Commit

Permalink
Merge pull request #54 from oreillymedia/allow-svg-styles
Browse files Browse the repository at this point in the history
Allow styles on SVG elements
  • Loading branch information
delfanbaum authored Sep 6, 2023
2 parents 330d3b8 + 5eaf5c0 commit aeedb82
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ Options:

## Release Notes

### 1.1.2

Bug fix:
- Allow "style" attributes to remain inside SVGs

### 1.1.1

Bug fix:
Expand Down
13 changes: 12 additions & 1 deletion jupyter_book_to_htmlbook/text_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,18 @@ def clean_chapter(chapter, rm_numbering=True):

for attr in remove_attrs:
for tag in chapter.find_all(attrs={attr: True}):
del tag[attr]
# we need to allow styles on svg elements
in_svg = False
if attr == "style":

if tag.name == "svg":
in_svg = True

for parent in tag.parents:
if parent.name == "svg":
in_svg = True
if not in_svg:
del tag[attr]

# (optionally) remove numbering
if rm_numbering:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "jupyter-book-to-htmlbook"
version = "1.1.1"
version = "1.1.2"
description = "A script to convert jupyter book html files to htmlbook for consumption in Atlas"
authors = ["delfanbaum"]

Expand Down
22 changes: 22 additions & 0 deletions tests/test_text_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,25 @@ def test_hidden_output_is_removed():
clean_chapter(chapter_text, False)
assert not chapter_text.find("details")
assert not chapter_text.find("div", class_="output")


def test_svg_retains_attrs():
"""
This is to get around styles applied to SVGs, which seems like
standard practice, for better or worse.
"""
svg_ch = BeautifulSoup("""
<div class="cell_output docutils container">
<div class="output text_html">
<svg width="250" height="150" style="color:blue">
<rect width="100%" height="100%" fill="white"/>
<line x1="125" y1="75" x2="225.0" y2="75.0" stroke-linecap="round" style="stroke:#663399;stroke-width:2"/>
<g visibility="visible" transform="rotate(-90,225.0,75.0) translate(225.0, 75.0)">
<circle stroke="gray" stroke-width="2" fill="transparent" r="5.5" cx="0" cy="0"/>
<polygon points="0,12 2,9 -2,9" style="fill:gray;stroke:gray;stroke-width:2"/>
</g>
</svg>
</div></div>""", "html.parser")
clean_chapter(svg_ch, False)
assert "stroke" in svg_ch.find("line").get('style') # type:ignore
assert "blue" in svg_ch.find("svg").get('style') # type:ignore

0 comments on commit aeedb82

Please sign in to comment.