Georg Brandl | c305192 | 2008-04-09 17:58:56 +0000 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | """ |
| 3 | pyspecific.py |
| 4 | ~~~~~~~~~~~~~ |
| 5 | |
| 6 | Sphinx extension with Python doc-specific markup. |
| 7 | |
Georg Brandl | 4381925 | 2009-04-26 09:56:44 +0000 | [diff] [blame] | 8 | :copyright: 2008, 2009 by Georg Brandl. |
Georg Brandl | c305192 | 2008-04-09 17:58:56 +0000 | [diff] [blame] | 9 | :license: Python license. |
| 10 | """ |
| 11 | |
| 12 | ISSUE_URI = 'http://bugs.python.org/issue%s' |
| 13 | |
| 14 | from docutils import nodes, utils |
| 15 | |
Georg Brandl | 85c5ccf | 2009-02-05 11:38:23 +0000 | [diff] [blame] | 16 | # monkey-patch reST parser to disable alphabetic and roman enumerated lists |
| 17 | from docutils.parsers.rst.states import Body |
| 18 | Body.enum.converters['loweralpha'] = \ |
| 19 | Body.enum.converters['upperalpha'] = \ |
| 20 | Body.enum.converters['lowerroman'] = \ |
| 21 | Body.enum.converters['upperroman'] = lambda x: None |
| 22 | |
Georg Brandl | 076ca5a | 2009-09-16 09:05:11 +0000 | [diff] [blame] | 23 | # monkey-patch HTML translator to give versionmodified paragraphs a class |
| 24 | def 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 | |
| 33 | from sphinx.writers.html import HTMLTranslator |
| 34 | from sphinx.locale import versionlabels |
| 35 | HTMLTranslator.visit_versionmodified = new_visit_versionmodified |
| 36 | |
Georg Brandl | 85c5ccf | 2009-02-05 11:38:23 +0000 | [diff] [blame] | 37 | |
Georg Brandl | 08be2e2 | 2009-10-22 08:05:04 +0000 | [diff] [blame] | 38 | # Support for marking up and linking to bugs.python.org issues |
| 39 | |
Georg Brandl | c305192 | 2008-04-09 17:58:56 +0000 | [diff] [blame] | 40 | def issue_role(typ, rawtext, text, lineno, inliner, options={}, content=[]): |
| 41 | issue = utils.unescape(text) |
| 42 | text = 'issue ' + issue |
| 43 | refnode = nodes.reference(text, text, refuri=ISSUE_URI % issue) |
| 44 | return [refnode], [] |
| 45 | |
| 46 | |
Georg Brandl | 08be2e2 | 2009-10-22 08:05:04 +0000 | [diff] [blame] | 47 | # Support for marking up implementation details |
| 48 | |
| 49 | from sphinx.util.compat import Directive |
| 50 | |
| 51 | class ImplementationDetail(Directive): |
| 52 | |
| 53 | has_content = True |
| 54 | required_arguments = 0 |
| 55 | optional_arguments = 1 |
| 56 | final_argument_whitespace = True |
| 57 | |
| 58 | def run(self): |
| 59 | pnode = nodes.compound(classes=['impl-detail']) |
| 60 | content = self.content |
Georg Brandl | a054722 | 2009-10-22 11:01:46 +0000 | [diff] [blame] | 61 | add_text = nodes.strong('CPython implementation detail:', |
| 62 | 'CPython implementation detail:') |
Georg Brandl | f5f7c66 | 2009-10-22 11:28:23 +0000 | [diff] [blame^] | 63 | if self.arguments: |
| 64 | n, m = self.state.inline_text(self.arguments[0], self.lineno) |
| 65 | pnode.append(nodes.paragraph('', '', *(n + m))) |
Georg Brandl | 08be2e2 | 2009-10-22 08:05:04 +0000 | [diff] [blame] | 66 | self.state.nested_parse(content, self.content_offset, pnode) |
Georg Brandl | a054722 | 2009-10-22 11:01:46 +0000 | [diff] [blame] | 67 | if pnode.children and isinstance(pnode[0], nodes.paragraph): |
| 68 | pnode[0].insert(0, add_text) |
| 69 | pnode[0].insert(1, nodes.Text(' ')) |
| 70 | else: |
| 71 | pnode.insert(0, nodes.paragraph('', '', add_text)) |
Georg Brandl | 08be2e2 | 2009-10-22 08:05:04 +0000 | [diff] [blame] | 72 | return [pnode] |
| 73 | |
| 74 | |
Georg Brandl | 681001e | 2008-06-01 20:33:55 +0000 | [diff] [blame] | 75 | # Support for building "topic help" for pydoc |
| 76 | |
| 77 | pydoc_topic_labels = [ |
| 78 | 'assert', 'assignment', 'atom-identifiers', 'atom-literals', |
| 79 | 'attribute-access', 'attribute-references', 'augassign', 'binary', |
| 80 | 'bitwise', 'bltin-code-objects', 'bltin-ellipsis-object', |
| 81 | 'bltin-file-objects', 'bltin-null-object', 'bltin-type-objects', 'booleans', |
| 82 | 'break', 'callable-types', 'calls', 'class', 'coercion-rules', |
| 83 | 'comparisons', 'compound', 'context-managers', 'continue', 'conversions', |
| 84 | 'customization', 'debugger', 'del', 'dict', 'dynamic-features', 'else', |
| 85 | 'exceptions', 'exec', 'execmodel', 'exprlists', 'floating', 'for', |
| 86 | 'formatstrings', 'function', 'global', 'id-classes', 'identifiers', 'if', |
| 87 | 'imaginary', 'import', 'in', 'integers', 'lambda', 'lists', 'naming', |
| 88 | 'numbers', 'numeric-types', 'objects', 'operator-summary', 'pass', 'power', |
| 89 | 'print', 'raise', 'return', 'sequence-methods', 'sequence-types', |
| 90 | 'shifting', 'slicings', 'specialattrs', 'specialnames', |
| 91 | 'string-conversions', 'string-methods', 'strings', 'subscriptions', 'truth', |
| 92 | 'try', 'types', 'typesfunctions', 'typesmapping', 'typesmethods', |
| 93 | 'typesmodules', 'typesseq', 'typesseq-mutable', 'unary', 'while', 'with', |
| 94 | 'yield' |
| 95 | ] |
| 96 | |
| 97 | from os import path |
| 98 | from time import asctime |
| 99 | from pprint import pformat |
| 100 | from docutils.io import StringOutput |
| 101 | from docutils.utils import new_document |
Benjamin Peterson | a2813c9 | 2008-12-20 23:48:54 +0000 | [diff] [blame] | 102 | |
Benjamin Peterson | 1a67f58 | 2009-01-08 04:01:00 +0000 | [diff] [blame] | 103 | from sphinx.builders import Builder |
| 104 | from sphinx.writers.text import TextWriter |
Benjamin Peterson | cb948f1 | 2008-12-01 12:52:51 +0000 | [diff] [blame] | 105 | |
Georg Brandl | 681001e | 2008-06-01 20:33:55 +0000 | [diff] [blame] | 106 | |
| 107 | class PydocTopicsBuilder(Builder): |
| 108 | name = 'pydoc-topics' |
| 109 | |
| 110 | def init(self): |
| 111 | self.topics = {} |
| 112 | |
| 113 | def get_outdated_docs(self): |
| 114 | return 'all pydoc topics' |
| 115 | |
| 116 | def get_target_uri(self, docname, typ=None): |
| 117 | return '' # no URIs |
| 118 | |
| 119 | def write(self, *ignored): |
| 120 | writer = TextWriter(self) |
Benjamin Peterson | c5206b3 | 2009-03-29 21:50:14 +0000 | [diff] [blame] | 121 | for label in self.status_iterator(pydoc_topic_labels, |
| 122 | 'building topics... ', |
| 123 | length=len(pydoc_topic_labels)): |
Georg Brandl | 681001e | 2008-06-01 20:33:55 +0000 | [diff] [blame] | 124 | if label not in self.env.labels: |
| 125 | self.warn('label %r not in documentation' % label) |
| 126 | continue |
| 127 | docname, labelid, sectname = self.env.labels[label] |
| 128 | doctree = self.env.get_and_resolve_doctree(docname, self) |
| 129 | document = new_document('<section node>') |
| 130 | document.append(doctree.ids[labelid]) |
| 131 | destination = StringOutput(encoding='utf-8') |
| 132 | writer.write(document, destination) |
| 133 | self.topics[label] = writer.output |
| 134 | |
| 135 | def finish(self): |
Georg Brandl | 4381925 | 2009-04-26 09:56:44 +0000 | [diff] [blame] | 136 | f = open(path.join(self.outdir, 'topics.py'), 'w') |
Georg Brandl | 681001e | 2008-06-01 20:33:55 +0000 | [diff] [blame] | 137 | try: |
| 138 | f.write('# Autogenerated by Sphinx on %s\n' % asctime()) |
| 139 | f.write('topics = ' + pformat(self.topics) + '\n') |
| 140 | finally: |
| 141 | f.close() |
| 142 | |
Georg Brandl | 08be2e2 | 2009-10-22 08:05:04 +0000 | [diff] [blame] | 143 | |
Georg Brandl | 700cf28 | 2009-01-04 10:23:49 +0000 | [diff] [blame] | 144 | # Support for checking for suspicious markup |
| 145 | |
| 146 | import suspicious |
Georg Brandl | 681001e | 2008-06-01 20:33:55 +0000 | [diff] [blame] | 147 | |
Georg Brandl | 08be2e2 | 2009-10-22 08:05:04 +0000 | [diff] [blame] | 148 | |
Georg Brandl | d4c7e63 | 2008-07-23 15:17:09 +0000 | [diff] [blame] | 149 | # Support for documenting Opcodes |
| 150 | |
| 151 | import re |
| 152 | from sphinx import addnodes |
| 153 | |
| 154 | opcode_sig_re = re.compile(r'(\w+(?:\+\d)?)\s*\((.*)\)') |
| 155 | |
| 156 | def parse_opcode_signature(env, sig, signode): |
| 157 | """Transform an opcode signature into RST nodes.""" |
| 158 | m = opcode_sig_re.match(sig) |
| 159 | if m is None: |
| 160 | raise ValueError |
| 161 | opname, arglist = m.groups() |
| 162 | signode += addnodes.desc_name(opname, opname) |
| 163 | paramlist = addnodes.desc_parameterlist() |
| 164 | signode += paramlist |
| 165 | paramlist += addnodes.desc_parameter(arglist, arglist) |
| 166 | return opname.strip() |
| 167 | |
| 168 | |
Georg Brandl | c305192 | 2008-04-09 17:58:56 +0000 | [diff] [blame] | 169 | def setup(app): |
| 170 | app.add_role('issue', issue_role) |
Georg Brandl | 08be2e2 | 2009-10-22 08:05:04 +0000 | [diff] [blame] | 171 | app.add_directive('impl-detail', ImplementationDetail) |
Georg Brandl | 681001e | 2008-06-01 20:33:55 +0000 | [diff] [blame] | 172 | app.add_builder(PydocTopicsBuilder) |
Georg Brandl | 700cf28 | 2009-01-04 10:23:49 +0000 | [diff] [blame] | 173 | app.add_builder(suspicious.CheckSuspiciousMarkupBuilder) |
Georg Brandl | d4c7e63 | 2008-07-23 15:17:09 +0000 | [diff] [blame] | 174 | app.add_description_unit('opcode', 'opcode', '%s (opcode)', |
| 175 | parse_opcode_signature) |
Benjamin Peterson | e0820e2 | 2009-02-07 23:01:19 +0000 | [diff] [blame] | 176 | app.add_description_unit('2to3fixer', '2to3fixer', '%s (2to3 fixer)') |