summaryrefslogtreecommitdiff
path: root/sphinx/util/docutils.py
diff options
context:
space:
mode:
authorTakeshi KOMIYA <i.tkomiya@gmail.com>2020-01-10 22:50:43 +0900
committerTakeshi KOMIYA <i.tkomiya@gmail.com>2020-01-10 22:50:43 +0900
commit35a092b7fe9b1a4230b39559a7b63a59dfb1d93a (patch)
tree7e8c2d202225b5e98533ad951c1f7a0bb2ff3ef5 /sphinx/util/docutils.py
parent4d64cd1142ff0902229b38e72197f96d444fdafc (diff)
downloadsphinx-git-35a092b7fe9b1a4230b39559a7b63a59dfb1d93a.tar.gz
SphinxTranslator calls visitor/departure method for super node class
Diffstat (limited to 'sphinx/util/docutils.py')
-rw-r--r--sphinx/util/docutils.py41
1 files changed, 40 insertions, 1 deletions
diff --git a/sphinx/util/docutils.py b/sphinx/util/docutils.py
index a44d8bd2e..23f2c888b 100644
--- a/sphinx/util/docutils.py
+++ b/sphinx/util/docutils.py
@@ -452,7 +452,10 @@ class ReferenceRole(SphinxRole):
class SphinxTranslator(nodes.NodeVisitor):
"""A base class for Sphinx translators.
- This class provides helper methods for Sphinx translators.
+ This class adds a support for visitor/departure method for super node class
+ if visitor/departure method for node class is not found.
+
+ It also provides helper methods for Sphinx translators.
.. note:: The subclasses of this class might not work with docutils.
This class is strongly coupled with Sphinx.
@@ -464,6 +467,42 @@ class SphinxTranslator(nodes.NodeVisitor):
self.config = builder.config
self.settings = document.settings
+ def dispatch_visit(self, node):
+ """
+ Dispatch node to appropriate visitor method.
+ The priority of visitor method is:
+
+ 1. ``self.visit_{node_class}()``
+ 2. ``self.visit_{supre_node_class}()``
+ 3. ``self.unknown_visit()``
+ """
+ for node_class in node.__class__.__mro__:
+ method = getattr(self, 'visit_%s' % (node_class.__name__), None)
+ if method:
+ logger.debug('SphinxTranslator.dispatch_visit calling %s for %s' %
+ (method.__name__, node))
+ return method(node)
+ else:
+ super().dispatch_visit(node)
+
+ def dispatch_departure(self, node):
+ """
+ Dispatch node to appropriate departure method.
+ The priority of departure method is:
+
+ 1. ``self.depart_{node_class}()``
+ 2. ``self.depart_{super_node_class}()``
+ 3. ``self.unknown_departure()``
+ """
+ for node_class in node.__class__.__mro__:
+ method = getattr(self, 'depart_%s' % (node_class.__name__), None)
+ if method:
+ logger.debug('SphinxTranslator.dispatch_departure calling %s for %s' %
+ (method.__name__, node))
+ return method(node)
+ else:
+ super().dispatch_departure(node)
+
# cache a vanilla instance of nodes.document
# Used in new_document() function