Alex Gaynor | 5951f46 | 2014-11-16 09:08:42 -0800 | [diff] [blame] | 1 | # 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 Gaynor | c37feed | 2014-03-08 08:32:56 -0800 | [diff] [blame] | 4 | |
| 5 | from __future__ import absolute_import, division, print_function |
| 6 | |
Alex Gaynor | af82d5e | 2013-10-29 17:07:24 -0700 | [diff] [blame] | 7 | from docutils import nodes |
| 8 | |
Alex Gaynor | 99ebc22 | 2017-05-17 14:55:21 -0700 | [diff] [blame] | 9 | from sphinx.util.compat import Directive |
Alex Gaynor | af82d5e | 2013-10-29 17:07:24 -0700 | [diff] [blame] | 10 | |
| 11 | |
| 12 | DANGER_MESSAGE = """ |
| 13 | This is a "Hazardous Materials" module. You should **ONLY** use it if you're |
| 14 | 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] | 15 | full of land mines, dragons, and dinosaurs with laser guns. |
| 16 | """ |
| 17 | |
| 18 | DANGER_ALTERNATE = """ |
| 19 | |
| 20 | You may instead be interested in :doc:`{alternate}`. |
| 21 | """ |
| 22 | |
Alex Gaynor | af82d5e | 2013-10-29 17:07:24 -0700 | [diff] [blame] | 23 | |
Alex Gaynor | af82d5e | 2013-10-29 17:07:24 -0700 | [diff] [blame] | 24 | class HazmatDirective(Directive): |
Alex Gaynor | 2724ff6 | 2013-12-20 13:51:42 -0800 | [diff] [blame] | 25 | has_content = True |
| 26 | |
Alex Gaynor | af82d5e | 2013-10-29 17:07:24 -0700 | [diff] [blame] | 27 | def run(self): |
Alex Gaynor | 2724ff6 | 2013-12-20 13:51:42 -0800 | [diff] [blame] | 28 | message = DANGER_MESSAGE |
| 29 | if self.content: |
| 30 | message += DANGER_ALTERNATE.format(alternate=self.content[0]) |
| 31 | |
Alex Gaynor | 99ebc22 | 2017-05-17 14:55:21 -0700 | [diff] [blame] | 32 | 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 Gaynor | af82d5e | 2013-10-29 17:07:24 -0700 | [diff] [blame] | 37 | |
| 38 | |
| 39 | class Hazmat(nodes.Admonition, nodes.Element): |
| 40 | pass |
| 41 | |
| 42 | |
Alex Gaynor | 69f5ee4 | 2013-11-19 14:06:18 -0800 | [diff] [blame] | 43 | def html_visit_hazmat_node(self, node): |
Alex Gaynor | af82d5e | 2013-10-29 17:07:24 -0700 | [diff] [blame] | 44 | return self.visit_admonition(node, "danger") |
| 45 | |
| 46 | |
Alex Gaynor | 69f5ee4 | 2013-11-19 14:06:18 -0800 | [diff] [blame] | 47 | def latex_visit_hazmat_node(self, node): |
| 48 | return self.visit_admonition(node) |
| 49 | |
| 50 | |
Alex Gaynor | af82d5e | 2013-10-29 17:07:24 -0700 | [diff] [blame] | 51 | def depart_hazmat_node(self, node): |
| 52 | return self.depart_admonition(node) |
| 53 | |
| 54 | |
| 55 | def setup(app): |
| 56 | app.add_node( |
| 57 | Hazmat, |
Alex Gaynor | 69f5ee4 | 2013-11-19 14:06:18 -0800 | [diff] [blame] | 58 | html=(html_visit_hazmat_node, depart_hazmat_node), |
| 59 | latex=(latex_visit_hazmat_node, depart_hazmat_node), |
Alex Gaynor | af82d5e | 2013-10-29 17:07:24 -0700 | [diff] [blame] | 60 | ) |
| 61 | app.add_directive("hazmat", HazmatDirective) |