codekingpro/portable-devtools
114k
1# Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.2#3# Licensed under the Apache License, Version 2.0 (the "License"). You4# may not use this file except in compliance with the License. A copy of5# the License is located at6#7# http://aws.amazon.com/apache2.0/8#9# or in the "license" file accompanying this file. This file is10# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF11# ANY KIND, either express or implied. See the License for the specific12# language governing permissions and limitations under the License.13from docutils import nodes14from sphinx.locale import admonitionlabels15from sphinx.writers.html5 import HTML5Translator as SphinxHTML5Translator16 17 18class BotoHTML5Translator(SphinxHTML5Translator):19 """Extension of Sphinx's ``HTML5Translator`` for Botocore documentation."""20 21 IGNORE_IMPLICIT_HEADINGS = [22 '[REQUIRED]',23 ]24 25 def visit_admonition(self, node, name=""):26 """Uses the h3 tag for admonition titles instead of the p tag."""27 self.body.append(28 self.starttag(node, "div", CLASS=("admonition " + name))29 )30 if name:31 title = (32 f"<h3 class='admonition-title'> {admonitionlabels[name]}</h3>"33 )34 self.body.append(title)35 36 def is_implicit_heading(self, node):37 """Determines if a node is an implicit heading.38 39 An implicit heading is represented by a paragraph node whose only40 child is a strong node with text that isnt in `IGNORE_IMPLICIT_HEADINGS`.41 """42 return (43 len(node) == 144 and isinstance(node[0], nodes.strong)45 and len(node[0]) == 146 and isinstance(node[0][0], nodes.Text)47 and node[0][0].astext() not in self.IGNORE_IMPLICIT_HEADINGS48 )49 50 def visit_paragraph(self, node):51 """Visit a paragraph HTML element.52 53 Replaces implicit headings with an h3 tag and defers to default54 behavior for normal paragraph elements.55 """56 if self.is_implicit_heading(node):57 text = node[0][0]58 self.body.append(f'<h3>{text}</h3>\n')59 # Do not visit the current nodes children or call its depart method.60 raise nodes.SkipNode61 else:62 super().visit_paragraph(node)63 