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 | |
| 16 | def 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 Brandl | 681001e | 2008-06-01 20:33:55 +0000 | [diff] [blame] | 23 | # Support for building "topic help" for pydoc |
| 24 | |
| 25 | pydoc_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', 'coercion-rules', |
| 31 | 'comparisons', 'compound', 'context-managers', 'continue', 'conversions', |
| 32 | 'customization', 'debugger', 'del', 'dict', 'dynamic-features', 'else', |
| 33 | 'exceptions', 'exec', 'execmodel', 'exprlists', 'floating', 'for', |
| 34 | 'formatstrings', 'function', 'global', 'id-classes', 'identifiers', 'if', |
| 35 | 'imaginary', 'import', 'in', 'integers', 'lambda', 'lists', 'naming', |
| 36 | 'numbers', 'numeric-types', 'objects', 'operator-summary', 'pass', 'power', |
| 37 | 'print', 'raise', 'return', 'sequence-methods', 'sequence-types', |
| 38 | 'shifting', 'slicings', 'specialattrs', 'specialnames', |
| 39 | 'string-conversions', 'string-methods', 'strings', 'subscriptions', 'truth', |
| 40 | 'try', 'types', 'typesfunctions', 'typesmapping', 'typesmethods', |
| 41 | 'typesmodules', 'typesseq', 'typesseq-mutable', 'unary', 'while', 'with', |
| 42 | 'yield' |
| 43 | ] |
| 44 | |
| 45 | from os import path |
| 46 | from time import asctime |
| 47 | from pprint import pformat |
| 48 | from docutils.io import StringOutput |
| 49 | from docutils.utils import new_document |
Benjamin Peterson | a2813c9 | 2008-12-20 23:48:54 +0000 | [diff] [blame] | 50 | |
| 51 | try: |
| 52 | from sphinx.builders import Builder |
| 53 | except ImportError: |
| 54 | from sphinx.builder import Builder |
Benjamin Peterson | cb948f1 | 2008-12-01 12:52:51 +0000 | [diff] [blame] | 55 | |
| 56 | try: |
| 57 | from sphinx.writers.text import TextWriter |
| 58 | except ImportError: |
| 59 | from sphinx.textwriter import TextWriter |
Georg Brandl | 681001e | 2008-06-01 20:33:55 +0000 | [diff] [blame] | 60 | |
| 61 | class PydocTopicsBuilder(Builder): |
| 62 | name = 'pydoc-topics' |
| 63 | |
| 64 | def init(self): |
| 65 | self.topics = {} |
| 66 | |
| 67 | def get_outdated_docs(self): |
| 68 | return 'all pydoc topics' |
| 69 | |
| 70 | def get_target_uri(self, docname, typ=None): |
| 71 | return '' # no URIs |
| 72 | |
| 73 | def write(self, *ignored): |
| 74 | writer = TextWriter(self) |
| 75 | for label in self.status_iterator(pydoc_topic_labels, 'building topics... '): |
| 76 | if label not in self.env.labels: |
| 77 | self.warn('label %r not in documentation' % label) |
| 78 | continue |
| 79 | docname, labelid, sectname = self.env.labels[label] |
| 80 | doctree = self.env.get_and_resolve_doctree(docname, self) |
| 81 | document = new_document('<section node>') |
| 82 | document.append(doctree.ids[labelid]) |
| 83 | destination = StringOutput(encoding='utf-8') |
| 84 | writer.write(document, destination) |
| 85 | self.topics[label] = writer.output |
| 86 | |
| 87 | def finish(self): |
| 88 | f = open(path.join(self.outdir, 'pydoc_topics.py'), 'w') |
| 89 | try: |
| 90 | f.write('# Autogenerated by Sphinx on %s\n' % asctime()) |
| 91 | f.write('topics = ' + pformat(self.topics) + '\n') |
| 92 | finally: |
| 93 | f.close() |
| 94 | |
Georg Brandl | 700cf28 | 2009-01-04 10:23:49 +0000 | [diff] [blame^] | 95 | # Support for checking for suspicious markup |
| 96 | |
| 97 | import suspicious |
Georg Brandl | 681001e | 2008-06-01 20:33:55 +0000 | [diff] [blame] | 98 | |
Georg Brandl | d4c7e63 | 2008-07-23 15:17:09 +0000 | [diff] [blame] | 99 | # Support for documenting Opcodes |
| 100 | |
| 101 | import re |
| 102 | from sphinx import addnodes |
| 103 | |
| 104 | opcode_sig_re = re.compile(r'(\w+(?:\+\d)?)\s*\((.*)\)') |
| 105 | |
| 106 | def parse_opcode_signature(env, sig, signode): |
| 107 | """Transform an opcode signature into RST nodes.""" |
| 108 | m = opcode_sig_re.match(sig) |
| 109 | if m is None: |
| 110 | raise ValueError |
| 111 | opname, arglist = m.groups() |
| 112 | signode += addnodes.desc_name(opname, opname) |
| 113 | paramlist = addnodes.desc_parameterlist() |
| 114 | signode += paramlist |
| 115 | paramlist += addnodes.desc_parameter(arglist, arglist) |
| 116 | return opname.strip() |
| 117 | |
| 118 | |
Georg Brandl | c305192 | 2008-04-09 17:58:56 +0000 | [diff] [blame] | 119 | def setup(app): |
| 120 | app.add_role('issue', issue_role) |
Georg Brandl | 681001e | 2008-06-01 20:33:55 +0000 | [diff] [blame] | 121 | app.add_builder(PydocTopicsBuilder) |
Georg Brandl | 700cf28 | 2009-01-04 10:23:49 +0000 | [diff] [blame^] | 122 | app.add_builder(suspicious.CheckSuspiciousMarkupBuilder) |
Georg Brandl | d4c7e63 | 2008-07-23 15:17:09 +0000 | [diff] [blame] | 123 | app.add_description_unit('opcode', 'opcode', '%s (opcode)', |
| 124 | parse_opcode_signature) |