From 49a77ad9d5593817556ee69933f5938c861c7025 Mon Sep 17 00:00:00 2001 From: Jeremy Maitin-Shepard Date: Thu, 10 Mar 2022 18:57:17 -0800 Subject: [PATCH] Factor out common logic from HTMLTranslator and HTML5Translator Previously, HTML5Translator contained a lot of code copied from HTMLTranslator, which created a maintenance burden and made it difficult to know what the actual differences are. With this change the common logic is factored out into a mixin, which eliminates the code duplication. This change also elimintes the `generate_targets_for_listing` fix from HTMLTranslator now that Sphinx depends on a version of docutils that contains the fix. --- sphinx/writers/html.py | 100 +++--- sphinx/writers/html5.py | 773 +--------------------------------------- 2 files changed, 48 insertions(+), 825 deletions(-) diff --git a/sphinx/writers/html.py b/sphinx/writers/html.py index 34b73a0a5c3..0ed898462ef 100644 --- a/sphinx/writers/html.py +++ b/sphinx/writers/html.py @@ -75,9 +75,9 @@ def translate(self) -> None: self.clean_meta = ''.join(self.visitor.meta[2:]) -class HTMLTranslator(SphinxTranslator, BaseTranslator): +class HTMLTranslatorCommonMixin: """ - Our custom HTML translator. + Overrides that are common to both HTMLTranslator and HTML5Translator. """ builder: "StandaloneHTMLBuilder" = None @@ -204,7 +204,7 @@ def visit_desc_parameter(self, node: Element) -> None: if self.optional_param_level == 0: self.required_params_left -= 1 if not node.hasattr('noemph'): - self.body.append('') + self.body.append('') def depart_desc_parameter(self, node: Element) -> None: if not node.hasattr('noemph'): @@ -347,33 +347,13 @@ def add_permalink_ref(self, node: Element, title: str) -> None: self.body.append(format % (node['ids'][0], title, self.config.html_permalinks_icon)) - def generate_targets_for_listing(self, node: Element) -> None: - """Generate hyperlink targets for listings. - - Original visit_bullet_list(), visit_definition_list() and visit_enumerated_list() - generates hyperlink targets inside listing tags (