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