chiark / gitweb /
m.htmlsanity: make hyphenation scope saner.
authorVladimír Vondruš <mosra@centrum.cz>
Tue, 12 Sep 2017 10:09:22 +0000 (12:09 +0200)
committerVladimír Vondruš <mosra@centrum.cz>
Thu, 14 Sep 2017 22:11:11 +0000 (00:11 +0200)
Article summary is now hyphenated, document title and other fields are
not. Also not hyphenating inline raw nodes (breaks math).

pelican-plugins/m/htmlsanity.py

index ed34c94172dba2feb443061d492c1a5eb131e78b..3f835491ea1b06f18bf6be32ec57d96bbae1529d 100644 (file)
@@ -141,11 +141,24 @@ class Pyphen(Transform):
 
             for txtnode in node.traverse(nodes.Text):
                 # Exclude:
+                #  - document title
                 #  - literals and spans inside literals
-                #  - field bodies (such as various :save_as: etc.)
-                if isinstance(txtnode.parent, nodes.literal) or isinstance(txtnode.parent.parent, nodes.literal) or isinstance(txtnode.parent.parent, nodes.field_body):
+                #  - raw code (such as SVG)
+                if isinstance(txtnode.parent, nodes.title) or \
+                   isinstance(txtnode.parent, nodes.literal) or \
+                   isinstance(txtnode.parent.parent, nodes.literal) or \
+                   isinstance(txtnode.parent, nodes.raw):
                     continue
 
+                # From fields include only the summary
+                if isinstance(txtnode.parent.parent, nodes.field_body):
+                    field_name_index = txtnode.parent.parent.parent.first_child_matching_class(nodes.field_name)
+                    if txtnode.parent.parent.parent[field_name_index][0] != 'summary':
+                        continue
+
+                # Useful for debugging, don't remove ;)
+                #print(repr(txtnode.parent), repr(txtnode.parent.parent), repr(txtnode.parent.parent.parent))
+
                 txtnode.parent.replace(txtnode, nodes.Text(words_re.sub(lambda m: pyphen_for_lang[lang].inserted(m.group(0), '\u00AD'), txtnode.astext())))
 
 class SaneHtmlTranslator(HTMLTranslator):