Martin v. Löwis | 5680d0c | 2008-04-10 03:06:53 +0000 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | """ |
| 3 | pyspecific.py |
| 4 | ~~~~~~~~~~~~~ |
| 5 | |
| 6 | Sphinx extension with Python doc-specific markup. |
| 7 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 8 | :copyright: 2008, 2009, 2010 by Georg Brandl. |
Martin v. Löwis | 5680d0c | 2008-04-10 03:06:53 +0000 | [diff] [blame] | 9 | :license: Python license. |
| 10 | """ |
| 11 | |
| 12 | ISSUE_URI = 'http://bugs.python.org/issue%s' |
Ezio Melotti | 1a87e32 | 2011-03-11 15:16:01 +0200 | [diff] [blame] | 13 | SOURCE_URI = 'http://hg.python.org/cpython/file/default/%s' |
Martin v. Löwis | 5680d0c | 2008-04-10 03:06:53 +0000 | [diff] [blame] | 14 | |
| 15 | from docutils import nodes, utils |
Georg Brandl | 6881886 | 2010-11-06 07:19:35 +0000 | [diff] [blame] | 16 | from sphinx.util.nodes import split_explicit_title |
Martin v. Löwis | 5680d0c | 2008-04-10 03:06:53 +0000 | [diff] [blame] | 17 | |
Benjamin Peterson | 5c6d787 | 2009-02-06 02:40:07 +0000 | [diff] [blame] | 18 | # monkey-patch reST parser to disable alphabetic and roman enumerated lists |
| 19 | from docutils.parsers.rst.states import Body |
| 20 | Body.enum.converters['loweralpha'] = \ |
| 21 | Body.enum.converters['upperalpha'] = \ |
| 22 | Body.enum.converters['lowerroman'] = \ |
| 23 | Body.enum.converters['upperroman'] = lambda x: None |
| 24 | |
Georg Brandl | ee8783d | 2009-09-16 16:00:31 +0000 | [diff] [blame] | 25 | # monkey-patch HTML translator to give versionmodified paragraphs a class |
| 26 | def new_visit_versionmodified(self, node): |
| 27 | self.body.append(self.starttag(node, 'p', CLASS=node['type'])) |
| 28 | text = versionlabels[node['type']] % node['version'] |
| 29 | if len(node): |
| 30 | text += ': ' |
| 31 | else: |
| 32 | text += '.' |
| 33 | self.body.append('<span class="versionmodified">%s</span>' % text) |
| 34 | |
| 35 | from sphinx.writers.html import HTMLTranslator |
| 36 | from sphinx.locale import versionlabels |
| 37 | HTMLTranslator.visit_versionmodified = new_visit_versionmodified |
| 38 | |
Benjamin Peterson | 5c6d787 | 2009-02-06 02:40:07 +0000 | [diff] [blame] | 39 | |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 40 | # Support for marking up and linking to bugs.python.org issues |
| 41 | |
Martin v. Löwis | 5680d0c | 2008-04-10 03:06:53 +0000 | [diff] [blame] | 42 | def issue_role(typ, rawtext, text, lineno, inliner, options={}, content=[]): |
| 43 | issue = utils.unescape(text) |
| 44 | text = 'issue ' + issue |
| 45 | refnode = nodes.reference(text, text, refuri=ISSUE_URI % issue) |
| 46 | return [refnode], [] |
| 47 | |
| 48 | |
Georg Brandl | 6881886 | 2010-11-06 07:19:35 +0000 | [diff] [blame] | 49 | # Support for linking to Python source files easily |
| 50 | |
| 51 | def source_role(typ, rawtext, text, lineno, inliner, options={}, content=[]): |
| 52 | has_t, title, target = split_explicit_title(text) |
| 53 | title = utils.unescape(title) |
| 54 | target = utils.unescape(target) |
| 55 | refnode = nodes.reference(title, title, refuri=SOURCE_URI % target) |
| 56 | return [refnode], [] |
| 57 | |
| 58 | |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 59 | # Support for marking up implementation details |
| 60 | |
| 61 | from sphinx.util.compat import Directive |
| 62 | |
| 63 | class ImplementationDetail(Directive): |
| 64 | |
| 65 | has_content = True |
| 66 | required_arguments = 0 |
| 67 | optional_arguments = 1 |
| 68 | final_argument_whitespace = True |
| 69 | |
| 70 | def run(self): |
| 71 | pnode = nodes.compound(classes=['impl-detail']) |
| 72 | content = self.content |
| 73 | add_text = nodes.strong('CPython implementation detail:', |
| 74 | 'CPython implementation detail:') |
| 75 | if self.arguments: |
| 76 | n, m = self.state.inline_text(self.arguments[0], self.lineno) |
| 77 | pnode.append(nodes.paragraph('', '', *(n + m))) |
| 78 | self.state.nested_parse(content, self.content_offset, pnode) |
| 79 | if pnode.children and isinstance(pnode[0], nodes.paragraph): |
| 80 | pnode[0].insert(0, add_text) |
| 81 | pnode[0].insert(1, nodes.Text(' ')) |
| 82 | else: |
| 83 | pnode.insert(0, nodes.paragraph('', '', add_text)) |
| 84 | return [pnode] |
| 85 | |
| 86 | |
Georg Brandl | 8a1caa2 | 2010-07-29 16:01:11 +0000 | [diff] [blame] | 87 | # Support for documenting decorators |
| 88 | |
| 89 | from sphinx import addnodes |
| 90 | from sphinx.domains.python import PyModulelevel, PyClassmember |
| 91 | |
| 92 | class PyDecoratorMixin(object): |
| 93 | def handle_signature(self, sig, signode): |
| 94 | ret = super(PyDecoratorMixin, self).handle_signature(sig, signode) |
| 95 | signode.insert(0, addnodes.desc_addname('@', '@')) |
| 96 | return ret |
| 97 | |
| 98 | def needs_arglist(self): |
| 99 | return False |
| 100 | |
| 101 | class PyDecoratorFunction(PyDecoratorMixin, PyModulelevel): |
| 102 | def run(self): |
| 103 | # a decorator function is a function after all |
| 104 | self.name = 'py:function' |
| 105 | return PyModulelevel.run(self) |
| 106 | |
| 107 | class PyDecoratorMethod(PyDecoratorMixin, PyClassmember): |
| 108 | def run(self): |
| 109 | self.name = 'py:method' |
| 110 | return PyClassmember.run(self) |
| 111 | |
| 112 | |
Georg Brandl | 281d6ba | 2010-11-12 08:57:12 +0000 | [diff] [blame] | 113 | # Support for documenting version of removal in deprecations |
| 114 | |
| 115 | from sphinx.locale import versionlabels |
| 116 | from sphinx.util.compat import Directive |
| 117 | |
| 118 | versionlabels['deprecated-removed'] = \ |
| 119 | 'Deprecated since version %s, will be removed in version %s' |
| 120 | |
| 121 | class DeprecatedRemoved(Directive): |
| 122 | has_content = True |
| 123 | required_arguments = 2 |
| 124 | optional_arguments = 1 |
| 125 | final_argument_whitespace = True |
| 126 | option_spec = {} |
| 127 | |
| 128 | def run(self): |
| 129 | node = addnodes.versionmodified() |
| 130 | node.document = self.state.document |
| 131 | node['type'] = 'deprecated-removed' |
| 132 | version = (self.arguments[0], self.arguments[1]) |
| 133 | node['version'] = version |
| 134 | if len(self.arguments) == 3: |
| 135 | inodes, messages = self.state.inline_text(self.arguments[2], |
| 136 | self.lineno+1) |
| 137 | node.extend(inodes) |
| 138 | if self.content: |
| 139 | self.state.nested_parse(self.content, self.content_offset, node) |
| 140 | ret = [node] + messages |
| 141 | else: |
| 142 | ret = [node] |
| 143 | env = self.state.document.settings.env |
| 144 | env.note_versionchange('deprecated', version[0], node, self.lineno) |
| 145 | return ret |
| 146 | |
| 147 | |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 148 | # Support for building "topic help" for pydoc |
| 149 | |
| 150 | pydoc_topic_labels = [ |
| 151 | 'assert', 'assignment', 'atom-identifiers', 'atom-literals', |
| 152 | 'attribute-access', 'attribute-references', 'augassign', 'binary', |
| 153 | 'bitwise', 'bltin-code-objects', 'bltin-ellipsis-object', |
Benjamin Peterson | 0d31d58 | 2010-06-06 02:44:41 +0000 | [diff] [blame] | 154 | 'bltin-null-object', 'bltin-type-objects', 'booleans', |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 155 | 'break', 'callable-types', 'calls', 'class', 'comparisons', 'compound', |
| 156 | 'context-managers', 'continue', 'conversions', 'customization', 'debugger', |
| 157 | 'del', 'dict', 'dynamic-features', 'else', 'exceptions', 'execmodel', |
| 158 | 'exprlists', 'floating', 'for', 'formatstrings', 'function', 'global', |
| 159 | 'id-classes', 'identifiers', 'if', 'imaginary', 'import', 'in', 'integers', |
Benjamin Peterson | f5a3d69 | 2010-08-31 14:31:01 +0000 | [diff] [blame] | 160 | 'lambda', 'lists', 'naming', 'nonlocal', 'numbers', 'numeric-types', |
| 161 | 'objects', 'operator-summary', 'pass', 'power', 'raise', 'return', |
| 162 | 'sequence-types', 'shifting', 'slicings', 'specialattrs', 'specialnames', |
| 163 | 'string-methods', 'strings', 'subscriptions', 'truth', 'try', 'types', |
| 164 | 'typesfunctions', 'typesmapping', 'typesmethods', 'typesmodules', |
| 165 | 'typesseq', 'typesseq-mutable', 'unary', 'while', 'with', 'yield' |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 166 | ] |
| 167 | |
| 168 | from os import path |
| 169 | from time import asctime |
| 170 | from pprint import pformat |
| 171 | from docutils.io import StringOutput |
| 172 | from docutils.utils import new_document |
Benjamin Peterson | 6e43fb1 | 2008-12-21 00:16:13 +0000 | [diff] [blame] | 173 | |
Benjamin Peterson | 28d88b4 | 2009-01-09 03:03:23 +0000 | [diff] [blame] | 174 | from sphinx.builders import Builder |
| 175 | from sphinx.writers.text import TextWriter |
Georg Brandl | db6f108 | 2008-12-01 23:02:51 +0000 | [diff] [blame] | 176 | |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 177 | |
| 178 | class PydocTopicsBuilder(Builder): |
| 179 | name = 'pydoc-topics' |
| 180 | |
| 181 | def init(self): |
| 182 | self.topics = {} |
| 183 | |
| 184 | def get_outdated_docs(self): |
| 185 | return 'all pydoc topics' |
| 186 | |
| 187 | def get_target_uri(self, docname, typ=None): |
| 188 | return '' # no URIs |
| 189 | |
| 190 | def write(self, *ignored): |
| 191 | writer = TextWriter(self) |
Benjamin Peterson | 5879d41 | 2009-03-30 14:51:56 +0000 | [diff] [blame] | 192 | for label in self.status_iterator(pydoc_topic_labels, |
| 193 | 'building topics... ', |
| 194 | length=len(pydoc_topic_labels)): |
Georg Brandl | 80ff2ad | 2010-07-31 08:27:46 +0000 | [diff] [blame] | 195 | if label not in self.env.domaindata['std']['labels']: |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 196 | self.warn('label %r not in documentation' % label) |
| 197 | continue |
Georg Brandl | 80ff2ad | 2010-07-31 08:27:46 +0000 | [diff] [blame] | 198 | docname, labelid, sectname = self.env.domaindata['std']['labels'][label] |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 199 | doctree = self.env.get_and_resolve_doctree(docname, self) |
| 200 | document = new_document('<section node>') |
| 201 | document.append(doctree.ids[labelid]) |
| 202 | destination = StringOutput(encoding='utf-8') |
| 203 | writer.write(document, destination) |
| 204 | self.topics[label] = str(writer.output) |
| 205 | |
| 206 | def finish(self): |
Georg Brandl | 5617db8 | 2009-04-27 16:28:57 +0000 | [diff] [blame] | 207 | f = open(path.join(self.outdir, 'topics.py'), 'w') |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 208 | try: |
| 209 | f.write('# Autogenerated by Sphinx on %s\n' % asctime()) |
| 210 | f.write('topics = ' + pformat(self.topics) + '\n') |
| 211 | finally: |
| 212 | f.close() |
| 213 | |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 214 | |
Benjamin Peterson | 28d88b4 | 2009-01-09 03:03:23 +0000 | [diff] [blame] | 215 | # Support for checking for suspicious markup |
| 216 | |
| 217 | import suspicious |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 218 | |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 219 | |
Georg Brandl | f0dd6a6 | 2008-07-23 15:19:11 +0000 | [diff] [blame] | 220 | # Support for documenting Opcodes |
| 221 | |
| 222 | import re |
Georg Brandl | f0dd6a6 | 2008-07-23 15:19:11 +0000 | [diff] [blame] | 223 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 224 | opcode_sig_re = re.compile(r'(\w+(?:\+\d)?)(?:\s*\((.*)\))?') |
Georg Brandl | f0dd6a6 | 2008-07-23 15:19:11 +0000 | [diff] [blame] | 225 | |
| 226 | def parse_opcode_signature(env, sig, signode): |
| 227 | """Transform an opcode signature into RST nodes.""" |
| 228 | m = opcode_sig_re.match(sig) |
| 229 | if m is None: |
| 230 | raise ValueError |
| 231 | opname, arglist = m.groups() |
| 232 | signode += addnodes.desc_name(opname, opname) |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 233 | if arglist is not None: |
| 234 | paramlist = addnodes.desc_parameterlist() |
| 235 | signode += paramlist |
| 236 | paramlist += addnodes.desc_parameter(arglist, arglist) |
Georg Brandl | f0dd6a6 | 2008-07-23 15:19:11 +0000 | [diff] [blame] | 237 | return opname.strip() |
| 238 | |
| 239 | |
Georg Brandl | 281d6ba | 2010-11-12 08:57:12 +0000 | [diff] [blame] | 240 | # Support for documenting pdb commands |
| 241 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 242 | pdbcmd_sig_re = re.compile(r'([a-z()!]+)\s*(.*)') |
| 243 | |
| 244 | # later... |
| 245 | #pdbargs_tokens_re = re.compile(r'''[a-zA-Z]+ | # identifiers |
| 246 | # [.,:]+ | # punctuation |
| 247 | # [\[\]()] | # parens |
| 248 | # \s+ # whitespace |
| 249 | # ''', re.X) |
| 250 | |
| 251 | def parse_pdb_command(env, sig, signode): |
| 252 | """Transform a pdb command signature into RST nodes.""" |
| 253 | m = pdbcmd_sig_re.match(sig) |
| 254 | if m is None: |
| 255 | raise ValueError |
| 256 | name, args = m.groups() |
| 257 | fullname = name.replace('(', '').replace(')', '') |
| 258 | signode += addnodes.desc_name(name, name) |
| 259 | if args: |
| 260 | signode += addnodes.desc_addname(' '+args, ' '+args) |
| 261 | return fullname |
| 262 | |
| 263 | |
Martin v. Löwis | 5680d0c | 2008-04-10 03:06:53 +0000 | [diff] [blame] | 264 | def setup(app): |
| 265 | app.add_role('issue', issue_role) |
Georg Brandl | 6881886 | 2010-11-06 07:19:35 +0000 | [diff] [blame] | 266 | app.add_role('source', source_role) |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 267 | app.add_directive('impl-detail', ImplementationDetail) |
Georg Brandl | 281d6ba | 2010-11-12 08:57:12 +0000 | [diff] [blame] | 268 | app.add_directive('deprecated-removed', DeprecatedRemoved) |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 269 | app.add_builder(PydocTopicsBuilder) |
Benjamin Peterson | 28d88b4 | 2009-01-09 03:03:23 +0000 | [diff] [blame] | 270 | app.add_builder(suspicious.CheckSuspiciousMarkupBuilder) |
Georg Brandl | f0dd6a6 | 2008-07-23 15:19:11 +0000 | [diff] [blame] | 271 | app.add_description_unit('opcode', 'opcode', '%s (opcode)', |
| 272 | parse_opcode_signature) |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 273 | app.add_description_unit('pdbcommand', 'pdbcmd', '%s (pdb command)', |
| 274 | parse_pdb_command) |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 275 | app.add_description_unit('2to3fixer', '2to3fixer', '%s (2to3 fixer)') |
Georg Brandl | 8a1caa2 | 2010-07-29 16:01:11 +0000 | [diff] [blame] | 276 | app.add_directive_to_domain('py', 'decorator', PyDecoratorFunction) |
| 277 | app.add_directive_to_domain('py', 'decoratormethod', PyDecoratorMethod) |