chiark / gitweb /
m.htmlsanity: take inline language into account for hyphenation.
authorVladimír Vondruš <mosra@centrum.cz>
Mon, 11 Sep 2017 17:02:23 +0000 (19:02 +0200)
committerVladimír Vondruš <mosra@centrum.cz>
Thu, 14 Sep 2017 22:11:11 +0000 (00:11 +0200)
And also don't crash if given language is not supported.

pelican-plugins/m/htmlsanity.py

index 21aa04aefe8d9ad6485fcd511ff2c1d6e467811d..e57da75522c9d60a7b486caaa0a8810d4399b6be 100644 (file)
@@ -126,15 +126,6 @@ class Pyphen(Transform):
             if isinstance(node, (nodes.FixedTextElement, nodes.Special, nodes.field_name)):
                 continue
 
-            # Proper language-dependent hyphenation
-            lang = node.get_language_code(document_language)
-
-            # Create new Pyphen object for given lang, if not yet cached. I'm
-            # assuming this is faster than recreating the instance for every
-            # text node
-            if lang not in pyphen_for_lang:
-                pyphen_for_lang[lang] = pyphen.Pyphen(lang=lang)
-
             for txtnode in node.traverse(nodes.Text):
                 # Exclude:
                 #  - document title
@@ -155,6 +146,17 @@ class Pyphen(Transform):
                 # Useful for debugging, don't remove ;)
                 #print(repr(txtnode.parent), repr(txtnode.parent.parent), repr(txtnode.parent.parent.parent))
 
+                # Proper language-dependent hyphenation. Can't be done for
+                # `node` as a paragraph can consist of more than one language.
+                lang = txtnode.parent.get_language_code(document_language)
+
+                # Create new Pyphen object for given lang, if not yet cached.
+                # I'm assuming this is faster than recreating the instance for
+                # every text node
+                if lang not in pyphen_for_lang:
+                    if lang not in pyphen.LANGUAGES: continue
+                    pyphen_for_lang[lang] = pyphen.Pyphen(lang=lang)
+
                 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):