diff options
author | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2020-01-19 22:47:02 +0900 |
---|---|---|
committer | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2020-01-19 22:47:02 +0900 |
commit | 347e301727c3b2b08e277b0d8a72c33a1eba13d8 (patch) | |
tree | 8b0c32ac6d5bd84ce4a8746eff3ef54acec93830 /sphinx/util/docutils.py | |
parent | ad271f4ca33d298a880da8fdc75cc318b4a7842f (diff) | |
parent | eb273fdc08840945b9c2419f20fb2e0220b0a004 (diff) | |
download | sphinx-git-347e301727c3b2b08e277b0d8a72c33a1eba13d8.tar.gz |
Merge branch '2.0'
Diffstat (limited to 'sphinx/util/docutils.py')
-rw-r--r-- | sphinx/util/docutils.py | 41 |
1 files changed, 40 insertions, 1 deletions
diff --git a/sphinx/util/docutils.py b/sphinx/util/docutils.py index 16b09bd7c..9add18ec0 100644 --- a/sphinx/util/docutils.py +++ b/sphinx/util/docutils.py @@ -429,7 +429,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. @@ -441,6 +444,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 |