Skip to content

Commit

Permalink
Typogrify alt attributes in images when a DOM can be parsed
Browse files Browse the repository at this point in the history
  • Loading branch information
apasel422 committed Jun 24, 2024
1 parent c9e9908 commit b246b0a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
7 changes: 6 additions & 1 deletion se/commands/typogrify.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ def typogrify(plain_output: bool) -> int:

processed_xhtml = dom.to_string()
else:
processed_xhtml = se.typography.typogrify(xhtml, args.quotes)
for node in dom.xpath("/html/body//img[@alt]"):
node.set_attr("alt", se.typography.typogrify(node.get_attr("alt"), args.quotes))

processed_xhtml = dom.to_string()
# Word joiners and nbsp don't belong in alt but that is handled by typogrify itself
processed_xhtml = se.typography.typogrify(processed_xhtml, args.quotes)

# Tweak: Word joiners and nbsp don't go in the ToC
if dom.xpath("/html/body//nav[contains(@epub:type, 'toc')]"):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<h2 epub:type="title">A Dreary Story</h2>
<!-- Straight quotes converted to curly quotes -->
<p>content of “chapter 1”</p>
<img alt="“Typography’s great …”—you can quote me on that."/>
<!-- Appropriate quotes for ’tis or equivalent -->
<ul>
<li>’tis</li>
Expand Down
1 change: 1 addition & 0 deletions tests/draft_commands/typogrify/test-1/in/typogrify.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<h2 epub:type="title">A Dreary Story</h2>
<!-- Straight quotes converted to curly quotes -->
<p>content of "chapter 1"</p>
<img alt="&quot;Typography's great...&quot; -- you can quote me on that."/>
<!-- Appropriate quotes for ’tis or equivalent -->
<ul>
<li>'tis</li>
Expand Down

0 comments on commit b246b0a

Please sign in to comment.