chiark / gitweb /
m.htmlsanity: render TOC as a <nav> instead of <aside>.
authorVladimír Vondruš <mosra@centrum.cz>
Wed, 5 Jan 2022 20:51:15 +0000 (21:51 +0100)
committerVladimír Vondruš <mosra@centrum.cz>
Wed, 5 Jan 2022 20:56:00 +0000 (21:56 +0100)
doc/plugins/htmlsanity.rst
plugins/m/htmlsanity.py
plugins/m/test/htmlsanity_content/page.html

index d338bd0ef56b34512cb81fe8d1767db9c9dbbefd..9b778bafee1df148c873967f96f046d5df5dc4da 100644 (file)
@@ -117,7 +117,8 @@ horrible, right?) with a custom HTML5 writer derived from
 -   Even the Docutils HTML5 writer was putting *frightening* :html:`<colgroup>`
     things into HTML tables. Not anymore.
 -   Topics are using HTML5 :html:`<aside>` tag, topic headers are using
-    :html:`<h3>` instead of a nondescript :html:`<div>`
+    :html:`<h3>` instead of a nondescript :html:`<div>`. A special case is
+    Table of Contents, which is a :html:`<nav>` instead of :html:`<aside>`
 -   Line blocks are simply :html:`<p>` elements with lines delimited using
     :html:`<br>`
 -   The :html:`<abbr>` tag now properly includes a ``title`` attribute
index 4709d9df5a405699998c31f41b697e07dd89e47b..bfb404873ea0633c1edea5491d6bb4c90116d401 100644 (file)
@@ -382,13 +382,22 @@ class SaneHtmlTranslator(HTMLTranslator):
             self.body.append('\n')
         self.in_mailto = False
 
-    # Use <aside> instead of a meaningless <div>
+    # Use <aside> for general topics, <nav> for table of contents (but drop the
+    # contents class and ID)
     def visit_topic(self, node):
-        self.body.append(self.starttag(node, 'aside'))
         self.topic_classes = node['classes']
 
+        if 'contents' in node['classes']:
+            node.html_tagname = 'nav'
+            node['classes'].remove('contents')
+            node['ids'].remove('contents')
+        else:
+            node.html_tagname = 'aside'
+
+        self.body.append(self.starttag(node, node.html_tagname))
+
     def depart_topic(self, node):
-        self.body.append('</aside>\n')
+        self.body.append('</{}>\n'.format(node.html_tagname))
         self.topic_classes = []
 
     # Don't use <colgroup> or other shit in tables
index a9b3ce794b575d9b6482fc9a2fdd5fa67963493f..2ef5c60e75245841fcd2f08730eb280bb89dafa2 100644 (file)
@@ -23,7 +23,7 @@
       <div class="m-col-l-10 m-push-l-1">
         <h1>A page</h1>
 <!-- content -->
-<aside class="contents m-block m-default" id="contents">
+<nav class="m-block m-default">
 <h3>Contents</h3>
 <ul>
 <li><a href="#section-title" id="id6">Section title</a><ul>
@@ -32,7 +32,7 @@
 </li>
 <li><a href="#section-after" id="id8">Section after</a></li>
 </ul>
-</aside>
+</nav>
 <p>A paragraph.</p>
 <blockquote>
 A block quote.</blockquote>