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