blob: 688de95b8ea09bb9d94cf36b20d4190e2856c1f7 [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
Georg Brandlec7d3902009-02-23 10:41:11 +000016# monkey-patch reST parser to disable alphabetic and roman enumerated lists
17from docutils.parsers.rst.states import Body
18Body.enum.converters['loweralpha'] = \
19 Body.enum.converters['upperalpha'] = \
20 Body.enum.converters['lowerroman'] = \
21 Body.enum.converters['upperroman'] = lambda x: None
22
23
Georg Brandlc3051922008-04-09 17:58:56 +000024def 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], []
29
30
Georg Brandl681001e2008-06-01 20:33:55 +000031# Support for building "topic help" for pydoc
32
33pydoc_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'
51]
52
53from os import path
54from time import asctime
55from pprint import pformat
56from docutils.io import StringOutput
57from docutils.utils import new_document
Benjamin Petersonc6e80eb2008-12-21 17:01:26 +000058
Georg Brandlbadbba42009-01-26 23:06:17 +000059try:
60 from sphinx.builders import Builder
61except ImportError:
62 from sphinx.builder import Builder
63
64try:
65 from sphinx.writers.text import TextWriter
66except ImportError:
67 from sphinx.textwriter import TextWriter
Georg Brandl23f74902008-12-02 08:25:00 +000068
Georg Brandl681001e2008-06-01 20:33:55 +000069
70class PydocTopicsBuilder(Builder):
71 name = 'pydoc-topics'
72
73 def init(self):
74 self.topics = {}
75
76 def get_outdated_docs(self):
77 return 'all pydoc topics'
78
79 def get_target_uri(self, docname, typ=None):
80 return '' # no URIs
81
82 def write(self, *ignored):
83 writer = TextWriter(self)
84 for label in self.status_iterator(pydoc_topic_labels, 'building topics... '):
85 if label not in self.env.labels:
86 self.warn('label %r not in documentation' % label)
87 continue
88 docname, labelid, sectname = self.env.labels[label]
89 doctree = self.env.get_and_resolve_doctree(docname, self)
90 document = new_document('<section node>')
91 document.append(doctree.ids[labelid])
92 destination = StringOutput(encoding='utf-8')
93 writer.write(document, destination)
94 self.topics[label] = writer.output
95
96 def finish(self):
97 f = open(path.join(self.outdir, 'pydoc_topics.py'), 'w')
98 try:
99 f.write('# Autogenerated by Sphinx on %s\n' % asctime())
100 f.write('topics = ' + pformat(self.topics) + '\n')
101 finally:
102 f.close()
103
Benjamin Peterson9f7ae1b2009-01-09 03:04:01 +0000104# Support for checking for suspicious markup
105
106import suspicious
Georg Brandl681001e2008-06-01 20:33:55 +0000107
Georg Brandld4c7e632008-07-23 15:17:09 +0000108# Support for documenting Opcodes
109
110import re
111from sphinx import addnodes
112
113opcode_sig_re = re.compile(r'(\w+(?:\+\d)?)\s*\((.*)\)')
114
115def parse_opcode_signature(env, sig, signode):
116 """Transform an opcode signature into RST nodes."""
117 m = opcode_sig_re.match(sig)
118 if m is None:
119 raise ValueError
120 opname, arglist = m.groups()
121 signode += addnodes.desc_name(opname, opname)
122 paramlist = addnodes.desc_parameterlist()
123 signode += paramlist
124 paramlist += addnodes.desc_parameter(arglist, arglist)
125 return opname.strip()
126
127
Georg Brandlc3051922008-04-09 17:58:56 +0000128def setup(app):
129 app.add_role('issue', issue_role)
Georg Brandl681001e2008-06-01 20:33:55 +0000130 app.add_builder(PydocTopicsBuilder)
Benjamin Peterson9f7ae1b2009-01-09 03:04:01 +0000131 app.add_builder(suspicious.CheckSuspiciousMarkupBuilder)
Georg Brandld4c7e632008-07-23 15:17:09 +0000132 app.add_description_unit('opcode', 'opcode', '%s (opcode)',
133 parse_opcode_signature)