blob: 9a8b897689b3b3de18dc8cd30111d0db4c8e4156 [file] [log] [blame]
Georg Brandlc3051922008-04-09 17:58:56 +00001# -*- coding: utf-8 -*-
2"""
3 pyspecific.py
4 ~~~~~~~~~~~~~
5
6 Sphinx extension with Python doc-specific markup.
7
8 :copyright: 2008 by Georg Brandl.
9 :license: Python license.
10"""
11
12ISSUE_URI = 'http://bugs.python.org/issue%s'
13
14from docutils import nodes, utils
15
16def issue_role(typ, rawtext, text, lineno, inliner, options={}, content=[]):
17 issue = utils.unescape(text)
18 text = 'issue ' + issue
19 refnode = nodes.reference(text, text, refuri=ISSUE_URI % issue)
20 return [refnode], []
21
22
Georg Brandl681001e2008-06-01 20:33:55 +000023# Support for building "topic help" for pydoc
24
25pydoc_topic_labels = [
26 'assert', 'assignment', 'atom-identifiers', 'atom-literals',
27 'attribute-access', 'attribute-references', 'augassign', 'binary',
28 'bitwise', 'bltin-code-objects', 'bltin-ellipsis-object',
29 'bltin-file-objects', 'bltin-null-object', 'bltin-type-objects', 'booleans',
30 'break', 'callable-types', 'calls', 'class', 'coercion-rules',
31 'comparisons', 'compound', 'context-managers', 'continue', 'conversions',
32 'customization', 'debugger', 'del', 'dict', 'dynamic-features', 'else',
33 'exceptions', 'exec', 'execmodel', 'exprlists', 'floating', 'for',
34 'formatstrings', 'function', 'global', 'id-classes', 'identifiers', 'if',
35 'imaginary', 'import', 'in', 'integers', 'lambda', 'lists', 'naming',
36 'numbers', 'numeric-types', 'objects', 'operator-summary', 'pass', 'power',
37 'print', 'raise', 'return', 'sequence-methods', 'sequence-types',
38 'shifting', 'slicings', 'specialattrs', 'specialnames',
39 'string-conversions', 'string-methods', 'strings', 'subscriptions', 'truth',
40 'try', 'types', 'typesfunctions', 'typesmapping', 'typesmethods',
41 'typesmodules', 'typesseq', 'typesseq-mutable', 'unary', 'while', 'with',
42 'yield'
43]
44
45from os import path
46from time import asctime
47from pprint import pformat
48from docutils.io import StringOutput
49from docutils.utils import new_document
50from sphinx.builder import Builder
51from sphinx.textwriter import TextWriter
52
53class PydocTopicsBuilder(Builder):
54 name = 'pydoc-topics'
55
56 def init(self):
57 self.topics = {}
58
59 def get_outdated_docs(self):
60 return 'all pydoc topics'
61
62 def get_target_uri(self, docname, typ=None):
63 return '' # no URIs
64
65 def write(self, *ignored):
66 writer = TextWriter(self)
67 for label in self.status_iterator(pydoc_topic_labels, 'building topics... '):
68 if label not in self.env.labels:
69 self.warn('label %r not in documentation' % label)
70 continue
71 docname, labelid, sectname = self.env.labels[label]
72 doctree = self.env.get_and_resolve_doctree(docname, self)
73 document = new_document('<section node>')
74 document.append(doctree.ids[labelid])
75 destination = StringOutput(encoding='utf-8')
76 writer.write(document, destination)
77 self.topics[label] = writer.output
78
79 def finish(self):
80 f = open(path.join(self.outdir, 'pydoc_topics.py'), 'w')
81 try:
82 f.write('# Autogenerated by Sphinx on %s\n' % asctime())
83 f.write('topics = ' + pformat(self.topics) + '\n')
84 finally:
85 f.close()
86
87
Georg Brandld4c7e632008-07-23 15:17:09 +000088# Support for documenting Opcodes
89
90import re
91from sphinx import addnodes
92
93opcode_sig_re = re.compile(r'(\w+(?:\+\d)?)\s*\((.*)\)')
94
95def parse_opcode_signature(env, sig, signode):
96 """Transform an opcode signature into RST nodes."""
97 m = opcode_sig_re.match(sig)
98 if m is None:
99 raise ValueError
100 opname, arglist = m.groups()
101 signode += addnodes.desc_name(opname, opname)
102 paramlist = addnodes.desc_parameterlist()
103 signode += paramlist
104 paramlist += addnodes.desc_parameter(arglist, arglist)
105 return opname.strip()
106
107
Georg Brandlc3051922008-04-09 17:58:56 +0000108def setup(app):
109 app.add_role('issue', issue_role)
Georg Brandl681001e2008-06-01 20:33:55 +0000110 app.add_builder(PydocTopicsBuilder)
Georg Brandld4c7e632008-07-23 15:17:09 +0000111 app.add_description_unit('opcode', 'opcode', '%s (opcode)',
112 parse_opcode_signature)