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 | |
| 8 | :copyright: 2008 by Georg Brandl. |
| 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 | |
| 23 | |
Georg Brandl | c305192 | 2008-04-09 17:58:56 +0000 | [diff] [blame] | 24 | def 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 Brandl | 681001e | 2008-06-01 20:33:55 +0000 | [diff] [blame] | 31 | # Support for building "topic help" for pydoc |
| 32 | |
| 33 | pydoc_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', 'coercion-rules', |
| 39 | 'comparisons', 'compound', 'context-managers', 'continue', 'conversions', |
| 40 | 'customization', 'debugger', 'del', 'dict', 'dynamic-features', 'else', |
| 41 | 'exceptions', 'exec', 'execmodel', 'exprlists', 'floating', 'for', |
| 42 | 'formatstrings', 'function', 'global', 'id-classes', 'identifiers', 'if', |
| 43 | 'imaginary', 'import', 'in', 'integers', 'lambda', 'lists', 'naming', |
| 44 | 'numbers', 'numeric-types', 'objects', 'operator-summary', 'pass', 'power', |
| 45 | 'print', 'raise', 'return', 'sequence-methods', 'sequence-types', |
| 46 | 'shifting', 'slicings', 'specialattrs', 'specialnames', |
| 47 | 'string-conversions', 'string-methods', 'strings', 'subscriptions', 'truth', |
| 48 | 'try', 'types', 'typesfunctions', 'typesmapping', 'typesmethods', |
| 49 | 'typesmodules', 'typesseq', 'typesseq-mutable', 'unary', 'while', 'with', |
| 50 | 'yield' |
| 51 | ] |
| 52 | |
| 53 | from os import path |
| 54 | from time import asctime |
| 55 | from pprint import pformat |
| 56 | from docutils.io import StringOutput |
| 57 | from docutils.utils import new_document |
Benjamin Peterson | a2813c9 | 2008-12-20 23:48:54 +0000 | [diff] [blame] | 58 | |
Benjamin Peterson | 1a67f58 | 2009-01-08 04:01:00 +0000 | [diff] [blame] | 59 | from sphinx.builders import Builder |
| 60 | from sphinx.writers.text import TextWriter |
Benjamin Peterson | cb948f1 | 2008-12-01 12:52:51 +0000 | [diff] [blame] | 61 | |
Georg Brandl | 681001e | 2008-06-01 20:33:55 +0000 | [diff] [blame] | 62 | |
| 63 | class PydocTopicsBuilder(Builder): |
| 64 | name = 'pydoc-topics' |
| 65 | |
| 66 | def init(self): |
| 67 | self.topics = {} |
| 68 | |
| 69 | def get_outdated_docs(self): |
| 70 | return 'all pydoc topics' |
| 71 | |
| 72 | def get_target_uri(self, docname, typ=None): |
| 73 | return '' # no URIs |
| 74 | |
| 75 | def write(self, *ignored): |
| 76 | writer = TextWriter(self) |
| 77 | for label in self.status_iterator(pydoc_topic_labels, 'building topics... '): |
| 78 | 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] = 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 | |
Georg Brandl | 700cf28 | 2009-01-04 10:23:49 +0000 | [diff] [blame] | 97 | # Support for checking for suspicious markup |
| 98 | |
| 99 | import suspicious |
Georg Brandl | 681001e | 2008-06-01 20:33:55 +0000 | [diff] [blame] | 100 | |
Georg Brandl | d4c7e63 | 2008-07-23 15:17:09 +0000 | [diff] [blame] | 101 | # Support for documenting Opcodes |
| 102 | |
| 103 | import re |
| 104 | from sphinx import addnodes |
| 105 | |
| 106 | opcode_sig_re = re.compile(r'(\w+(?:\+\d)?)\s*\((.*)\)') |
| 107 | |
| 108 | def 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 | |
Georg Brandl | c305192 | 2008-04-09 17:58:56 +0000 | [diff] [blame] | 121 | def setup(app): |
| 122 | app.add_role('issue', issue_role) |
Georg Brandl | 681001e | 2008-06-01 20:33:55 +0000 | [diff] [blame] | 123 | app.add_builder(PydocTopicsBuilder) |
Georg Brandl | 700cf28 | 2009-01-04 10:23:49 +0000 | [diff] [blame] | 124 | app.add_builder(suspicious.CheckSuspiciousMarkupBuilder) |
Georg Brandl | d4c7e63 | 2008-07-23 15:17:09 +0000 | [diff] [blame] | 125 | app.add_description_unit('opcode', 'opcode', '%s (opcode)', |
| 126 | parse_opcode_signature) |
Benjamin Peterson | e0820e2 | 2009-02-07 23:01:19 +0000 | [diff] [blame] | 127 | app.add_description_unit('2to3fixer', '2to3fixer', '%s (2to3 fixer)') |