00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 """MongoDB specific extensions to Sphinx."""
00016
00017 from docutils import nodes
00018 from docutils.writers import html4css1
00019 from sphinx import addnodes
00020 from sphinx.util.compat import (Directive,
00021 make_admonition)
00022
00023 class mongodoc(nodes.Admonition, nodes.Element):
00024 pass
00025
00026 class mongoref(nodes.reference):
00027 pass
00028
00029
00030 def visit_mongodoc_node(self, node):
00031 self.visit_admonition(node, "seealso")
00032
00033
00034 def depart_mongodoc_node(self, node):
00035 self.depart_admonition(node)
00036
00037
00038 def visit_mongoref_node(self, node):
00039 atts = {"class": "reference external",
00040 "href": node["refuri"],
00041 "name": node["name"]}
00042 self.body.append(self.starttag(node, 'a', '', **atts))
00043
00044
00045 def depart_mongoref_node(self, node):
00046 self.body.append('</a>')
00047 if not isinstance(node.parent, nodes.TextElement):
00048 self.body.append('\n')
00049
00050
00051 class MongodocDirective(Directive):
00052
00053 has_content = True
00054 required_arguments = 0
00055 optional_arguments = 0
00056 final_argument_whitespace = False
00057 option_spec = {}
00058
00059 def run(self):
00060 env = self.state.document.settings.env
00061
00062 return make_admonition(mongodoc, self.name,
00063 ['See general MongoDB documentation'],
00064 self.options, self.content, self.lineno,
00065 self.content_offset, self.block_text, self.state,
00066 self.state_machine)
00067
00068
00069 def process_mongodoc_nodes(app, doctree, fromdocname):
00070 env = app.builder.env
00071
00072 for node in doctree.traverse(mongodoc):
00073 anchor = None
00074 for name in node.parent.parent.traverse(addnodes.desc_signature):
00075 anchor = name["ids"][0]
00076 break
00077 if not anchor:
00078 for name in node.parent.traverse(nodes.section):
00079 anchor = name["ids"][0]
00080 break
00081 for para in node.traverse(nodes.paragraph):
00082 tag = str(para.traverse()[1])
00083 link = mongoref("", "")
00084 link["refuri"] = "http://dochub.mongodb.org/core/%s" % tag
00085 link["name"] = anchor
00086 link.append(nodes.emphasis(tag, tag))
00087 new_para = nodes.paragraph()
00088 new_para += link
00089 node.replace(para, new_para)
00090
00091
00092 def setup(app):
00093 app.add_node(mongodoc,
00094 html=(visit_mongodoc_node, depart_mongodoc_node),
00095 latex=(visit_mongodoc_node, depart_mongodoc_node),
00096 text=(visit_mongodoc_node, depart_mongodoc_node))
00097 app.add_node(mongoref,
00098 html=(visit_mongoref_node, depart_mongoref_node))
00099
00100 app.add_directive("mongodoc", MongodocDirective)
00101 app.connect("doctree-resolved", process_mongodoc_nodes)