Alex Gaynor | c37feed | 2014-03-08 08:32:56 -0800 | [diff] [blame] | 1 | # 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 | |
| 14 | from __future__ import absolute_import, division, print_function |
| 15 | |
Alex Gaynor | af82d5e | 2013-10-29 17:07:24 -0700 | [diff] [blame] | 16 | from docutils import nodes |
| 17 | |
| 18 | from sphinx.util.compat import Directive, make_admonition |
| 19 | |
| 20 | |
| 21 | DANGER_MESSAGE = """ |
| 22 | This is a "Hazardous Materials" module. You should **ONLY** use it if you're |
| 23 | 100% absolutely sure that you know what you're doing because this module is |
Alex Gaynor | 2724ff6 | 2013-12-20 13:51:42 -0800 | [diff] [blame] | 24 | full of land mines, dragons, and dinosaurs with laser guns. |
| 25 | """ |
| 26 | |
| 27 | DANGER_ALTERNATE = """ |
| 28 | |
| 29 | You may instead be interested in :doc:`{alternate}`. |
| 30 | """ |
| 31 | |
Alex Gaynor | af82d5e | 2013-10-29 17:07:24 -0700 | [diff] [blame] | 32 | |
Alex Gaynor | af82d5e | 2013-10-29 17:07:24 -0700 | [diff] [blame] | 33 | class HazmatDirective(Directive): |
Alex Gaynor | 2724ff6 | 2013-12-20 13:51:42 -0800 | [diff] [blame] | 34 | has_content = True |
| 35 | |
Alex Gaynor | af82d5e | 2013-10-29 17:07:24 -0700 | [diff] [blame] | 36 | def run(self): |
Alex Gaynor | 2724ff6 | 2013-12-20 13:51:42 -0800 | [diff] [blame] | 37 | message = DANGER_MESSAGE |
| 38 | if self.content: |
| 39 | message += DANGER_ALTERNATE.format(alternate=self.content[0]) |
| 40 | |
Alex Gaynor | af82d5e | 2013-10-29 17:07:24 -0700 | [diff] [blame] | 41 | ad = make_admonition( |
| 42 | Hazmat, |
| 43 | self.name, |
| 44 | [], |
| 45 | self.options, |
Alex Gaynor | 2724ff6 | 2013-12-20 13:51:42 -0800 | [diff] [blame] | 46 | nodes.paragraph("", message), |
Alex Gaynor | af82d5e | 2013-10-29 17:07:24 -0700 | [diff] [blame] | 47 | 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 | |
| 57 | class Hazmat(nodes.Admonition, nodes.Element): |
| 58 | pass |
| 59 | |
| 60 | |
Alex Gaynor | 69f5ee4 | 2013-11-19 14:06:18 -0800 | [diff] [blame] | 61 | def html_visit_hazmat_node(self, node): |
Alex Gaynor | af82d5e | 2013-10-29 17:07:24 -0700 | [diff] [blame] | 62 | return self.visit_admonition(node, "danger") |
| 63 | |
| 64 | |
Alex Gaynor | 69f5ee4 | 2013-11-19 14:06:18 -0800 | [diff] [blame] | 65 | def latex_visit_hazmat_node(self, node): |
| 66 | return self.visit_admonition(node) |
| 67 | |
| 68 | |
Alex Gaynor | af82d5e | 2013-10-29 17:07:24 -0700 | [diff] [blame] | 69 | def depart_hazmat_node(self, node): |
| 70 | return self.depart_admonition(node) |
| 71 | |
| 72 | |
| 73 | def setup(app): |
| 74 | app.add_node( |
| 75 | Hazmat, |
Alex Gaynor | 69f5ee4 | 2013-11-19 14:06:18 -0800 | [diff] [blame] | 76 | html=(html_visit_hazmat_node, depart_hazmat_node), |
| 77 | latex=(latex_visit_hazmat_node, depart_hazmat_node), |
Alex Gaynor | af82d5e | 2013-10-29 17:07:24 -0700 | [diff] [blame] | 78 | ) |
| 79 | app.add_directive("hazmat", HazmatDirective) |