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 | 7ed509a | 2014-01-21 19:20:31 +0100 | [diff] [blame] | 8 | :copyright: 2008-2014 by Georg Brandl. |
Martin v. Löwis | 5680d0c | 2008-04-10 03:06:53 +0000 | [diff] [blame] | 9 | :license: Python license. |
| 10 | """ |
| 11 | |
Georg Brandl | 35903c8 | 2014-10-30 22:55:13 +0100 | [diff] [blame] | 12 | import re |
Victor Stinner | 272d888 | 2017-06-16 08:59:01 +0200 | [diff] [blame] | 13 | import io |
Georg Brandl | 35903c8 | 2014-10-30 22:55:13 +0100 | [diff] [blame] | 14 | from os import path |
| 15 | from time import asctime |
| 16 | from pprint import pformat |
| 17 | from docutils.io import StringOutput |
Ned Deily | 50f5816 | 2017-07-15 15:28:02 -0400 | [diff] [blame] | 18 | from docutils.parsers.rst import Directive |
Georg Brandl | 35903c8 | 2014-10-30 22:55:13 +0100 | [diff] [blame] | 19 | from docutils.utils import new_document |
Martin v. Löwis | 5680d0c | 2008-04-10 03:06:53 +0000 | [diff] [blame] | 20 | |
| 21 | from docutils import nodes, utils |
Georg Brandl | 239990d | 2013-10-12 20:50:21 +0200 | [diff] [blame] | 22 | |
Georg Brandl | 35903c8 | 2014-10-30 22:55:13 +0100 | [diff] [blame] | 23 | from sphinx import addnodes |
| 24 | from sphinx.builders import Builder |
INADA Naoki | c351ce6 | 2017-03-08 19:07:13 +0900 | [diff] [blame] | 25 | from sphinx.locale import translators |
Miss Islington (bot) | 7899153 | 2018-04-16 23:32:43 -0700 | [diff] [blame] | 26 | from sphinx.util import status_iterator |
Georg Brandl | 6881886 | 2010-11-06 07:19:35 +0000 | [diff] [blame] | 27 | from sphinx.util.nodes import split_explicit_title |
Georg Brandl | 239990d | 2013-10-12 20:50:21 +0200 | [diff] [blame] | 28 | from sphinx.writers.html import HTMLTranslator |
Miss Islington (bot) | 7899153 | 2018-04-16 23:32:43 -0700 | [diff] [blame] | 29 | from sphinx.writers.text import TextWriter, TextTranslator |
Georg Brandl | 239990d | 2013-10-12 20:50:21 +0200 | [diff] [blame] | 30 | from sphinx.writers.latex import LaTeXTranslator |
Georg Brandl | 35903c8 | 2014-10-30 22:55:13 +0100 | [diff] [blame] | 31 | from sphinx.domains.python import PyModulelevel, PyClassmember |
| 32 | |
| 33 | # Support for checking for suspicious markup |
| 34 | |
| 35 | import suspicious |
| 36 | |
| 37 | |
| 38 | ISSUE_URI = 'https://bugs.python.org/issue%s' |
Ned Deily | 7f38637 | 2018-01-31 18:24:44 -0500 | [diff] [blame] | 39 | SOURCE_URI = 'https://github.com/python/cpython/tree/3.7/%s' |
Martin v. Löwis | 5680d0c | 2008-04-10 03:06:53 +0000 | [diff] [blame] | 40 | |
Benjamin Peterson | 5c6d787 | 2009-02-06 02:40:07 +0000 | [diff] [blame] | 41 | # monkey-patch reST parser to disable alphabetic and roman enumerated lists |
| 42 | from docutils.parsers.rst.states import Body |
| 43 | Body.enum.converters['loweralpha'] = \ |
| 44 | Body.enum.converters['upperalpha'] = \ |
| 45 | Body.enum.converters['lowerroman'] = \ |
| 46 | Body.enum.converters['upperroman'] = lambda x: None |
| 47 | |
Georg Brandl | 83e51f4 | 2012-10-10 16:45:11 +0200 | [diff] [blame] | 48 | # monkey-patch HTML and LaTeX translators to keep doctest blocks in the |
| 49 | # doctest docs themselves |
| 50 | orig_visit_literal_block = HTMLTranslator.visit_literal_block |
Georg Brandl | 35903c8 | 2014-10-30 22:55:13 +0100 | [diff] [blame] | 51 | orig_depart_literal_block = LaTeXTranslator.depart_literal_block |
| 52 | |
| 53 | |
Georg Brandl | 83e51f4 | 2012-10-10 16:45:11 +0200 | [diff] [blame] | 54 | def new_visit_literal_block(self, node): |
| 55 | meta = self.builder.env.metadata[self.builder.current_docname] |
| 56 | old_trim_doctest_flags = self.highlighter.trim_doctest_flags |
| 57 | if 'keepdoctest' in meta: |
| 58 | self.highlighter.trim_doctest_flags = False |
| 59 | try: |
| 60 | orig_visit_literal_block(self, node) |
| 61 | finally: |
| 62 | self.highlighter.trim_doctest_flags = old_trim_doctest_flags |
| 63 | |
Georg Brandl | 83e51f4 | 2012-10-10 16:45:11 +0200 | [diff] [blame] | 64 | |
Georg Brandl | 83e51f4 | 2012-10-10 16:45:11 +0200 | [diff] [blame] | 65 | def new_depart_literal_block(self, node): |
| 66 | meta = self.builder.env.metadata[self.curfilestack[-1]] |
| 67 | old_trim_doctest_flags = self.highlighter.trim_doctest_flags |
| 68 | if 'keepdoctest' in meta: |
| 69 | self.highlighter.trim_doctest_flags = False |
| 70 | try: |
| 71 | orig_depart_literal_block(self, node) |
| 72 | finally: |
| 73 | self.highlighter.trim_doctest_flags = old_trim_doctest_flags |
| 74 | |
Georg Brandl | 35903c8 | 2014-10-30 22:55:13 +0100 | [diff] [blame] | 75 | |
| 76 | HTMLTranslator.visit_literal_block = new_visit_literal_block |
Georg Brandl | 83e51f4 | 2012-10-10 16:45:11 +0200 | [diff] [blame] | 77 | LaTeXTranslator.depart_literal_block = new_depart_literal_block |
Benjamin Peterson | 5c6d787 | 2009-02-06 02:40:07 +0000 | [diff] [blame] | 78 | |
Georg Brandl | 35903c8 | 2014-10-30 22:55:13 +0100 | [diff] [blame] | 79 | |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 80 | # Support for marking up and linking to bugs.python.org issues |
| 81 | |
Martin v. Löwis | 5680d0c | 2008-04-10 03:06:53 +0000 | [diff] [blame] | 82 | def issue_role(typ, rawtext, text, lineno, inliner, options={}, content=[]): |
| 83 | issue = utils.unescape(text) |
Brett Cannon | 79ab8be | 2017-02-10 15:10:13 -0800 | [diff] [blame] | 84 | text = 'bpo-' + issue |
Martin v. Löwis | 5680d0c | 2008-04-10 03:06:53 +0000 | [diff] [blame] | 85 | refnode = nodes.reference(text, text, refuri=ISSUE_URI % issue) |
| 86 | return [refnode], [] |
| 87 | |
| 88 | |
Georg Brandl | 6881886 | 2010-11-06 07:19:35 +0000 | [diff] [blame] | 89 | # Support for linking to Python source files easily |
| 90 | |
| 91 | def source_role(typ, rawtext, text, lineno, inliner, options={}, content=[]): |
| 92 | has_t, title, target = split_explicit_title(text) |
| 93 | title = utils.unescape(title) |
| 94 | target = utils.unescape(target) |
| 95 | refnode = nodes.reference(title, title, refuri=SOURCE_URI % target) |
| 96 | return [refnode], [] |
| 97 | |
| 98 | |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 99 | # Support for marking up implementation details |
| 100 | |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 101 | class ImplementationDetail(Directive): |
| 102 | |
| 103 | has_content = True |
| 104 | required_arguments = 0 |
| 105 | optional_arguments = 1 |
| 106 | final_argument_whitespace = True |
| 107 | |
INADA Naoki | c351ce6 | 2017-03-08 19:07:13 +0900 | [diff] [blame] | 108 | # This text is copied to templates/dummy.html |
| 109 | label_text = 'CPython implementation detail:' |
| 110 | |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 111 | def run(self): |
| 112 | pnode = nodes.compound(classes=['impl-detail']) |
INADA Naoki | c351ce6 | 2017-03-08 19:07:13 +0900 | [diff] [blame] | 113 | label = translators['sphinx'].gettext(self.label_text) |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 114 | content = self.content |
INADA Naoki | c351ce6 | 2017-03-08 19:07:13 +0900 | [diff] [blame] | 115 | add_text = nodes.strong(label, label) |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 116 | if self.arguments: |
| 117 | n, m = self.state.inline_text(self.arguments[0], self.lineno) |
| 118 | pnode.append(nodes.paragraph('', '', *(n + m))) |
| 119 | self.state.nested_parse(content, self.content_offset, pnode) |
| 120 | if pnode.children and isinstance(pnode[0], nodes.paragraph): |
INADA Naoki | c351ce6 | 2017-03-08 19:07:13 +0900 | [diff] [blame] | 121 | content = nodes.inline(pnode[0].rawsource, translatable=True) |
| 122 | content.source = pnode[0].source |
| 123 | content.line = pnode[0].line |
| 124 | content += pnode[0].children |
| 125 | pnode[0].replace_self(nodes.paragraph('', '', content, |
| 126 | translatable=False)) |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 127 | pnode[0].insert(0, add_text) |
| 128 | pnode[0].insert(1, nodes.Text(' ')) |
| 129 | else: |
| 130 | pnode.insert(0, nodes.paragraph('', '', add_text)) |
| 131 | return [pnode] |
| 132 | |
| 133 | |
Georg Brandl | 8a1caa2 | 2010-07-29 16:01:11 +0000 | [diff] [blame] | 134 | # Support for documenting decorators |
| 135 | |
Georg Brandl | 8a1caa2 | 2010-07-29 16:01:11 +0000 | [diff] [blame] | 136 | class PyDecoratorMixin(object): |
| 137 | def handle_signature(self, sig, signode): |
| 138 | ret = super(PyDecoratorMixin, self).handle_signature(sig, signode) |
| 139 | signode.insert(0, addnodes.desc_addname('@', '@')) |
| 140 | return ret |
| 141 | |
| 142 | def needs_arglist(self): |
| 143 | return False |
| 144 | |
Georg Brandl | 35903c8 | 2014-10-30 22:55:13 +0100 | [diff] [blame] | 145 | |
Georg Brandl | 8a1caa2 | 2010-07-29 16:01:11 +0000 | [diff] [blame] | 146 | class PyDecoratorFunction(PyDecoratorMixin, PyModulelevel): |
| 147 | def run(self): |
| 148 | # a decorator function is a function after all |
| 149 | self.name = 'py:function' |
| 150 | return PyModulelevel.run(self) |
| 151 | |
Georg Brandl | 35903c8 | 2014-10-30 22:55:13 +0100 | [diff] [blame] | 152 | |
Georg Brandl | 8a1caa2 | 2010-07-29 16:01:11 +0000 | [diff] [blame] | 153 | class PyDecoratorMethod(PyDecoratorMixin, PyClassmember): |
| 154 | def run(self): |
| 155 | self.name = 'py:method' |
| 156 | return PyClassmember.run(self) |
| 157 | |
| 158 | |
Victor Stinner | bdd574d | 2015-02-12 22:49:18 +0100 | [diff] [blame] | 159 | class PyCoroutineMixin(object): |
| 160 | def handle_signature(self, sig, signode): |
| 161 | ret = super(PyCoroutineMixin, self).handle_signature(sig, signode) |
Victor Stinner | bdd574d | 2015-02-12 22:49:18 +0100 | [diff] [blame] | 162 | signode.insert(0, addnodes.desc_annotation('coroutine ', 'coroutine ')) |
| 163 | return ret |
| 164 | |
Victor Stinner | bdd574d | 2015-02-12 22:49:18 +0100 | [diff] [blame] | 165 | |
Miss Islington (bot) | 73c0006 | 2018-09-18 15:09:51 -0700 | [diff] [blame^] | 166 | class PyAwaitableMixin(object): |
| 167 | def handle_signature(self, sig, signode): |
| 168 | ret = super(PyAwaitableMixin, self).handle_signature(sig, signode) |
| 169 | signode.insert(0, addnodes.desc_annotation('awaitable ', 'awaitable ')) |
| 170 | return ret |
| 171 | |
| 172 | |
Victor Stinner | bdd574d | 2015-02-12 22:49:18 +0100 | [diff] [blame] | 173 | class PyCoroutineFunction(PyCoroutineMixin, PyModulelevel): |
| 174 | def run(self): |
Victor Stinner | bdd574d | 2015-02-12 22:49:18 +0100 | [diff] [blame] | 175 | self.name = 'py:function' |
| 176 | return PyModulelevel.run(self) |
| 177 | |
| 178 | |
| 179 | class PyCoroutineMethod(PyCoroutineMixin, PyClassmember): |
| 180 | def run(self): |
| 181 | self.name = 'py:method' |
| 182 | return PyClassmember.run(self) |
| 183 | |
| 184 | |
Miss Islington (bot) | 73c0006 | 2018-09-18 15:09:51 -0700 | [diff] [blame^] | 185 | class PyAwaitableFunction(PyAwaitableMixin, PyClassmember): |
| 186 | def run(self): |
| 187 | self.name = 'py:function' |
| 188 | return PyClassmember.run(self) |
| 189 | |
| 190 | |
| 191 | class PyAwaitableMethod(PyAwaitableMixin, PyClassmember): |
| 192 | def run(self): |
| 193 | self.name = 'py:method' |
| 194 | return PyClassmember.run(self) |
| 195 | |
| 196 | |
Berker Peksag | 6e9d2e6 | 2015-12-08 12:14:50 +0200 | [diff] [blame] | 197 | class PyAbstractMethod(PyClassmember): |
| 198 | |
| 199 | def handle_signature(self, sig, signode): |
| 200 | ret = super(PyAbstractMethod, self).handle_signature(sig, signode) |
| 201 | signode.insert(0, addnodes.desc_annotation('abstractmethod ', |
| 202 | 'abstractmethod ')) |
| 203 | return ret |
| 204 | |
| 205 | def run(self): |
| 206 | self.name = 'py:method' |
| 207 | return PyClassmember.run(self) |
| 208 | |
| 209 | |
Georg Brandl | 281d6ba | 2010-11-12 08:57:12 +0000 | [diff] [blame] | 210 | # Support for documenting version of removal in deprecations |
| 211 | |
Georg Brandl | 281d6ba | 2010-11-12 08:57:12 +0000 | [diff] [blame] | 212 | class DeprecatedRemoved(Directive): |
| 213 | has_content = True |
| 214 | required_arguments = 2 |
| 215 | optional_arguments = 1 |
| 216 | final_argument_whitespace = True |
| 217 | option_spec = {} |
| 218 | |
Miss Islington (bot) | c673a62 | 2018-02-23 04:08:45 -0800 | [diff] [blame] | 219 | _label = 'Deprecated since version {deprecated}, will be removed in version {removed}' |
Georg Brandl | 7ed509a | 2014-01-21 19:20:31 +0100 | [diff] [blame] | 220 | |
Georg Brandl | 281d6ba | 2010-11-12 08:57:12 +0000 | [diff] [blame] | 221 | def run(self): |
| 222 | node = addnodes.versionmodified() |
| 223 | node.document = self.state.document |
| 224 | node['type'] = 'deprecated-removed' |
| 225 | version = (self.arguments[0], self.arguments[1]) |
| 226 | node['version'] = version |
Miss Islington (bot) | c673a62 | 2018-02-23 04:08:45 -0800 | [diff] [blame] | 227 | label = translators['sphinx'].gettext(self._label) |
| 228 | text = label.format(deprecated=self.arguments[0], removed=self.arguments[1]) |
Georg Brandl | 281d6ba | 2010-11-12 08:57:12 +0000 | [diff] [blame] | 229 | if len(self.arguments) == 3: |
| 230 | inodes, messages = self.state.inline_text(self.arguments[2], |
| 231 | self.lineno+1) |
Miss Islington (bot) | c673a62 | 2018-02-23 04:08:45 -0800 | [diff] [blame] | 232 | para = nodes.paragraph(self.arguments[2], '', *inodes, translatable=False) |
Georg Brandl | 7ed509a | 2014-01-21 19:20:31 +0100 | [diff] [blame] | 233 | node.append(para) |
Georg Brandl | 281d6ba | 2010-11-12 08:57:12 +0000 | [diff] [blame] | 234 | else: |
Georg Brandl | 7ed509a | 2014-01-21 19:20:31 +0100 | [diff] [blame] | 235 | messages = [] |
| 236 | if self.content: |
| 237 | self.state.nested_parse(self.content, self.content_offset, node) |
Berker Peksag | eb1a3cd | 2014-11-08 22:40:22 +0200 | [diff] [blame] | 238 | if len(node): |
Georg Brandl | 7ed509a | 2014-01-21 19:20:31 +0100 | [diff] [blame] | 239 | if isinstance(node[0], nodes.paragraph) and node[0].rawsource: |
| 240 | content = nodes.inline(node[0].rawsource, translatable=True) |
| 241 | content.source = node[0].source |
| 242 | content.line = node[0].line |
| 243 | content += node[0].children |
Miss Islington (bot) | c673a62 | 2018-02-23 04:08:45 -0800 | [diff] [blame] | 244 | node[0].replace_self(nodes.paragraph('', '', content, translatable=False)) |
Berker Peksag | eb1a3cd | 2014-11-08 22:40:22 +0200 | [diff] [blame] | 245 | node[0].insert(0, nodes.inline('', '%s: ' % text, |
| 246 | classes=['versionmodified'])) |
Ned Deily | b682fd3 | 2014-09-22 14:44:22 -0700 | [diff] [blame] | 247 | else: |
Georg Brandl | 7ed509a | 2014-01-21 19:20:31 +0100 | [diff] [blame] | 248 | para = nodes.paragraph('', '', |
Georg Brandl | 35903c8 | 2014-10-30 22:55:13 +0100 | [diff] [blame] | 249 | nodes.inline('', '%s.' % text, |
Miss Islington (bot) | c673a62 | 2018-02-23 04:08:45 -0800 | [diff] [blame] | 250 | classes=['versionmodified']), |
| 251 | translatable=False) |
Berker Peksag | eb1a3cd | 2014-11-08 22:40:22 +0200 | [diff] [blame] | 252 | node.append(para) |
Georg Brandl | 281d6ba | 2010-11-12 08:57:12 +0000 | [diff] [blame] | 253 | env = self.state.document.settings.env |
| 254 | env.note_versionchange('deprecated', version[0], node, self.lineno) |
Georg Brandl | 7ed509a | 2014-01-21 19:20:31 +0100 | [diff] [blame] | 255 | return [node] + messages |
Georg Brandl | 281d6ba | 2010-11-12 08:57:12 +0000 | [diff] [blame] | 256 | |
| 257 | |
Georg Brandl | 2cac28b | 2012-09-30 15:10:06 +0200 | [diff] [blame] | 258 | # Support for including Misc/NEWS |
| 259 | |
Brett Cannon | 79ab8be | 2017-02-10 15:10:13 -0800 | [diff] [blame] | 260 | issue_re = re.compile('(?:[Ii]ssue #|bpo-)([0-9]+)') |
Georg Brandl | 44d0c21 | 2012-10-01 19:08:50 +0200 | [diff] [blame] | 261 | whatsnew_re = re.compile(r"(?im)^what's new in (.*?)\??$") |
Georg Brandl | 2cac28b | 2012-09-30 15:10:06 +0200 | [diff] [blame] | 262 | |
Georg Brandl | 35903c8 | 2014-10-30 22:55:13 +0100 | [diff] [blame] | 263 | |
Georg Brandl | 2cac28b | 2012-09-30 15:10:06 +0200 | [diff] [blame] | 264 | class MiscNews(Directive): |
| 265 | has_content = False |
| 266 | required_arguments = 1 |
| 267 | optional_arguments = 0 |
| 268 | final_argument_whitespace = False |
| 269 | option_spec = {} |
| 270 | |
| 271 | def run(self): |
| 272 | fname = self.arguments[0] |
| 273 | source = self.state_machine.input_lines.source( |
| 274 | self.lineno - self.state_machine.input_offset - 1) |
| 275 | source_dir = path.dirname(path.abspath(source)) |
Georg Brandl | 44d0c21 | 2012-10-01 19:08:50 +0200 | [diff] [blame] | 276 | fpath = path.join(source_dir, fname) |
| 277 | self.state.document.settings.record_dependencies.add(fpath) |
Georg Brandl | 2cac28b | 2012-09-30 15:10:06 +0200 | [diff] [blame] | 278 | try: |
Victor Stinner | 272d888 | 2017-06-16 08:59:01 +0200 | [diff] [blame] | 279 | with io.open(fpath, encoding='utf-8') as fp: |
Georg Brandl | 2cac28b | 2012-09-30 15:10:06 +0200 | [diff] [blame] | 280 | content = fp.read() |
Georg Brandl | 2cac28b | 2012-09-30 15:10:06 +0200 | [diff] [blame] | 281 | except Exception: |
| 282 | text = 'The NEWS file is not available.' |
| 283 | node = nodes.strong(text, text) |
| 284 | return [node] |
Brett Cannon | 79ab8be | 2017-02-10 15:10:13 -0800 | [diff] [blame] | 285 | content = issue_re.sub(r'`bpo-\1 <https://bugs.python.org/issue\1>`__', |
Georg Brandl | 2cac28b | 2012-09-30 15:10:06 +0200 | [diff] [blame] | 286 | content) |
Georg Brandl | 44d0c21 | 2012-10-01 19:08:50 +0200 | [diff] [blame] | 287 | content = whatsnew_re.sub(r'\1', content) |
Georg Brandl | 2cac28b | 2012-09-30 15:10:06 +0200 | [diff] [blame] | 288 | # remove first 3 lines as they are the main heading |
Georg Brandl | 6c47581 | 2012-10-01 19:27:05 +0200 | [diff] [blame] | 289 | lines = ['.. default-role:: obj', ''] + content.splitlines()[3:] |
Georg Brandl | 2cac28b | 2012-09-30 15:10:06 +0200 | [diff] [blame] | 290 | self.state_machine.insert_input(lines, fname) |
| 291 | return [] |
| 292 | |
| 293 | |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 294 | # Support for building "topic help" for pydoc |
| 295 | |
| 296 | pydoc_topic_labels = [ |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 297 | 'assert', 'assignment', 'async', 'atom-identifiers', 'atom-literals', |
| 298 | 'attribute-access', 'attribute-references', 'augassign', 'await', |
| 299 | 'binary', 'bitwise', 'bltin-code-objects', 'bltin-ellipsis-object', |
Benjamin Peterson | 0d31d58 | 2010-06-06 02:44:41 +0000 | [diff] [blame] | 300 | 'bltin-null-object', 'bltin-type-objects', 'booleans', |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 301 | 'break', 'callable-types', 'calls', 'class', 'comparisons', 'compound', |
| 302 | 'context-managers', 'continue', 'conversions', 'customization', 'debugger', |
| 303 | 'del', 'dict', 'dynamic-features', 'else', 'exceptions', 'execmodel', |
| 304 | 'exprlists', 'floating', 'for', 'formatstrings', 'function', 'global', |
| 305 | 'id-classes', 'identifiers', 'if', 'imaginary', 'import', 'in', 'integers', |
Benjamin Peterson | f5a3d69 | 2010-08-31 14:31:01 +0000 | [diff] [blame] | 306 | 'lambda', 'lists', 'naming', 'nonlocal', 'numbers', 'numeric-types', |
| 307 | 'objects', 'operator-summary', 'pass', 'power', 'raise', 'return', |
| 308 | 'sequence-types', 'shifting', 'slicings', 'specialattrs', 'specialnames', |
| 309 | 'string-methods', 'strings', 'subscriptions', 'truth', 'try', 'types', |
| 310 | 'typesfunctions', 'typesmapping', 'typesmethods', 'typesmodules', |
| 311 | 'typesseq', 'typesseq-mutable', 'unary', 'while', 'with', 'yield' |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 312 | ] |
| 313 | |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 314 | |
| 315 | class PydocTopicsBuilder(Builder): |
| 316 | name = 'pydoc-topics' |
| 317 | |
Miss Islington (bot) | 7899153 | 2018-04-16 23:32:43 -0700 | [diff] [blame] | 318 | default_translator_class = TextTranslator |
| 319 | |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 320 | def init(self): |
| 321 | self.topics = {} |
Miss Islington (bot) | 7899153 | 2018-04-16 23:32:43 -0700 | [diff] [blame] | 322 | self.secnumbers = {} |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 323 | |
| 324 | def get_outdated_docs(self): |
| 325 | return 'all pydoc topics' |
| 326 | |
| 327 | def get_target_uri(self, docname, typ=None): |
| 328 | return '' # no URIs |
| 329 | |
| 330 | def write(self, *ignored): |
| 331 | writer = TextWriter(self) |
Miss Islington (bot) | 7899153 | 2018-04-16 23:32:43 -0700 | [diff] [blame] | 332 | for label in status_iterator(pydoc_topic_labels, |
| 333 | 'building topics... ', |
| 334 | length=len(pydoc_topic_labels)): |
Georg Brandl | 80ff2ad | 2010-07-31 08:27:46 +0000 | [diff] [blame] | 335 | if label not in self.env.domaindata['std']['labels']: |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 336 | self.warn('label %r not in documentation' % label) |
| 337 | continue |
Georg Brandl | 80ff2ad | 2010-07-31 08:27:46 +0000 | [diff] [blame] | 338 | docname, labelid, sectname = self.env.domaindata['std']['labels'][label] |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 339 | doctree = self.env.get_and_resolve_doctree(docname, self) |
| 340 | document = new_document('<section node>') |
| 341 | document.append(doctree.ids[labelid]) |
| 342 | destination = StringOutput(encoding='utf-8') |
| 343 | writer.write(document, destination) |
Ned Deily | b682fd3 | 2014-09-22 14:44:22 -0700 | [diff] [blame] | 344 | self.topics[label] = writer.output |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 345 | |
| 346 | def finish(self): |
Ned Deily | b682fd3 | 2014-09-22 14:44:22 -0700 | [diff] [blame] | 347 | f = open(path.join(self.outdir, 'topics.py'), 'wb') |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 348 | try: |
Ned Deily | b682fd3 | 2014-09-22 14:44:22 -0700 | [diff] [blame] | 349 | f.write('# -*- coding: utf-8 -*-\n'.encode('utf-8')) |
| 350 | f.write(('# Autogenerated by Sphinx on %s\n' % asctime()).encode('utf-8')) |
| 351 | f.write(('topics = ' + pformat(self.topics) + '\n').encode('utf-8')) |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 352 | finally: |
| 353 | f.close() |
| 354 | |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 355 | |
Georg Brandl | f0dd6a6 | 2008-07-23 15:19:11 +0000 | [diff] [blame] | 356 | # Support for documenting Opcodes |
| 357 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 358 | opcode_sig_re = re.compile(r'(\w+(?:\+\d)?)(?:\s*\((.*)\))?') |
Georg Brandl | f0dd6a6 | 2008-07-23 15:19:11 +0000 | [diff] [blame] | 359 | |
Georg Brandl | 35903c8 | 2014-10-30 22:55:13 +0100 | [diff] [blame] | 360 | |
Georg Brandl | f0dd6a6 | 2008-07-23 15:19:11 +0000 | [diff] [blame] | 361 | def parse_opcode_signature(env, sig, signode): |
| 362 | """Transform an opcode signature into RST nodes.""" |
| 363 | m = opcode_sig_re.match(sig) |
| 364 | if m is None: |
| 365 | raise ValueError |
| 366 | opname, arglist = m.groups() |
| 367 | signode += addnodes.desc_name(opname, opname) |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 368 | if arglist is not None: |
| 369 | paramlist = addnodes.desc_parameterlist() |
| 370 | signode += paramlist |
| 371 | paramlist += addnodes.desc_parameter(arglist, arglist) |
Georg Brandl | f0dd6a6 | 2008-07-23 15:19:11 +0000 | [diff] [blame] | 372 | return opname.strip() |
| 373 | |
| 374 | |
Georg Brandl | 281d6ba | 2010-11-12 08:57:12 +0000 | [diff] [blame] | 375 | # Support for documenting pdb commands |
| 376 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 377 | pdbcmd_sig_re = re.compile(r'([a-z()!]+)\s*(.*)') |
| 378 | |
| 379 | # later... |
Georg Brandl | 35903c8 | 2014-10-30 22:55:13 +0100 | [diff] [blame] | 380 | # pdbargs_tokens_re = re.compile(r'''[a-zA-Z]+ | # identifiers |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 381 | # [.,:]+ | # punctuation |
| 382 | # [\[\]()] | # parens |
| 383 | # \s+ # whitespace |
| 384 | # ''', re.X) |
| 385 | |
Georg Brandl | 35903c8 | 2014-10-30 22:55:13 +0100 | [diff] [blame] | 386 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 387 | def parse_pdb_command(env, sig, signode): |
| 388 | """Transform a pdb command signature into RST nodes.""" |
| 389 | m = pdbcmd_sig_re.match(sig) |
| 390 | if m is None: |
| 391 | raise ValueError |
| 392 | name, args = m.groups() |
| 393 | fullname = name.replace('(', '').replace(')', '') |
| 394 | signode += addnodes.desc_name(name, name) |
| 395 | if args: |
| 396 | signode += addnodes.desc_addname(' '+args, ' '+args) |
| 397 | return fullname |
| 398 | |
| 399 | |
Martin v. Löwis | 5680d0c | 2008-04-10 03:06:53 +0000 | [diff] [blame] | 400 | def setup(app): |
| 401 | app.add_role('issue', issue_role) |
Georg Brandl | 6881886 | 2010-11-06 07:19:35 +0000 | [diff] [blame] | 402 | app.add_role('source', source_role) |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 403 | app.add_directive('impl-detail', ImplementationDetail) |
Georg Brandl | 281d6ba | 2010-11-12 08:57:12 +0000 | [diff] [blame] | 404 | app.add_directive('deprecated-removed', DeprecatedRemoved) |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 405 | app.add_builder(PydocTopicsBuilder) |
Benjamin Peterson | 28d88b4 | 2009-01-09 03:03:23 +0000 | [diff] [blame] | 406 | app.add_builder(suspicious.CheckSuspiciousMarkupBuilder) |
Georg Brandl | f0dd6a6 | 2008-07-23 15:19:11 +0000 | [diff] [blame] | 407 | app.add_description_unit('opcode', 'opcode', '%s (opcode)', |
| 408 | parse_opcode_signature) |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 409 | app.add_description_unit('pdbcommand', 'pdbcmd', '%s (pdb command)', |
| 410 | parse_pdb_command) |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 411 | app.add_description_unit('2to3fixer', '2to3fixer', '%s (2to3 fixer)') |
Georg Brandl | 8a1caa2 | 2010-07-29 16:01:11 +0000 | [diff] [blame] | 412 | app.add_directive_to_domain('py', 'decorator', PyDecoratorFunction) |
| 413 | app.add_directive_to_domain('py', 'decoratormethod', PyDecoratorMethod) |
Victor Stinner | bdd574d | 2015-02-12 22:49:18 +0100 | [diff] [blame] | 414 | app.add_directive_to_domain('py', 'coroutinefunction', PyCoroutineFunction) |
| 415 | app.add_directive_to_domain('py', 'coroutinemethod', PyCoroutineMethod) |
Miss Islington (bot) | 73c0006 | 2018-09-18 15:09:51 -0700 | [diff] [blame^] | 416 | app.add_directive_to_domain('py', 'awaitablefunction', PyAwaitableFunction) |
| 417 | app.add_directive_to_domain('py', 'awaitablemethod', PyAwaitableMethod) |
Berker Peksag | 6e9d2e6 | 2015-12-08 12:14:50 +0200 | [diff] [blame] | 418 | app.add_directive_to_domain('py', 'abstractmethod', PyAbstractMethod) |
Georg Brandl | 2cac28b | 2012-09-30 15:10:06 +0200 | [diff] [blame] | 419 | app.add_directive('miscnews', MiscNews) |
Georg Brandl | bae334c | 2014-09-30 22:17:41 +0200 | [diff] [blame] | 420 | return {'version': '1.0', 'parallel_read_safe': True} |