use the awesome new status iterator
[python.git] / Doc / tools / sphinxext / pyspecific.py
blobf00a89975242e84fa2d91390046bd33cf8a87643
1 # -*- coding: utf-8 -*-
2 """
3 pyspecific.py
4 ~~~~~~~~~~~~~
6 Sphinx extension with Python doc-specific markup.
8 :copyright: 2008 by Georg Brandl.
9 :license: Python license.
10 """
12 ISSUE_URI = 'http://bugs.python.org/issue%s'
14 from docutils import nodes, utils
16 # monkey-patch reST parser to disable alphabetic and roman enumerated lists
17 from docutils.parsers.rst.states import Body
18 Body.enum.converters['loweralpha'] = \
19 Body.enum.converters['upperalpha'] = \
20 Body.enum.converters['lowerroman'] = \
21 Body.enum.converters['upperroman'] = lambda x: None
24 def issue_role(typ, rawtext, text, lineno, inliner, options={}, content=[]):
25 issue = utils.unescape(text)
26 text = 'issue ' + issue
27 refnode = nodes.reference(text, text, refuri=ISSUE_URI % issue)
28 return [refnode], []
31 # Support for building "topic help" for pydoc
33 pydoc_topic_labels = [
34 'assert', 'assignment', 'atom-identifiers', 'atom-literals',
35 'attribute-access', 'attribute-references', 'augassign', 'binary',
36 'bitwise', 'bltin-code-objects', 'bltin-ellipsis-object',
37 'bltin-file-objects', 'bltin-null-object', 'bltin-type-objects', 'booleans',
38 'break', 'callable-types', 'calls', 'class', 'coercion-rules',
39 'comparisons', 'compound', 'context-managers', 'continue', 'conversions',
40 'customization', 'debugger', 'del', 'dict', 'dynamic-features', 'else',
41 'exceptions', 'exec', 'execmodel', 'exprlists', 'floating', 'for',
42 'formatstrings', 'function', 'global', 'id-classes', 'identifiers', 'if',
43 'imaginary', 'import', 'in', 'integers', 'lambda', 'lists', 'naming',
44 'numbers', 'numeric-types', 'objects', 'operator-summary', 'pass', 'power',
45 'print', 'raise', 'return', 'sequence-methods', 'sequence-types',
46 'shifting', 'slicings', 'specialattrs', 'specialnames',
47 'string-conversions', 'string-methods', 'strings', 'subscriptions', 'truth',
48 'try', 'types', 'typesfunctions', 'typesmapping', 'typesmethods',
49 'typesmodules', 'typesseq', 'typesseq-mutable', 'unary', 'while', 'with',
50 'yield'
53 from os import path
54 from time import asctime
55 from pprint import pformat
56 from docutils.io import StringOutput
57 from docutils.utils import new_document
59 from sphinx.builders import Builder
60 from sphinx.writers.text import TextWriter
63 class PydocTopicsBuilder(Builder):
64 name = 'pydoc-topics'
66 def init(self):
67 self.topics = {}
69 def get_outdated_docs(self):
70 return 'all pydoc topics'
72 def get_target_uri(self, docname, typ=None):
73 return '' # no URIs
75 def write(self, *ignored):
76 writer = TextWriter(self)
77 for label in self.status_iterator(pydoc_topic_labels,
78 'building topics... ',
79 length=len(pydoc_topic_labels)):
80 if label not in self.env.labels:
81 self.warn('label %r not in documentation' % label)
82 continue
83 docname, labelid, sectname = self.env.labels[label]
84 doctree = self.env.get_and_resolve_doctree(docname, self)
85 document = new_document('<section node>')
86 document.append(doctree.ids[labelid])
87 destination = StringOutput(encoding='utf-8')
88 writer.write(document, destination)
89 self.topics[label] = writer.output
91 def finish(self):
92 f = open(path.join(self.outdir, 'pydoc_topics.py'), 'w')
93 try:
94 f.write('# Autogenerated by Sphinx on %s\n' % asctime())
95 f.write('topics = ' + pformat(self.topics) + '\n')
96 finally:
97 f.close()
99 # Support for checking for suspicious markup
101 import suspicious
103 # Support for documenting Opcodes
105 import re
106 from sphinx import addnodes
108 opcode_sig_re = re.compile(r'(\w+(?:\+\d)?)\s*\((.*)\)')
110 def parse_opcode_signature(env, sig, signode):
111 """Transform an opcode signature into RST nodes."""
112 m = opcode_sig_re.match(sig)
113 if m is None:
114 raise ValueError
115 opname, arglist = m.groups()
116 signode += addnodes.desc_name(opname, opname)
117 paramlist = addnodes.desc_parameterlist()
118 signode += paramlist
119 paramlist += addnodes.desc_parameter(arglist, arglist)
120 return opname.strip()
123 def setup(app):
124 app.add_role('issue', issue_role)
125 app.add_builder(PydocTopicsBuilder)
126 app.add_builder(suspicious.CheckSuspiciousMarkupBuilder)
127 app.add_description_unit('opcode', 'opcode', '%s (opcode)',
128 parse_opcode_signature)
129 app.add_description_unit('2to3fixer', '2to3fixer', '%s (2to3 fixer)')