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 | |
| 9 | from sphinx.util.compat import Directive, make_admonition |
| 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 | af82d5e | 2013-10-29 17:07:24 -0700 | [diff] [blame] | 32 | ad = make_admonition( |
| 33 | Hazmat, |
| 34 | self.name, |
| 35 | [], |
| 36 | self.options, |
Alex Gaynor | 2724ff6 | 2013-12-20 13:51:42 -0800 | [diff] [blame] | 37 | nodes.paragraph("", message), |
Alex Gaynor | af82d5e | 2013-10-29 17:07:24 -0700 | [diff] [blame] | 38 | 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 | |
| 48 | class Hazmat(nodes.Admonition, nodes.Element): |
| 49 | pass |
| 50 | |
| 51 | |
Alex Gaynor | 69f5ee4 | 2013-11-19 14:06:18 -0800 | [diff] [blame] | 52 | def html_visit_hazmat_node(self, node): |
Alex Gaynor | af82d5e | 2013-10-29 17:07:24 -0700 | [diff] [blame] | 53 | return self.visit_admonition(node, "danger") |
| 54 | |
| 55 | |
Alex Gaynor | 69f5ee4 | 2013-11-19 14:06:18 -0800 | [diff] [blame] | 56 | def latex_visit_hazmat_node(self, node): |
| 57 | return self.visit_admonition(node) |
| 58 | |
| 59 | |
Alex Gaynor | af82d5e | 2013-10-29 17:07:24 -0700 | [diff] [blame] | 60 | def depart_hazmat_node(self, node): |
| 61 | return self.depart_admonition(node) |
| 62 | |
| 63 | |
| 64 | def setup(app): |
| 65 | app.add_node( |
| 66 | Hazmat, |
Alex Gaynor | 69f5ee4 | 2013-11-19 14:06:18 -0800 | [diff] [blame] | 67 | html=(html_visit_hazmat_node, depart_hazmat_node), |
| 68 | latex=(latex_visit_hazmat_node, depart_hazmat_node), |
Alex Gaynor | af82d5e | 2013-10-29 17:07:24 -0700 | [diff] [blame] | 69 | ) |
| 70 | app.add_directive("hazmat", HazmatDirective) |