blob: 238dd69ee358e1ae8d8aacc69f7f5b38b7c73669 [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
8
Alex Gaynor99ebc222017-05-17 14:55:21 -07009from sphinx.util.compat import Directive
Alex Gaynoraf82d5e2013-10-29 17:07:24 -070010
11
12DANGER_MESSAGE = """
13This is a "Hazardous Materials" module. You should **ONLY** use it if you're
14100% absolutely sure that you know what you're doing because this module is
Alex Gaynor2724ff62013-12-20 13:51:42 -080015full of land mines, dragons, and dinosaurs with laser guns.
16"""
17
18DANGER_ALTERNATE = """
19
20You may instead be interested in :doc:`{alternate}`.
21"""
22
Alex Gaynoraf82d5e2013-10-29 17:07:24 -070023
Alex Gaynoraf82d5e2013-10-29 17:07:24 -070024class HazmatDirective(Directive):
Alex Gaynor2724ff62013-12-20 13:51:42 -080025 has_content = True
26
Alex Gaynoraf82d5e2013-10-29 17:07:24 -070027 def run(self):
Alex Gaynor2724ff62013-12-20 13:51:42 -080028 message = DANGER_MESSAGE
29 if self.content:
30 message += DANGER_ALTERNATE.format(alternate=self.content[0])
31
Alex Gaynor99ebc222017-05-17 14:55:21 -070032 content = nodes.paragraph("", message)
33 admonition_node = Hazmat("\n".join(content))
34 self.state.nested_parse(content, self.content_offset, admonition_node)
35 admonition_node.line = self.lineno
36 return [admonition_node]
Alex Gaynoraf82d5e2013-10-29 17:07:24 -070037
38
39class Hazmat(nodes.Admonition, nodes.Element):
40 pass
41
42
Alex Gaynor69f5ee42013-11-19 14:06:18 -080043def html_visit_hazmat_node(self, node):
Alex Gaynoraf82d5e2013-10-29 17:07:24 -070044 return self.visit_admonition(node, "danger")
45
46
Alex Gaynor69f5ee42013-11-19 14:06:18 -080047def latex_visit_hazmat_node(self, node):
48 return self.visit_admonition(node)
49
50
Alex Gaynoraf82d5e2013-10-29 17:07:24 -070051def depart_hazmat_node(self, node):
52 return self.depart_admonition(node)
53
54
55def setup(app):
56 app.add_node(
57 Hazmat,
Alex Gaynor69f5ee42013-11-19 14:06:18 -080058 html=(html_visit_hazmat_node, depart_hazmat_node),
59 latex=(latex_visit_hazmat_node, depart_hazmat_node),
Alex Gaynoraf82d5e2013-10-29 17:07:24 -070060 )
61 app.add_directive("hazmat", HazmatDirective)
Alex Gaynorb5c5bbe2017-09-20 10:14:56 -040062
63 return {
64 "parallel_read_safe": True,
65 }