blob: 923ec6f5b2c3a0aa3ce05befb795a2a5aad17f64 [file] [log] [blame]
Alex Gaynor5951f462014-11-16 09:08:42 -08001# 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.
Alex Gaynorc37feed2014-03-08 08:32:56 -08004
5from __future__ import absolute_import, division, print_function
6
Alex Gaynoraf82d5e2013-10-29 17:07:24 -07007from docutils import nodes
Alex Gaynor94020442018-02-13 10:31:05 -05008from docutils.parsers.rst import Directive
Alex Gaynoraf82d5e2013-10-29 17:07:24 -07009
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
Alex Gaynor2724ff62013-12-20 13:51:42 -080014full of land mines, dragons, and dinosaurs with laser guns.
15"""
16
17DANGER_ALTERNATE = """
18
19You may instead be interested in :doc:`{alternate}`.
20"""
21
Alex Gaynoraf82d5e2013-10-29 17:07:24 -070022
Alex Gaynoraf82d5e2013-10-29 17:07:24 -070023class HazmatDirective(Directive):
Alex Gaynor2724ff62013-12-20 13:51:42 -080024 has_content = True
25
Alex Gaynoraf82d5e2013-10-29 17:07:24 -070026 def run(self):
Alex Gaynor2724ff62013-12-20 13:51:42 -080027 message = DANGER_MESSAGE
28 if self.content:
29 message += DANGER_ALTERNATE.format(alternate=self.content[0])
30
Alex Gaynor99ebc222017-05-17 14:55:21 -070031 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]
Alex Gaynoraf82d5e2013-10-29 17:07:24 -070036
37
38class Hazmat(nodes.Admonition, nodes.Element):
39 pass
40
41
Alex Gaynor69f5ee42013-11-19 14:06:18 -080042def html_visit_hazmat_node(self, node):
Alex Gaynoraf82d5e2013-10-29 17:07:24 -070043 return self.visit_admonition(node, "danger")
44
45
Alex Gaynor69f5ee42013-11-19 14:06:18 -080046def latex_visit_hazmat_node(self, node):
47 return self.visit_admonition(node)
48
49
Alex Gaynoraf82d5e2013-10-29 17:07:24 -070050def depart_hazmat_node(self, node):
51 return self.depart_admonition(node)
52
53
54def setup(app):
55 app.add_node(
56 Hazmat,
Alex Gaynor69f5ee42013-11-19 14:06:18 -080057 html=(html_visit_hazmat_node, depart_hazmat_node),
58 latex=(latex_visit_hazmat_node, depart_hazmat_node),
Alex Gaynoraf82d5e2013-10-29 17:07:24 -070059 )
60 app.add_directive("hazmat", HazmatDirective)
Alex Gaynorb5c5bbe2017-09-20 10:14:56 -040061
62 return {
63 "parallel_read_safe": True,
64 }