blob: 66a0afc294965d8ad8c1257a7b34f106bc201a11 [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
Benjamin Peterson28d88b42009-01-09 03:03:23 +000049from sphinx.builders import Builder
50from sphinx.writers.text import TextWriter
Georg Brandldb6f1082008-12-01 23:02:51 +000051
Georg Brandl6b38daa2008-06-01 21:05:17 +000052
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] = str(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
Benjamin Peterson28d88b42009-01-09 03:03:23 +000087# Support for checking for suspicious markup
88
89import suspicious
Georg Brandl6b38daa2008-06-01 21:05:17 +000090
Georg Brandlf0dd6a62008-07-23 15:19:11 +000091# Support for documenting Opcodes
92
93import re
94from sphinx import addnodes
95
96opcode_sig_re = re.compile(r'(\w+(?:\+\d)?)\s*\((.*)\)')
97
98def parse_opcode_signature(env, sig, signode):
99 """Transform an opcode signature into RST nodes."""
100 m = opcode_sig_re.match(sig)
101 if m is None:
102 raise ValueError
103 opname, arglist = m.groups()
104 signode += addnodes.desc_name(opname, opname)
105 paramlist = addnodes.desc_parameterlist()
106 signode += paramlist
107 paramlist += addnodes.desc_parameter(arglist, arglist)
108 return opname.strip()
109
110
Martin v. Löwis5680d0c2008-04-10 03:06:53 +0000111def setup(app):
112 app.add_role('issue', issue_role)
Georg Brandl6b38daa2008-06-01 21:05:17 +0000113 app.add_builder(PydocTopicsBuilder)
Benjamin Peterson28d88b42009-01-09 03:03:23 +0000114 app.add_builder(suspicious.CheckSuspiciousMarkupBuilder)
Georg Brandlf0dd6a62008-07-23 15:19:11 +0000115 app.add_description_unit('opcode', 'opcode', '%s (opcode)',
116 parse_opcode_signature)