blob: 49ad940e76401e24341c4f0ae5b99fcda59b2ae0 [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
Georg Brandl43819252009-04-26 09:56:44 +00008 :copyright: 2008, 2009 by Georg Brandl.
Georg Brandlc3051922008-04-09 17:58:56 +00009 :license: Python license.
10"""
11
12ISSUE_URI = 'http://bugs.python.org/issue%s'
13
14from docutils import nodes, utils
15
Georg Brandl85c5ccf2009-02-05 11:38:23 +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
Georg Brandl076ca5a2009-09-16 09:05:11 +000023# monkey-patch HTML translator to give versionmodified paragraphs a class
24def new_visit_versionmodified(self, node):
25 self.body.append(self.starttag(node, 'p', CLASS=node['type']))
26 text = versionlabels[node['type']] % node['version']
27 if len(node):
28 text += ': '
29 else:
30 text += '.'
31 self.body.append('<span class="versionmodified">%s</span>' % text)
32
33from sphinx.writers.html import HTMLTranslator
34from sphinx.locale import versionlabels
35HTMLTranslator.visit_versionmodified = new_visit_versionmodified
36
Georg Brandl85c5ccf2009-02-05 11:38:23 +000037
Georg Brandlc3051922008-04-09 17:58:56 +000038def issue_role(typ, rawtext, text, lineno, inliner, options={}, content=[]):
39 issue = utils.unescape(text)
40 text = 'issue ' + issue
41 refnode = nodes.reference(text, text, refuri=ISSUE_URI % issue)
42 return [refnode], []
43
44
Georg Brandl681001e2008-06-01 20:33:55 +000045# Support for building "topic help" for pydoc
46
47pydoc_topic_labels = [
48 'assert', 'assignment', 'atom-identifiers', 'atom-literals',
49 'attribute-access', 'attribute-references', 'augassign', 'binary',
50 'bitwise', 'bltin-code-objects', 'bltin-ellipsis-object',
51 'bltin-file-objects', 'bltin-null-object', 'bltin-type-objects', 'booleans',
52 'break', 'callable-types', 'calls', 'class', 'coercion-rules',
53 'comparisons', 'compound', 'context-managers', 'continue', 'conversions',
54 'customization', 'debugger', 'del', 'dict', 'dynamic-features', 'else',
55 'exceptions', 'exec', 'execmodel', 'exprlists', 'floating', 'for',
56 'formatstrings', 'function', 'global', 'id-classes', 'identifiers', 'if',
57 'imaginary', 'import', 'in', 'integers', 'lambda', 'lists', 'naming',
58 'numbers', 'numeric-types', 'objects', 'operator-summary', 'pass', 'power',
59 'print', 'raise', 'return', 'sequence-methods', 'sequence-types',
60 'shifting', 'slicings', 'specialattrs', 'specialnames',
61 'string-conversions', 'string-methods', 'strings', 'subscriptions', 'truth',
62 'try', 'types', 'typesfunctions', 'typesmapping', 'typesmethods',
63 'typesmodules', 'typesseq', 'typesseq-mutable', 'unary', 'while', 'with',
64 'yield'
65]
66
67from os import path
68from time import asctime
69from pprint import pformat
70from docutils.io import StringOutput
71from docutils.utils import new_document
Benjamin Petersona2813c92008-12-20 23:48:54 +000072
Benjamin Peterson1a67f582009-01-08 04:01:00 +000073from sphinx.builders import Builder
74from sphinx.writers.text import TextWriter
Benjamin Petersoncb948f12008-12-01 12:52:51 +000075
Georg Brandl681001e2008-06-01 20:33:55 +000076
77class PydocTopicsBuilder(Builder):
78 name = 'pydoc-topics'
79
80 def init(self):
81 self.topics = {}
82
83 def get_outdated_docs(self):
84 return 'all pydoc topics'
85
86 def get_target_uri(self, docname, typ=None):
87 return '' # no URIs
88
89 def write(self, *ignored):
90 writer = TextWriter(self)
Benjamin Petersonc5206b32009-03-29 21:50:14 +000091 for label in self.status_iterator(pydoc_topic_labels,
92 'building topics... ',
93 length=len(pydoc_topic_labels)):
Georg Brandl681001e2008-06-01 20:33:55 +000094 if label not in self.env.labels:
95 self.warn('label %r not in documentation' % label)
96 continue
97 docname, labelid, sectname = self.env.labels[label]
98 doctree = self.env.get_and_resolve_doctree(docname, self)
99 document = new_document('<section node>')
100 document.append(doctree.ids[labelid])
101 destination = StringOutput(encoding='utf-8')
102 writer.write(document, destination)
103 self.topics[label] = writer.output
104
105 def finish(self):
Georg Brandl43819252009-04-26 09:56:44 +0000106 f = open(path.join(self.outdir, 'topics.py'), 'w')
Georg Brandl681001e2008-06-01 20:33:55 +0000107 try:
108 f.write('# Autogenerated by Sphinx on %s\n' % asctime())
109 f.write('topics = ' + pformat(self.topics) + '\n')
110 finally:
111 f.close()
112
Georg Brandl700cf282009-01-04 10:23:49 +0000113# Support for checking for suspicious markup
114
115import suspicious
Georg Brandl681001e2008-06-01 20:33:55 +0000116
Georg Brandld4c7e632008-07-23 15:17:09 +0000117# Support for documenting Opcodes
118
119import re
120from sphinx import addnodes
121
122opcode_sig_re = re.compile(r'(\w+(?:\+\d)?)\s*\((.*)\)')
123
124def parse_opcode_signature(env, sig, signode):
125 """Transform an opcode signature into RST nodes."""
126 m = opcode_sig_re.match(sig)
127 if m is None:
128 raise ValueError
129 opname, arglist = m.groups()
130 signode += addnodes.desc_name(opname, opname)
131 paramlist = addnodes.desc_parameterlist()
132 signode += paramlist
133 paramlist += addnodes.desc_parameter(arglist, arglist)
134 return opname.strip()
135
136
Georg Brandlc3051922008-04-09 17:58:56 +0000137def setup(app):
138 app.add_role('issue', issue_role)
Georg Brandl681001e2008-06-01 20:33:55 +0000139 app.add_builder(PydocTopicsBuilder)
Georg Brandl700cf282009-01-04 10:23:49 +0000140 app.add_builder(suspicious.CheckSuspiciousMarkupBuilder)
Georg Brandld4c7e632008-07-23 15:17:09 +0000141 app.add_description_unit('opcode', 'opcode', '%s (opcode)',
142 parse_opcode_signature)
Benjamin Petersone0820e22009-02-07 23:01:19 +0000143 app.add_description_unit('2to3fixer', '2to3fixer', '%s (2to3 fixer)')