chiark / gitweb /
m.htmlsanity: traverse() is deprecated since docutils 0.18.
authorVladimír Vondruš <mosra@centrum.cz>
Wed, 21 Aug 2024 10:23:57 +0000 (12:23 +0200)
committerVladimír Vondruš <mosra@centrum.cz>
Wed, 21 Aug 2024 11:54:54 +0000 (13:54 +0200)
Looks like it got changed to an iterator to be faster, unfortunately
Sphinx relied on it returning a list, so it got changed back and
deprecated in favor of findall().

Using the new name and adding it as an alias for traverse() on older
versions.

documentation/python.py
plugins/m/htmlsanity.py

index 8b056c5ec869e137fb27ab0c4e5c07e200eb3d81..5933db3a153c2240c4e93febd21a43fb788cd085 100755 (executable)
@@ -2188,7 +2188,7 @@ class ExtractImages(Transform):
 
     def apply(self):
         ExtractImages._external_data = set()
-        for image in self.document.traverse(docutils.nodes.image):
+        for image in self.document.findall(docutils.nodes.image):
             # Skip absolute URLs
             if urllib.parse.urlparse(image['uri']).netloc: continue
 
@@ -2368,7 +2368,7 @@ def render_page(state: State, path, input_filename, env):
 
     # Extract metadata from the page
     metadata = {}
-    for docinfo in pub.document.traverse(docutils.nodes.docinfo):
+    for docinfo in pub.document.findall(docutils.nodes.docinfo):
         for element in docinfo.children:
             if element.tagname == 'field':
                 name_elem, body_elem = element.children
index 2f68d84b85291937c75c2bad53fef476bf152f6c..435c1a8df8e0e48aab17e5408f164d692fb01c0a 100644 (file)
@@ -69,6 +69,14 @@ docutils_settings = {
     'embed_stylesheet': False
 }
 
+# findall() is new in docutils 0.18, replacing traverse() because that one was
+# attempted to be changed to return an iterator instead of a list, which broke
+# Sphinx, so it instead got deprecated in favor of findall() which is the same
+# as traverse() returning an iterator was. To retain compatibility with earlier
+# versions, add an alias.
+if not hasattr(docutils.nodes.Node, 'findall'):
+    setattr(docutils.nodes.Node, 'findall', docutils.nodes.Node.traverse)
+
 words_re = re.compile(r'\w+', re.UNICODE|re.X)
 
 def extract_document_language(document):
@@ -76,7 +84,7 @@ def extract_document_language(document):
     language = document.settings.language_code
 
     # Then try to find the :lang: metadata option
-    for field in document.traverse(nodes.field):
+    for field in document.findall(nodes.field):
         assert isinstance(field[0], nodes.field_name)
         assert isinstance(field[1], nodes.field_body)
         # field_body -> paragraph -> text
@@ -137,7 +145,7 @@ class SmartQuotes(docutils.transforms.universal.SmartQuotes):
 
         # "Educate" quotes in normal text. Handle each block of text
         # (TextElement node) as a unit to keep context around inline nodes:
-        for node in self.document.traverse(nodes.TextElement):
+        for node in self.document.findall(nodes.TextElement):
             # skip preformatted text blocks and special elements:
             if isinstance(node, (nodes.FixedTextElement, nodes.Special)):
                 continue
@@ -148,7 +156,7 @@ class SmartQuotes(docutils.transforms.universal.SmartQuotes):
             # list of text nodes in the "text block":
             # Patched here to exclude more stuff.
             txtnodes = []
-            for txtnode in node.traverse(nodes.Text):
+            for txtnode in node.findall(nodes.Text):
                 if not can_apply_typography(txtnode): continue
                 # Don't convert -- in option strings
                 if isinstance(txtnode.parent, nodes.option_string): continue
@@ -204,12 +212,12 @@ class Pyphen(Transform):
         pyphen_for_lang = {}
 
         # Go through all text words and hyphenate them
-        for node in self.document.traverse(nodes.TextElement):
+        for node in self.document.findall(nodes.TextElement):
             # Skip preformatted text blocks and special elements
             if isinstance(node, (nodes.FixedTextElement, nodes.Special)):
                 continue
 
-            for txtnode in node.traverse(nodes.Text):
+            for txtnode in node.findall(nodes.Text):
                 if not can_apply_typography(txtnode): continue
 
                 # Don't hyphenate document title. Not part of