From 3c5b3bdfbf5eb273c8266f681603fd853cc838e0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Tue, 12 Sep 2017 12:09:22 +0200 Subject: [PATCH] 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). --- pelican-plugins/m/htmlsanity.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) 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): -- 2.30.2