1# This file is dual licensed under the terms of the Apache License, Version 2# 2.0, and the BSD License. See the LICENSE file in the root of this repository 3# for complete details. 4 5from __future__ import absolute_import, division, print_function 6 7from docutils import nodes 8from docutils.parsers.rst import Directive 9 10 11DANGER_MESSAGE = """ 12This is a "Hazardous Materials" module. You should **ONLY** use it if you're 13100% absolutely sure that you know what you're doing because this module is 14full of land mines, dragons, and dinosaurs with laser guns. 15""" 16 17DANGER_ALTERNATE = """ 18 19You may instead be interested in :doc:`{alternate}`. 20""" 21 22 23class HazmatDirective(Directive): 24 has_content = True 25 26 def run(self): 27 message = DANGER_MESSAGE 28 if self.content: 29 message += DANGER_ALTERNATE.format(alternate=self.content[0]) 30 31 content = nodes.paragraph("", message) 32 admonition_node = Hazmat("\n".join(content)) 33 self.state.nested_parse(content, self.content_offset, admonition_node) 34 admonition_node.line = self.lineno 35 return [admonition_node] 36 37 38class Hazmat(nodes.Admonition, nodes.Element): 39 pass 40 41 42def html_visit_hazmat_node(self, node): 43 return self.visit_admonition(node, "danger") 44 45 46def latex_visit_hazmat_node(self, node): 47 return self.visit_admonition(node) 48 49 50def depart_hazmat_node(self, node): 51 return self.depart_admonition(node) 52 53 54def setup(app): 55 app.add_node( 56 Hazmat, 57 html=(html_visit_hazmat_node, depart_hazmat_node), 58 latex=(latex_visit_hazmat_node, depart_hazmat_node), 59 ) 60 app.add_directive("hazmat", HazmatDirective) 61 62 return { 63 "parallel_read_safe": True, 64 } 65