blob: 6aa7847dc33dda1a964afb71aa47f02f96563a1f [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
9from sphinx.util.compat import Directive, make_admonition
10
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 Gaynoraf82d5e2013-10-29 17:07:24 -070032 ad = make_admonition(
33 Hazmat,
34 self.name,
35 [],
36 self.options,
Alex Gaynor2724ff62013-12-20 13:51:42 -080037 nodes.paragraph("", message),
Alex Gaynoraf82d5e2013-10-29 17:07:24 -070038 self.lineno,
39 self.content_offset,
40 self.block_text,
41 self.state,
42 self.state_machine
43 )
44 ad[0].line = self.lineno
45 return ad
46
47
48class Hazmat(nodes.Admonition, nodes.Element):
49 pass
50
51
Alex Gaynor69f5ee42013-11-19 14:06:18 -080052def html_visit_hazmat_node(self, node):
Alex Gaynoraf82d5e2013-10-29 17:07:24 -070053 return self.visit_admonition(node, "danger")
54
55
Alex Gaynor69f5ee42013-11-19 14:06:18 -080056def latex_visit_hazmat_node(self, node):
57 return self.visit_admonition(node)
58
59
Alex Gaynoraf82d5e2013-10-29 17:07:24 -070060def depart_hazmat_node(self, node):
61 return self.depart_admonition(node)
62
63
64def setup(app):
65 app.add_node(
66 Hazmat,
Alex Gaynor69f5ee42013-11-19 14:06:18 -080067 html=(html_visit_hazmat_node, depart_hazmat_node),
68 latex=(latex_visit_hazmat_node, depart_hazmat_node),
Alex Gaynoraf82d5e2013-10-29 17:07:24 -070069 )
70 app.add_directive("hazmat", HazmatDirective)