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 | 08be2e2 | 2009-10-22 08:05:04 +0000 | [diff] [blame] | 63 | self.state.nested_parse(content, self.content_offset, pnode) |
Georg Brandl | a054722 | 2009-10-22 11:01:46 +0000 | [diff] [blame^] | 64 | if pnode.children and isinstance(pnode[0], nodes.paragraph): |
| 65 | pnode[0].insert(0, add_text) |
| 66 | pnode[0].insert(1, nodes.Text(' ')) |
| 67 | else: |
| 68 | pnode.insert(0, nodes.paragraph('', '', add_text)) |
Georg Brandl | 08be2e2 | 2009-10-22 08:05:04 +0000 | [diff] [blame] | 69 | return [pnode] |
| 70 | |
| 71 | |
Georg Brandl | 681001e | 2008-06-01 20:33:55 +0000 | [diff] [blame] | 72 | # Support for building "topic help" for pydoc |
| 73 | |
| 74 | pydoc_topic_labels = [ |
| 75 | 'assert', 'assignment', 'atom-identifiers', 'atom-literals', |
| 76 | 'attribute-access', 'attribute-references', 'augassign', 'binary', |
| 77 | 'bitwise', 'bltin-code-objects', 'bltin-ellipsis-object', |
| 78 | 'bltin-file-objects', 'bltin-null-object', 'bltin-type-objects', 'booleans', |
| 79 | 'break', 'callable-types', 'calls', 'class', 'coercion-rules', |
| 80 | 'comparisons', 'compound', 'context-managers', 'continue', 'conversions', |
| 81 | 'customization', 'debugger', 'del', 'dict', 'dynamic-features', 'else', |
| 82 | 'exceptions', 'exec', 'execmodel', 'exprlists', 'floating', 'for', |
| 83 | 'formatstrings', 'function', 'global', 'id-classes', 'identifiers', 'if', |
| 84 | 'imaginary', 'import', 'in', 'integers', 'lambda', 'lists', 'naming', |
| 85 | 'numbers', 'numeric-types', 'objects', 'operator-summary', 'pass', 'power', |
| 86 | 'print', 'raise', 'return', 'sequence-methods', 'sequence-types', |
| 87 | 'shifting', 'slicings', 'specialattrs', 'specialnames', |
| 88 | 'string-conversions', 'string-methods', 'strings', 'subscriptions', 'truth', |
| 89 | 'try', 'types', 'typesfunctions', 'typesmapping', 'typesmethods', |
| 90 | 'typesmodules', 'typesseq', 'typesseq-mutable', 'unary', 'while', 'with', |
| 91 | 'yield' |
| 92 | ] |
| 93 | |
| 94 | from os import path |
| 95 | from time import asctime |
| 96 | from pprint import pformat |
| 97 | from docutils.io import StringOutput |
| 98 | from docutils.utils import new_document |
Benjamin Peterson | a2813c9 | 2008-12-20 23:48:54 +0000 | [diff] [blame] | 99 | |
Benjamin Peterson | 1a67f58 | 2009-01-08 04:01:00 +0000 | [diff] [blame] | 100 | from sphinx.builders import Builder |
| 101 | from sphinx.writers.text import TextWriter |
Benjamin Peterson | cb948f1 | 2008-12-01 12:52:51 +0000 | [diff] [blame] | 102 | |
Georg Brandl | 681001e | 2008-06-01 20:33:55 +0000 | [diff] [blame] | 103 | |
| 104 | class PydocTopicsBuilder(Builder): |
| 105 | name = 'pydoc-topics' |
| 106 | |
| 107 | def init(self): |
| 108 | self.topics = {} |
| 109 | |
| 110 | def get_outdated_docs(self): |
| 111 | return 'all pydoc topics' |
| 112 | |
| 113 | def get_target_uri(self, docname, typ=None): |
| 114 | return '' # no URIs |
| 115 | |
| 116 | def write(self, *ignored): |
| 117 | writer = TextWriter(self) |
Benjamin Peterson | c5206b3 | 2009-03-29 21:50:14 +0000 | [diff] [blame] | 118 | for label in self.status_iterator(pydoc_topic_labels, |
| 119 | 'building topics... ', |
| 120 | length=len(pydoc_topic_labels)): |
Georg Brandl | 681001e | 2008-06-01 20:33:55 +0000 | [diff] [blame] | 121 | if label not in self.env.labels: |
| 122 | self.warn('label %r not in documentation' % label) |
| 123 | continue |
| 124 | docname, labelid, sectname = self.env.labels[label] |
| 125 | doctree = self.env.get_and_resolve_doctree(docname, self) |
| 126 | document = new_document('<section node>') |
| 127 | document.append(doctree.ids[labelid]) |
| 128 | destination = StringOutput(encoding='utf-8') |
| 129 | writer.write(document, destination) |
| 130 | self.topics[label] = writer.output |
| 131 | |
| 132 | def finish(self): |
Georg Brandl | 4381925 | 2009-04-26 09:56:44 +0000 | [diff] [blame] | 133 | f = open(path.join(self.outdir, 'topics.py'), 'w') |
Georg Brandl | 681001e | 2008-06-01 20:33:55 +0000 | [diff] [blame] | 134 | try: |
| 135 | f.write('# Autogenerated by Sphinx on %s\n' % asctime()) |
| 136 | f.write('topics = ' + pformat(self.topics) + '\n') |
| 137 | finally: |
| 138 | f.close() |
| 139 | |
Georg Brandl | 08be2e2 | 2009-10-22 08:05:04 +0000 | [diff] [blame] | 140 | |
Georg Brandl | 700cf28 | 2009-01-04 10:23:49 +0000 | [diff] [blame] | 141 | # Support for checking for suspicious markup |
| 142 | |
| 143 | import suspicious |
Georg Brandl | 681001e | 2008-06-01 20:33:55 +0000 | [diff] [blame] | 144 | |
Georg Brandl | 08be2e2 | 2009-10-22 08:05:04 +0000 | [diff] [blame] | 145 | |
Georg Brandl | d4c7e63 | 2008-07-23 15:17:09 +0000 | [diff] [blame] | 146 | # Support for documenting Opcodes |
| 147 | |
| 148 | import re |
| 149 | from sphinx import addnodes |
| 150 | |
| 151 | opcode_sig_re = re.compile(r'(\w+(?:\+\d)?)\s*\((.*)\)') |
| 152 | |
| 153 | def parse_opcode_signature(env, sig, signode): |
| 154 | """Transform an opcode signature into RST nodes.""" |
| 155 | m = opcode_sig_re.match(sig) |
| 156 | if m is None: |
| 157 | raise ValueError |
| 158 | opname, arglist = m.groups() |
| 159 | signode += addnodes.desc_name(opname, opname) |
| 160 | paramlist = addnodes.desc_parameterlist() |
| 161 | signode += paramlist |
| 162 | paramlist += addnodes.desc_parameter(arglist, arglist) |
| 163 | return opname.strip() |
| 164 | |
| 165 | |
Georg Brandl | c305192 | 2008-04-09 17:58:56 +0000 | [diff] [blame] | 166 | def setup(app): |
| 167 | app.add_role('issue', issue_role) |
Georg Brandl | 08be2e2 | 2009-10-22 08:05:04 +0000 | [diff] [blame] | 168 | app.add_directive('impl-detail', ImplementationDetail) |
Georg Brandl | 681001e | 2008-06-01 20:33:55 +0000 | [diff] [blame] | 169 | app.add_builder(PydocTopicsBuilder) |
Georg Brandl | 700cf28 | 2009-01-04 10:23:49 +0000 | [diff] [blame] | 170 | app.add_builder(suspicious.CheckSuspiciousMarkupBuilder) |
Georg Brandl | d4c7e63 | 2008-07-23 15:17:09 +0000 | [diff] [blame] | 171 | app.add_description_unit('opcode', 'opcode', '%s (opcode)', |
| 172 | parse_opcode_signature) |
Benjamin Peterson | e0820e2 | 2009-02-07 23:01:19 +0000 | [diff] [blame] | 173 | app.add_description_unit('2to3fixer', '2to3fixer', '%s (2to3 fixer)') |