|
| 1 | +# Copyright 2009 10gen, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""MongoDB specific extensions to Sphinx.""" |
| 16 | + |
| 17 | +from docutils import nodes |
| 18 | +from docutils.writers import html4css1 |
| 19 | +from sphinx import addnodes |
| 20 | +from sphinx.util.compat import (Directive, |
| 21 | + make_admonition) |
| 22 | + |
| 23 | +class mongodoc(nodes.Admonition, nodes.Element): |
| 24 | + pass |
| 25 | + |
| 26 | +class mongoref(nodes.reference): |
| 27 | + pass |
| 28 | + |
| 29 | + |
| 30 | +def visit_mongodoc_node(self, node): |
| 31 | + self.visit_admonition(node, "seealso") |
| 32 | + |
| 33 | + |
| 34 | +def depart_mongodoc_node(self, node): |
| 35 | + self.depart_admonition(node) |
| 36 | + |
| 37 | + |
| 38 | +def visit_mongoref_node(self, node): |
| 39 | + atts = {"class": "reference external", |
| 40 | + "href": node["refuri"], |
| 41 | + "name": node["name"]} |
| 42 | + self.body.append(self.starttag(node, 'a', '', **atts)) |
| 43 | + |
| 44 | + |
| 45 | +def depart_mongoref_node(self, node): |
| 46 | + self.body.append('</a>') |
| 47 | + if not isinstance(node.parent, nodes.TextElement): |
| 48 | + self.body.append('\n') |
| 49 | + |
| 50 | + |
| 51 | +class MongodocDirective(Directive): |
| 52 | + |
| 53 | + has_content = True |
| 54 | + required_arguments = 0 |
| 55 | + optional_arguments = 0 |
| 56 | + final_argument_whitespace = False |
| 57 | + option_spec = {} |
| 58 | + |
| 59 | + def run(self): |
| 60 | + env = self.state.document.settings.env |
| 61 | + |
| 62 | + return make_admonition(mongodoc, self.name, |
| 63 | + [_('See general MongoDB documentation')], |
| 64 | + self.options, self.content, self.lineno, |
| 65 | + self.content_offset, self.block_text, self.state, |
| 66 | + self.state_machine) |
| 67 | + |
| 68 | + |
| 69 | +def process_mongodoc_nodes(app, doctree, fromdocname): |
| 70 | + env = app.builder.env |
| 71 | + |
| 72 | + for node in doctree.traverse(mongodoc): |
| 73 | + for name in node.parent.parent.traverse(addnodes.desc_signature): |
| 74 | + anchor = "#%s" % name["ids"][0] |
| 75 | + break |
| 76 | + for para in node.traverse(nodes.paragraph): |
| 77 | + tag = str(para.traverse()[1]) |
| 78 | + link = mongoref("", "") |
| 79 | + link["refuri"] = "http://dochub.mongodb.org/core/%s" % tag |
| 80 | + link["name"] = anchor |
| 81 | + link.append(nodes.emphasis(_(tag), _(tag))) |
| 82 | + new_para = nodes.paragraph() |
| 83 | + new_para += link |
| 84 | + node.replace(para, new_para) |
| 85 | + break |
| 86 | + |
| 87 | + |
| 88 | +def setup(app): |
| 89 | + app.add_node(mongodoc, |
| 90 | + html=(visit_mongodoc_node, depart_mongodoc_node), |
| 91 | + latex=(visit_mongodoc_node, depart_mongodoc_node), |
| 92 | + text=(visit_mongodoc_node, depart_mongodoc_node)) |
| 93 | + app.add_node(mongoref, |
| 94 | + html=(visit_mongoref_node, depart_mongoref_node)) |
| 95 | + |
| 96 | + app.add_directive("mongodoc", MongodocDirective) |
| 97 | + app.connect("doctree-resolved", process_mongodoc_nodes) |
0 commit comments