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