| 1 |
|
| 2 |
""" |
| 3 |
pyspecific.py |
| 4 |
~~~~~~~~~~~~~ |
| 5 |
|
| 6 |
Sphinx extension with Python doc-specific markup. |
| 7 |
|
| 8 |
:copyright: 2008-2014 by Georg Brandl. |
| 9 |
:license: Python license. |
| 10 |
""" |
| 11 |
|
| 12 |
ISSUE_URI = 'https://bugs.python.org/issue%s' |
| 13 |
SOURCE_URI = 'https://github.com/python/cpython/tree/2.7/%s' |
| 14 |
|
| 15 |
from docutils import nodes, utils |
| 16 |
from docutils.parsers.rst import Directive |
| 17 |
|
| 18 |
from sphinx.util.nodes import split_explicit_title |
| 19 |
from sphinx.writers.html import HTMLTranslator |
| 20 |
from sphinx.writers.latex import LaTeXTranslator |
| 21 |
from sphinx.writers.text import TextTranslator |
| 22 |
|
| 23 |
|
| 24 |
from docutils.parsers.rst.states import Body |
| 25 |
Body.enum.converters['loweralpha'] = \ |
| 26 |
Body.enum.converters['upperalpha'] = \ |
| 27 |
Body.enum.converters['lowerroman'] = \ |
| 28 |
Body.enum.converters['upperroman'] = lambda x: None |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
orig_visit_literal_block = HTMLTranslator.visit_literal_block |
| 33 |
def new_visit_literal_block(self, node): |
| 34 |
meta = self.builder.env.metadata[self.builder.current_docname] |
| 35 |
old_trim_doctest_flags = self.highlighter.trim_doctest_flags |
| 36 |
if 'keepdoctest' in meta: |
| 37 |
self.highlighter.trim_doctest_flags = False |
| 38 |
try: |
| 39 |
orig_visit_literal_block(self, node) |
| 40 |
finally: |
| 41 |
self.highlighter.trim_doctest_flags = old_trim_doctest_flags |
| 42 |
|
| 43 |
HTMLTranslator.visit_literal_block = new_visit_literal_block |
| 44 |
|
| 45 |
orig_depart_literal_block = LaTeXTranslator.depart_literal_block |
| 46 |
def new_depart_literal_block(self, node): |
| 47 |
meta = self.builder.env.metadata[self.curfilestack[-1]] |
| 48 |
old_trim_doctest_flags = self.highlighter.trim_doctest_flags |
| 49 |
if 'keepdoctest' in meta: |
| 50 |
self.highlighter.trim_doctest_flags = False |
| 51 |
try: |
| 52 |
orig_depart_literal_block(self, node) |
| 53 |
finally: |
| 54 |
self.highlighter.trim_doctest_flags = old_trim_doctest_flags |
| 55 |
|
| 56 |
LaTeXTranslator.depart_literal_block = new_depart_literal_block |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
def issue_role(typ, rawtext, text, lineno, inliner, options={}, content=[]): |
| 61 |
issue = utils.unescape(text) |
| 62 |
text = 'bpo-'+ issue |
| 63 |
refnode = nodes.reference(text, text, refuri=ISSUE_URI % issue) |
| 64 |
return [refnode], [] |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
def source_role(typ, rawtext, text, lineno, inliner, options={}, content=[]): |
| 70 |
has_t, title, target = split_explicit_title(text) |
| 71 |
title = utils.unescape(title) |
| 72 |
target = utils.unescape(target) |
| 73 |
refnode = nodes.reference(title, title, refuri=SOURCE_URI % target) |
| 74 |
return [refnode], [] |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
class ImplementationDetail(Directive): |
| 80 |
|
| 81 |
has_content = True |
| 82 |
required_arguments = 0 |
| 83 |
optional_arguments = 1 |
| 84 |
final_argument_whitespace = True |
| 85 |
|
| 86 |
def run(self): |
| 87 |
pnode = nodes.compound(classes=['impl-detail']) |
| 88 |
content = self.content |
| 89 |
add_text = nodes.strong('CPython implementation detail:', |
| 90 |
'CPython implementation detail:') |
| 91 |
if self.arguments: |
| 92 |
n, m = self.state.inline_text(self.arguments[0], self.lineno) |
| 93 |
pnode.append(nodes.paragraph('', '', *(n + m))) |
| 94 |
self.state.nested_parse(content, self.content_offset, pnode) |
| 95 |
if pnode.children and isinstance(pnode[0], nodes.paragraph): |
| 96 |
pnode[0].insert(0, add_text) |
| 97 |
pnode[0].insert(1, nodes.Text(' ')) |
| 98 |
else: |
| 99 |
pnode.insert(0, nodes.paragraph('', '', add_text)) |
| 100 |
return [pnode] |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
from sphinx import addnodes |
| 106 |
from sphinx.domains.python import PyModulelevel, PyClassmember |
| 107 |
|
| 108 |
class PyDecoratorMixin(object): |
| 109 |
def handle_signature(self, sig, signode): |
| 110 |
ret = super(PyDecoratorMixin, self).handle_signature(sig, signode) |
| 111 |
signode.insert(0, addnodes.desc_addname('@', '@')) |
| 112 |
return ret |
| 113 |
|
| 114 |
def needs_arglist(self): |
| 115 |
return False |
| 116 |
|
| 117 |
class PyDecoratorFunction(PyDecoratorMixin, PyModulelevel): |
| 118 |
def run(self): |
| 119 |
|
| 120 |
self.name = 'py:function' |
| 121 |
return PyModulelevel.run(self) |
| 122 |
|
| 123 |
class PyDecoratorMethod(PyDecoratorMixin, PyClassmember): |
| 124 |
def run(self): |
| 125 |
self.name = 'py:method' |
| 126 |
return PyClassmember.run(self) |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
pydoc_topic_labels = [ |
| 132 |
'assert', 'assignment', 'atom-identifiers', 'atom-literals', |
| 133 |
'attribute-access', 'attribute-references', 'augassign', 'binary', |
| 134 |
'bitwise', 'bltin-code-objects', 'bltin-ellipsis-object', |
| 135 |
'bltin-file-objects', 'bltin-null-object', 'bltin-type-objects', 'booleans', |
| 136 |
'break', 'callable-types', 'calls', 'class', 'comparisons', 'compound', |
| 137 |
'context-managers', 'continue', 'conversions', 'customization', 'debugger', |
| 138 |
'del', 'dict', 'dynamic-features', 'else', 'exceptions', 'exec', 'execmodel', |
| 139 |
'exprlists', 'floating', 'for', 'formatstrings', 'function', 'global', |
| 140 |
'id-classes', 'identifiers', 'if', 'imaginary', 'import', 'in', 'integers', |
| 141 |
'lambda', 'lists', 'naming', 'numbers', 'numeric-types', |
| 142 |
'objects', 'operator-summary', 'pass', 'power', 'print', 'raise', 'return', |
| 143 |
'sequence-types', 'shifting', 'slicings', 'specialattrs', 'specialnames', |
| 144 |
'string-methods', 'strings', 'subscriptions', 'truth', 'try', 'types', |
| 145 |
'typesfunctions', 'typesmapping', 'typesmethods', 'typesmodules', |
| 146 |
'typesseq', 'typesseq-mutable', 'unary', 'while', 'with', 'yield' |
| 147 |
] |
| 148 |
|
| 149 |
from os import path |
| 150 |
from time import asctime |
| 151 |
from pprint import pformat |
| 152 |
from docutils.io import StringOutput |
| 153 |
from docutils.utils import new_document |
| 154 |
|
| 155 |
from sphinx.builders import Builder |
| 156 |
from sphinx.writers.text import TextWriter |
| 157 |
|
| 158 |
|
| 159 |
class PydocTopicsBuilder(Builder): |
| 160 |
name = 'pydoc-topics' |
| 161 |
|
| 162 |
default_translator_class = TextTranslator |
| 163 |
|
| 164 |
def init(self): |
| 165 |
self.topics = {} |
| 166 |
self.secnumbers = {} |
| 167 |
|
| 168 |
def get_outdated_docs(self): |
| 169 |
return 'all pydoc topics' |
| 170 |
|
| 171 |
def get_target_uri(self, docname, typ=None): |
| 172 |
return '' |
| 173 |
|
| 174 |
def write(self, *ignored): |
| 175 |
try: |
| 176 |
from sphinx.util import status_iterator |
| 177 |
except ImportError: |
| 178 |
status_iterator = self.status_iterator |
| 179 |
|
| 180 |
writer = TextWriter(self) |
| 181 |
for label in status_iterator(pydoc_topic_labels, |
| 182 |
'building topics... ', |
| 183 |
length=len(pydoc_topic_labels)): |
| 184 |
if label not in self.env.domaindata['std']['labels']: |
| 185 |
self.warn('label %r not in documentation' % label) |
| 186 |
continue |
| 187 |
docname, labelid, sectname = self.env.domaindata['std']['labels'][label] |
| 188 |
doctree = self.env.get_and_resolve_doctree(docname, self) |
| 189 |
document = new_document('<section node>') |
| 190 |
document.append(doctree.ids[labelid]) |
| 191 |
destination = StringOutput(encoding='utf-8') |
| 192 |
writer.write(document, destination) |
| 193 |
self.topics[label] = writer.output |
| 194 |
|
| 195 |
def finish(self): |
| 196 |
f = open(path.join(self.outdir, 'topics.py'), 'wb') |
| 197 |
try: |
| 198 |
f.write('# -*- coding: utf-8 -*-\n'.encode('utf-8')) |
| 199 |
f.write(('# Autogenerated by Sphinx on %s\n' % asctime()).encode('utf-8')) |
| 200 |
f.write(('topics = ' + pformat(self.topics) + '\n').encode('utf-8')) |
| 201 |
finally: |
| 202 |
f.close() |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
import suspicious |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
import re |
| 213 |
|
| 214 |
opcode_sig_re = re.compile(r'(\w+(?:\+\d)?)(?:\s*\((.*)\))?') |
| 215 |
|
| 216 |
def parse_opcode_signature(env, sig, signode): |
| 217 |
"""Transform an opcode signature into RST nodes.""" |
| 218 |
m = opcode_sig_re.match(sig) |
| 219 |
if m is None: |
| 220 |
raise ValueError |
| 221 |
opname, arglist = m.groups() |
| 222 |
signode += addnodes.desc_name(opname, opname) |
| 223 |
if arglist is not None: |
| 224 |
paramlist = addnodes.desc_parameterlist() |
| 225 |
signode += paramlist |
| 226 |
paramlist += addnodes.desc_parameter(arglist, arglist) |
| 227 |
return opname.strip() |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
pdbcmd_sig_re = re.compile(r'([a-z()!]+)\s*(.*)') |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
def parse_pdb_command(env, sig, signode): |
| 242 |
"""Transform a pdb command signature into RST nodes.""" |
| 243 |
m = pdbcmd_sig_re.match(sig) |
| 244 |
if m is None: |
| 245 |
raise ValueError |
| 246 |
name, args = m.groups() |
| 247 |
fullname = name.replace('(', '').replace(')', '') |
| 248 |
signode += addnodes.desc_name(name, name) |
| 249 |
if args: |
| 250 |
signode += addnodes.desc_addname(' '+args, ' '+args) |
| 251 |
return fullname |
| 252 |
|
| 253 |
|
| 254 |
def setup(app): |
| 255 |
app.add_role('issue', issue_role) |
| 256 |
app.add_role('source', source_role) |
| 257 |
app.add_directive('impl-detail', ImplementationDetail) |
| 258 |
app.add_builder(PydocTopicsBuilder) |
| 259 |
app.add_builder(suspicious.CheckSuspiciousMarkupBuilder) |
| 260 |
app.add_object_type('opcode', 'opcode', '%s (opcode)', parse_opcode_signature) |
| 261 |
app.add_object_type('pdbcommand', 'pdbcmd', '%s (pdb command)', parse_pdb_command) |
| 262 |
app.add_object_type('2to3fixer', '2to3fixer', '%s (2to3 fixer)') |
| 263 |
app.add_directive_to_domain('py', 'decorator', PyDecoratorFunction) |
| 264 |
app.add_directive_to_domain('py', 'decoratormethod', PyDecoratorMethod) |
| 265 |
return {'version': '1.0', 'parallel_read_safe': True} |