blob: e4e9296ca7a6ef1f535844abaaa7511a83d645d0 [file] [log] [blame]
Alex Gaynorc37feed2014-03-08 08:32:56 -08001# Licensed under the Apache License, Version 2.0 (the "License");
2# you may not use this file except in compliance with the License.
3# You may obtain a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS,
9# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
10# implied.
11# See the License for the specific language governing permissions and
12# limitations under the License.
13
14from __future__ import absolute_import, division, print_function
15
Alex Gaynoraf82d5e2013-10-29 17:07:24 -070016from docutils import nodes
17
18from sphinx.util.compat import Directive, make_admonition
19
20
21DANGER_MESSAGE = """
22This is a "Hazardous Materials" module. You should **ONLY** use it if you're
23100% absolutely sure that you know what you're doing because this module is
Alex Gaynor2724ff62013-12-20 13:51:42 -080024full of land mines, dragons, and dinosaurs with laser guns.
25"""
26
27DANGER_ALTERNATE = """
28
29You may instead be interested in :doc:`{alternate}`.
30"""
31
Alex Gaynoraf82d5e2013-10-29 17:07:24 -070032
Alex Gaynoraf82d5e2013-10-29 17:07:24 -070033class HazmatDirective(Directive):
Alex Gaynor2724ff62013-12-20 13:51:42 -080034 has_content = True
35
Alex Gaynoraf82d5e2013-10-29 17:07:24 -070036 def run(self):
Alex Gaynor2724ff62013-12-20 13:51:42 -080037 message = DANGER_MESSAGE
38 if self.content:
39 message += DANGER_ALTERNATE.format(alternate=self.content[0])
40
Alex Gaynoraf82d5e2013-10-29 17:07:24 -070041 ad = make_admonition(
42 Hazmat,
43 self.name,
44 [],
45 self.options,
Alex Gaynor2724ff62013-12-20 13:51:42 -080046 nodes.paragraph("", message),
Alex Gaynoraf82d5e2013-10-29 17:07:24 -070047 self.lineno,
48 self.content_offset,
49 self.block_text,
50 self.state,
51 self.state_machine
52 )
53 ad[0].line = self.lineno
54 return ad
55
56
57class Hazmat(nodes.Admonition, nodes.Element):
58 pass
59
60
Alex Gaynor69f5ee42013-11-19 14:06:18 -080061def html_visit_hazmat_node(self, node):
Alex Gaynoraf82d5e2013-10-29 17:07:24 -070062 return self.visit_admonition(node, "danger")
63
64
Alex Gaynor69f5ee42013-11-19 14:06:18 -080065def latex_visit_hazmat_node(self, node):
66 return self.visit_admonition(node)
67
68
Alex Gaynoraf82d5e2013-10-29 17:07:24 -070069def depart_hazmat_node(self, node):
70 return self.depart_admonition(node)
71
72
73def setup(app):
74 app.add_node(
75 Hazmat,
Alex Gaynor69f5ee42013-11-19 14:06:18 -080076 html=(html_visit_hazmat_node, depart_hazmat_node),
77 latex=(latex_visit_hazmat_node, depart_hazmat_node),
Alex Gaynoraf82d5e2013-10-29 17:07:24 -070078 )
79 app.add_directive("hazmat", HazmatDirective)