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