blob: e4727e3a65640a25daf4222a250a7d7ceb0ef3c1 [file] [log] [blame]
Martin v. Löwis5680d0c2008-04-10 03:06:53 +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 Brandl6b38daa2008-06-01 21:05:17 +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', 'comparisons', 'compound',
31 'context-managers', 'continue', 'conversions', 'customization', 'debugger',
32 'del', 'dict', 'dynamic-features', 'else', 'exceptions', 'execmodel',
33 'exprlists', 'floating', 'for', 'formatstrings', 'function', 'global',
34 'id-classes', 'identifiers', 'if', 'imaginary', 'import', 'in', 'integers',
35 'lambda', 'lists', 'naming', 'numbers', 'numeric-types', 'objects',
36 'operator-summary', 'pass', 'power', 'raise', 'return', 'sequence-types',
37 'shifting', 'slicings', 'specialattrs', 'specialnames', 'string-methods',
38 'strings', 'subscriptions', 'truth', 'try', 'types', 'typesfunctions',
39 'typesmapping', 'typesmethods', 'typesmodules', 'typesseq',
40 'typesseq-mutable', 'unary', 'while', 'with', 'yield'
41]
42
43from os import path
44from time import asctime
45from pprint import pformat
46from docutils.io import StringOutput
47from docutils.utils import new_document
Benjamin Peterson6e43fb12008-12-21 00:16:13 +000048
49try:
50 from sphinx.builders import Builder
51except ImportError:
52 from sphinx.builder import Builder
Georg Brandldb6f1082008-12-01 23:02:51 +000053
54try:
55 from sphinx.writers.text import TextWriter
56except ImportError:
57 from sphinx.textwriter import TextWriter
Georg Brandl6b38daa2008-06-01 21:05:17 +000058
59class PydocTopicsBuilder(Builder):
60 name = 'pydoc-topics'
61
62 def init(self):
63 self.topics = {}
64
65 def get_outdated_docs(self):
66 return 'all pydoc topics'
67
68 def get_target_uri(self, docname, typ=None):
69 return '' # no URIs
70
71 def write(self, *ignored):
72 writer = TextWriter(self)
73 for label in self.status_iterator(pydoc_topic_labels, 'building topics... '):
74 if label not in self.env.labels:
75 self.warn('label %r not in documentation' % label)
76 continue
77 docname, labelid, sectname = self.env.labels[label]
78 doctree = self.env.get_and_resolve_doctree(docname, self)
79 document = new_document('<section node>')
80 document.append(doctree.ids[labelid])
81 destination = StringOutput(encoding='utf-8')
82 writer.write(document, destination)
83 self.topics[label] = str(writer.output)
84
85 def finish(self):
86 f = open(path.join(self.outdir, 'pydoc_topics.py'), 'w')
87 try:
88 f.write('# Autogenerated by Sphinx on %s\n' % asctime())
89 f.write('topics = ' + pformat(self.topics) + '\n')
90 finally:
91 f.close()
92
93
Georg Brandlf0dd6a62008-07-23 15:19:11 +000094# Support for documenting Opcodes
95
96import re
97from sphinx import addnodes
98
99opcode_sig_re = re.compile(r'(\w+(?:\+\d)?)\s*\((.*)\)')
100
101def parse_opcode_signature(env, sig, signode):
102 """Transform an opcode signature into RST nodes."""
103 m = opcode_sig_re.match(sig)
104 if m is None:
105 raise ValueError
106 opname, arglist = m.groups()
107 signode += addnodes.desc_name(opname, opname)
108 paramlist = addnodes.desc_parameterlist()
109 signode += paramlist
110 paramlist += addnodes.desc_parameter(arglist, arglist)
111 return opname.strip()
112
113
Martin v. Löwis5680d0c2008-04-10 03:06:53 +0000114def setup(app):
115 app.add_role('issue', issue_role)
Georg Brandl6b38daa2008-06-01 21:05:17 +0000116 app.add_builder(PydocTopicsBuilder)
Georg Brandlf0dd6a62008-07-23 15:19:11 +0000117 app.add_description_unit('opcode', 'opcode', '%s (opcode)',
118 parse_opcode_signature)