From: Vladimír Vondruš Date: Tue, 12 Sep 2017 10:09:22 +0000 (+0200) Subject: m.htmlsanity: make hyphenation scope saner. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~cjwatson/git?a=commitdiff_plain;h=3c5b3bdfbf5eb273c8266f681603fd853cc838e0;p=blog.git m.htmlsanity: make hyphenation scope saner. Article summary is now hyphenated, document title and other fields are not. Also not hyphenating inline raw nodes (breaks math). --- diff --git a/pelican-plugins/m/htmlsanity.py b/pelican-plugins/m/htmlsanity.py index ed34c941..3f835491 100644 --- a/pelican-plugins/m/htmlsanity.py +++ b/pelican-plugins/m/htmlsanity.py @@ -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):