Package epydoc :: Package markup :: Module restructuredtext
[hide private]
[frames] | no frames]

Source Code for Module epydoc.markup.restructuredtext

  1  # 
  2  # rst.py: ReStructuredText docstring parsing 
  3  # Edward Loper 
  4  # 
  5  # Created [06/28/03 02:52 AM] 
  6  # $Id: restructuredtext.py 1551 2007-02-25 16:13:17Z dvarrazzo $ 
  7  # 
  8   
  9  """ 
 10  Epydoc parser for ReStructuredText strings.  ReStructuredText is the 
 11  standard markup language used by the Docutils project. 
 12  L{parse_docstring()} provides the primary interface to this module; it 
 13  returns a L{ParsedRstDocstring}, which supports all of the methods 
 14  defined by L{ParsedDocstring}. 
 15   
 16  L{ParsedRstDocstring} is basically just a L{ParsedDocstring} wrapper 
 17  for the C{docutils.nodes.document} class. 
 18   
 19  Creating C{ParsedRstDocstring}s 
 20  =============================== 
 21   
 22  C{ParsedRstDocstring}s are created by the C{parse_document} function, 
 23  using the C{docutils.core.publish_string()} method, with the following 
 24  helpers: 
 25   
 26    - An L{_EpydocReader} is used to capture all error messages as it 
 27      parses the docstring. 
 28    - A L{_DocumentPseudoWriter} is used to extract the document itself, 
 29      without actually writing any output.  The document is saved for 
 30      further processing.  The settings for the writer are copied from 
 31      C{docutils.writers.html4css1.Writer}, since those settings will 
 32      be used when we actually write the docstring to html. 
 33   
 34  Using C{ParsedRstDocstring}s 
 35  ============================ 
 36   
 37  C{ParsedRstDocstring}s support all of the methods defined by 
 38  C{ParsedDocstring}; but only the following four methods have 
 39  non-default behavior: 
 40   
 41    - L{to_html()<ParsedRstDocstring.to_html>} uses an 
 42      L{_EpydocHTMLTranslator} to translate the C{ParsedRstDocstring}'s 
 43      document into an HTML segment. 
 44    - L{split_fields()<ParsedRstDocstring.split_fields>} uses a 
 45      L{_SplitFieldsTranslator} to divide the C{ParsedRstDocstring}'s 
 46      document into its main body and its fields.  Special handling 
 47      is done to account for consolidated fields. 
 48    - L{summary()<ParsedRstDocstring.summary>} uses a 
 49      L{_SummaryExtractor} to extract the first sentence from 
 50      the C{ParsedRstDocstring}'s document. 
 51    - L{to_plaintext()<ParsedRstDocstring.to_plaintext>} uses 
 52      C{document.astext()} to convert the C{ParsedRstDocstring}'s 
 53      document to plaintext. 
 54   
 55  @todo: Add ParsedRstDocstring.to_latex() 
 56  @var CONSOLIDATED_FIELDS: A dictionary encoding the set of 
 57  'consolidated fields' that can be used.  Each consolidated field is 
 58  marked by a single tag, and contains a single bulleted list, where 
 59  each list item starts with an identifier, marked as interpreted text 
 60  (C{`...`}).  This module automatically splits these consolidated 
 61  fields into individual fields.  The keys of C{CONSOLIDATED_FIELDS} are 
 62  the names of possible consolidated fields; and the values are the 
 63  names of the field tags that should be used for individual entries in 
 64  the list. 
 65  """ 
 66  __docformat__ = 'epytext en' 
 67   
 68  # Imports 
 69  import re, os, os.path 
 70  from xml.dom.minidom import * 
 71   
 72  from docutils.core import publish_string 
 73  from docutils.writers import Writer 
 74  from docutils.writers.html4css1 import HTMLTranslator, Writer as HTMLWriter 
 75  from docutils.writers.latex2e import LaTeXTranslator, Writer as LaTeXWriter 
 76  from docutils.readers.standalone import Reader as StandaloneReader 
 77  from docutils.utils import new_document 
 78  from docutils.nodes import NodeVisitor, Text, SkipChildren 
 79  from docutils.nodes import SkipNode, TreeCopyVisitor 
 80  from docutils.frontend import OptionParser 
 81  from docutils.parsers.rst import directives, roles 
 82  import docutils.nodes 
 83  import docutils.transforms.frontmatter 
 84  import docutils.transforms 
 85  import docutils.utils 
 86   
 87  from epydoc.compat import * # Backwards compatibility 
 88  from epydoc.markup import * 
 89  from epydoc.apidoc import ModuleDoc, ClassDoc 
 90  from epydoc.docwriter.dotgraph import * 
 91  from epydoc.docwriter.xlink import ApiLinkReader 
 92  from epydoc.markup.doctest import doctest_to_html, doctest_to_latex, \ 
 93                                    HTMLDoctestColorizer 
 94   
 95  #: A dictionary whose keys are the "consolidated fields" that are 
 96  #: recognized by epydoc; and whose values are the corresponding epydoc 
 97  #: field names that should be used for the individual fields. 
 98  CONSOLIDATED_FIELDS = { 
 99      'parameters': 'param', 
100      'arguments': 'arg', 
101      'exceptions': 'except', 
102      'variables': 'var', 
103      'ivariables': 'ivar', 
104      'cvariables': 'cvar', 
105      'groups': 'group', 
106      'types': 'type', 
107      'keywords': 'keyword', 
108      } 
109   
110  #: A list of consolidated fields whose bodies may be specified using a 
111  #: definition list, rather than a bulleted list.  For these fields, the 
112  #: 'classifier' for each term in the definition list is translated into 
113  #: a @type field. 
114  CONSOLIDATED_DEFLIST_FIELDS = ['param', 'arg', 'var', 'ivar', 'cvar', 'keyword'] 
115   
116 -def parse_docstring(docstring, errors, **options):
117 """ 118 Parse the given docstring, which is formatted using 119 ReStructuredText; and return a L{ParsedDocstring} representation 120 of its contents. 121 @param docstring: The docstring to parse 122 @type docstring: C{string} 123 @param errors: A list where any errors generated during parsing 124 will be stored. 125 @type errors: C{list} of L{ParseError} 126 @param options: Extra options. Unknown options are ignored. 127 Currently, no extra options are defined. 128 @rtype: L{ParsedDocstring} 129 """ 130 writer = _DocumentPseudoWriter() 131 reader = _EpydocReader(errors) # Outputs errors to the list. 132 publish_string(docstring, writer=writer, reader=reader, 133 settings_overrides={'report_level':10000, 134 'halt_level':10000, 135 'warning_stream':None}) 136 return ParsedRstDocstring(writer.document)
137
138 -class OptimizedReporter(docutils.utils.Reporter):
139 """A reporter that ignores all debug messages. This is used to 140 shave a couple seconds off of epydoc's run time, since docutils 141 isn't very fast about processing its own debug messages."""
142 - def debug(self, *args, **kwargs): pass
143
144 -class ParsedRstDocstring(ParsedDocstring):
145 """ 146 An encoded version of a ReStructuredText docstring. The contents 147 of the docstring are encoded in the L{_document} instance 148 variable. 149 150 @ivar _document: A ReStructuredText document, encoding the 151 docstring. 152 @type _document: C{docutils.nodes.document} 153 """
154 - def __init__(self, document):
155 """ 156 @type document: C{docutils.nodes.document} 157 """ 158 self._document = document 159 160 # The default document reporter and transformer are not 161 # pickle-able; so replace them with stubs that are. 162 document.reporter = OptimizedReporter( 163 document.reporter.source, 'SEVERE', 'SEVERE', '') 164 document.transformer = docutils.transforms.Transformer(document)
165
166 - def split_fields(self, errors=None):
167 # Inherit docs 168 visitor = _SplitFieldsTranslator(self._document, errors) 169 self._document.walk(visitor) 170 if len(self._document.children) > 0: 171 return self, visitor.fields 172 else: 173 return None, visitor.fields
174
175 - def summary(self):
176 # Inherit docs 177 visitor = _SummaryExtractor(self._document) 178 try: self._document.walk(visitor) 179 except docutils.nodes.NodeFound: pass 180 return visitor.summary, bool(visitor.other_docs)
181 182 # def concatenate(self, other): 183 # result = self._document.copy() 184 # for child in (self._document.get_children() + 185 # other._document.get_children()): 186 # visitor = TreeCopyVisitor(self._document) 187 # child.walkabout(visitor) 188 # result.append(visitor.get_tree_copy()) 189 # return ParsedRstDocstring(result) 190
191 - def to_html(self, docstring_linker, directory=None, 192 docindex=None, context=None, **options):
193 # Inherit docs 194 visitor = _EpydocHTMLTranslator(self._document, docstring_linker, 195 directory, docindex, context) 196 self._document.walkabout(visitor) 197 return ''.join(visitor.body)
198
199 - def to_latex(self, docstring_linker, **options):
200 # Inherit docs 201 visitor = _EpydocLaTeXTranslator(self._document, docstring_linker) 202 self._document.walkabout(visitor) 203 return ''.join(visitor.body)
204
205 - def to_plaintext(self, docstring_linker, **options):
206 # This is should be replaced by something better: 207 return self._document.astext()
208
209 - def __repr__(self): return '<ParsedRstDocstring: ...>'
210
211 - def index_terms(self):
212 visitor = _TermsExtractor(self._document) 213 self._document.walkabout(visitor) 214 return visitor.terms
215
216 -class _EpydocReader(ApiLinkReader):
217 """ 218 A reader that captures all errors that are generated by parsing, 219 and appends them to a list. 220 """ 221 # Remove the DocInfo transform, to ensure that :author: fields are 222 # correctly handled. This needs to be handled differently 223 # depending on the version of docutils that's being used, because 224 # the default_transforms attribute was deprecated & replaced by 225 # get_transforms(). 226 version = [int(v) for v in docutils.__version__.split('.')] 227 version += [ 0 ] * (3 - len(version)) 228 if version < [0,4,0]: 229 default_transforms = list(ApiLinkReader.default_transforms) 230 try: default_transforms.remove(docutils.transforms.frontmatter.DocInfo) 231 except ValueError: pass 232 else:
233 - def get_transforms(self):
234 return [t for t in ApiLinkReader.get_transforms(self) 235 if t != docutils.transforms.frontmatter.DocInfo]
236 del version 237
238 - def __init__(self, errors):
239 self._errors = errors 240 ApiLinkReader.__init__(self)
241
242 - def new_document(self):
243 document = new_document(self.source.source_path, self.settings) 244 # Capture all warning messages. 245 document.reporter.attach_observer(self.report) 246 # These are used so we know how to encode warning messages: 247 self._encoding = document.reporter.encoding 248 self._error_handler = document.reporter.error_handler 249 # Return the new document. 250 return document
251
252 - def report(self, error):
253 try: is_fatal = int(error['level']) > 2 254 except: is_fatal = 1 255 try: linenum = int(error['line']) 256 except: linenum = None 257 258 msg = ''.join([c.astext().encode(self._encoding, self._error_handler) 259 for c in error]) 260 261 self._errors.append(ParseError(msg, linenum, is_fatal))
262
263 -class _DocumentPseudoWriter(Writer):
264 """ 265 A pseudo-writer for the docutils framework, that can be used to 266 access the document itself. The output of C{_DocumentPseudoWriter} 267 is just an empty string; but after it has been used, the most 268 recently processed document is available as the instance variable 269 C{document} 270 271 @type document: C{docutils.nodes.document} 272 @ivar document: The most recently processed document. 273 """
274 - def __init__(self):
275 self.document = None 276 Writer.__init__(self)
277
278 - def translate(self):
279 self.output = ''
280
281 -class _SummaryExtractor(NodeVisitor):
282 """ 283 A docutils node visitor that extracts the first sentence from 284 the first paragraph in a document. 285 """
286 - def __init__(self, document):
287 NodeVisitor.__init__(self, document) 288 self.summary = None 289 self.other_docs = None
290
291 - def visit_document(self, node):
292 self.summary = None
293
294 - def visit_paragraph(self, node):
295 if self.summary is not None: 296 # found a paragraph after the first one 297 self.other_docs = True 298 raise docutils.nodes.NodeFound('Found summary') 299 300 summary_pieces = [] 301 302 # Extract the first sentence. 303 for child in node: 304 if isinstance(child, docutils.nodes.Text): 305 m = re.match(r'(\s*[\w\W]*?\.)(\s|$)', child.data) 306 if m: 307 summary_pieces.append(docutils.nodes.Text(m.group(1))) 308 other = child.data[m.end():] 309 if other and not other.isspace(): 310 self.other_docs = True 311 break 312 summary_pieces.append(child) 313 314 summary_doc = self.document.copy() # shallow copy 315 summary_para = node.copy() # shallow copy 316 summary_doc[:] = [summary_para] 317 summary_para[:] = summary_pieces 318 self.summary = ParsedRstDocstring(summary_doc)
319
320 - def visit_field(self, node):
321 raise SkipNode
322
323 - def unknown_visit(self, node):
324 'Ignore all unknown nodes'
325
326 -class _TermsExtractor(NodeVisitor):
327 """ 328 A docutils node visitor that extracts the terms from documentation. 329 330 Terms are created using the C{:term:} interpreted text role. 331 """
332 - def __init__(self, document):
333 NodeVisitor.__init__(self, document) 334 335 self.terms = None 336 """ 337 The terms currently found. 338 @type: C{list} 339 """
340
341 - def visit_document(self, node):
342 self.terms = [] 343 self._in_term = False
344
345 - def visit_emphasis(self, node):
346 if 'term' in node.get('classes'): 347 self._in_term = True
348
349 - def depart_emphasis(self, node):
350 if 'term' in node.get('classes'): 351 self._in_term = False
352
353 - def visit_Text(self, node):
354 if self._in_term: 355 doc = self.document.copy() 356 doc[:] = [node.copy()] 357 self.terms.append(ParsedRstDocstring(doc))
358
359 - def unknown_visit(self, node):
360 'Ignore all unknown nodes'
361
362 - def unknown_departure(self, node):
363 'Ignore all unknown nodes'
364
365 -class _SplitFieldsTranslator(NodeVisitor):
366 """ 367 A docutils translator that removes all fields from a document, and 368 collects them into the instance variable C{fields} 369 370 @ivar fields: The fields of the most recently walked document. 371 @type fields: C{list} of L{Field<markup.Field>} 372 """ 373 374 ALLOW_UNMARKED_ARG_IN_CONSOLIDATED_FIELD = True 375 """If true, then consolidated fields are not required to mark 376 arguments with C{`backticks`}. (This is currently only 377 implemented for consolidated fields expressed as definition lists; 378 consolidated fields expressed as unordered lists still require 379 backticks for now.""" 380
381 - def __init__(self, document, errors):
382 NodeVisitor.__init__(self, document) 383 self._errors = errors 384 self.fields = [] 385 self._newfields = {}
386
387 - def visit_document(self, node):
388 self.fields = []
389
390 - def visit_field(self, node):
391 # Remove the field from the tree. 392 node.parent.remove(node) 393 394 # Extract the field name & optional argument 395 tag = node[0].astext().split(None, 1) 396 tagname = tag[0] 397 if len(tag)>1: arg = tag[1] 398 else: arg = None 399 400 # Handle special fields: 401 fbody = node[1] 402 if arg is None: 403 for (list_tag, entry_tag) in CONSOLIDATED_FIELDS.items(): 404 if tagname.lower() == list_tag: 405 try: 406 self.handle_consolidated_field(fbody, entry_tag) 407 return 408 except ValueError, e: 409 estr = 'Unable to split consolidated field ' 410 estr += '"%s" - %s' % (tagname, e) 411 self._errors.append(ParseError(estr, node.line, 412 is_fatal=0)) 413 414 # Use a @newfield to let it be displayed as-is. 415 if not self._newfields.has_key(tagname.lower()): 416 newfield = Field('newfield', tagname.lower(), 417 parse(tagname, 'plaintext')) 418 self.fields.append(newfield) 419 self._newfields[tagname.lower()] = 1 420 421 self._add_field(tagname, arg, fbody)
422
423 - def _add_field(self, tagname, arg, fbody):
424 field_doc = self.document.copy() 425 for child in fbody: field_doc.append(child) 426 field_pdoc = ParsedRstDocstring(field_doc) 427 self.fields.append(Field(tagname, arg, field_pdoc))
428
429 - def visit_field_list(self, node):
430 # Remove the field list from the tree. The visitor will still walk 431 # over the node's children. 432 node.parent.remove(node)
433
434 - def handle_consolidated_field(self, body, tagname):
435 """ 436 Attempt to handle a consolidated section. 437 """ 438 if len(body) != 1: 439 raise ValueError('does not contain a single list.') 440 elif body[0].tagname == 'bullet_list': 441 self.handle_consolidated_bullet_list(body[0], tagname) 442 elif (body[0].tagname == 'definition_list' and 443 tagname in CONSOLIDATED_DEFLIST_FIELDS): 444 self.handle_consolidated_definition_list(body[0], tagname) 445 elif tagname in CONSOLIDATED_DEFLIST_FIELDS: 446 raise ValueError('does not contain a bulleted list or ' 447 'definition list.') 448 else: 449 raise ValueError('does not contain a bulleted list.')
450
451 - def handle_consolidated_bullet_list(self, items, tagname):
452 # Check the contents of the list. In particular, each list 453 # item should have the form: 454 # - `arg`: description... 455 n = 0 456 _BAD_ITEM = ("list item %d is not well formed. Each item must " 457 "consist of a single marked identifier (e.g., `x`), " 458 "optionally followed by a colon or dash and a " 459 "description.") 460 for item in items: 461 n += 1 462 if item.tagname != 'list_item' or len(item) == 0: 463 raise ValueError('bad bulleted list (bad child %d).' % n) 464 if item[0].tagname != 'paragraph': 465 if item[0].tagname == 'definition_list': 466 raise ValueError(('list item %d contains a definition '+ 467 'list (it\'s probably indented '+ 468 'wrong).') % n) 469 else: 470 raise ValueError(_BAD_ITEM % n) 471 if len(item[0]) == 0: 472 raise ValueError(_BAD_ITEM % n) 473 if item[0][0].tagname != 'title_reference': 474 raise ValueError(_BAD_ITEM % n) 475 476 # Everything looks good; convert to multiple fields. 477 for item in items: 478 # Extract the arg 479 arg = item[0][0].astext() 480 481 # Extract the field body, and remove the arg 482 fbody = item[:] 483 fbody[0] = fbody[0].copy() 484 fbody[0][:] = item[0][1:] 485 486 # Remove the separating ":", if present 487 if (len(fbody[0]) > 0 and 488 isinstance(fbody[0][0], docutils.nodes.Text)): 489 child = fbody[0][0] 490 if child.data[:1] in ':-': 491 child.data = child.data[1:].lstrip() 492 elif child.data[:2] in (' -', ' :'): 493 child.data = child.data[2:].lstrip() 494 495 # Wrap the field body, and add a new field 496 self._add_field(tagname, arg, fbody)
497
498 - def handle_consolidated_definition_list(self, items, tagname):
499 # Check the list contents. 500 n = 0 501 _BAD_ITEM = ("item %d is not well formed. Each item's term must " 502 "consist of a single marked identifier (e.g., `x`), " 503 "optionally followed by a space, colon, space, and " 504 "a type description.") 505 for item in items: 506 n += 1 507 if (item.tagname != 'definition_list_item' or len(item) < 2 or 508 item[0].tagname != 'term' or 509 item[-1].tagname != 'definition'): 510 raise ValueError('bad definition list (bad child %d).' % n) 511 if len(item) > 3: 512 raise ValueError(_BAD_ITEM % n) 513 if not ((item[0][0].tagname == 'title_reference') or 514 (self.ALLOW_UNMARKED_ARG_IN_CONSOLIDATED_FIELD and 515 isinstance(item[0][0], docutils.nodes.Text))): 516 raise ValueError(_BAD_ITEM % n) 517 for child in item[0][1:]: 518 if child.astext() != '': 519 raise ValueError(_BAD_ITEM % n) 520 521 # Extract it. 522 for item in items: 523 # The basic field. 524 arg = item[0][0].astext() 525 fbody = item[-1] 526 self._add_field(tagname, arg, fbody) 527 # If there's a classifier, treat it as a type. 528 if len(item) == 3: 529 type_descr = item[1] 530 self._add_field('type', arg, type_descr)
531
532 - def unknown_visit(self, node):
533 'Ignore all unknown nodes'
534
535 -def latex_head_prefix():
536 document = new_document('<fake>') 537 translator = _EpydocLaTeXTranslator(document, None) 538 return translator.head_prefix
539
540 -class _EpydocLaTeXTranslator(LaTeXTranslator):
541 settings = None
542 - def __init__(self, document, docstring_linker):
543 # Set the document's settings. 544 if self.settings is None: 545 settings = OptionParser([LaTeXWriter()]).get_default_values() 546 self.__class__.settings = settings 547 document.settings = self.settings 548 549 LaTeXTranslator.__init__(self, document) 550 self._linker = docstring_linker 551 552 # Start at section level 3. (Unfortunately, we now have to 553 # set a private variable to make this work; perhaps the standard 554 # latex translator should grow an official way to spell this?) 555 self.section_level = 3 556 self._section_number = [0]*self.section_level
557 558 # Handle interpreted text (crossreferences)
559 - def visit_title_reference(self, node):
560 target = self.encode(node.astext()) 561 xref = self._linker.translate_identifier_xref(target, target) 562 self.body.append(xref) 563 raise SkipNode()
564
565 - def visit_document(self, node): pass
566 - def depart_document(self, node): pass
567 568 # For now, just ignore dotgraphs. [XXX]
569 - def visit_dotgraph(self, node):
570 log.warning("Ignoring dotgraph in latex output (dotgraph " 571 "rendering for latex not implemented yet).") 572 raise SkipNode()
573
574 - def visit_doctest_block(self, node):
575 self.body.append(doctest_to_latex(node[0].astext())) 576 raise SkipNode()
577
578 -class _EpydocHTMLTranslator(HTMLTranslator):
579 settings = None
580 - def __init__(self, document, docstring_linker, directory, 581 docindex, context):
582 self._linker = docstring_linker 583 self._directory = directory 584 self._docindex = docindex 585 self._context = context 586 587 # Set the document's settings. 588 if self.settings is None: 589 settings = OptionParser([HTMLWriter()]).get_default_values() 590 self.__class__.settings = settings 591 document.settings = self.settings 592 593 # Call the parent constructor. 594 HTMLTranslator.__init__(self, document)
595 596 # Handle interpreted text (crossreferences)
597 - def visit_title_reference(self, node):
598 target = self.encode(node.astext()) 599 xref = self._linker.translate_identifier_xref(target, target) 600 self.body.append(xref) 601 raise SkipNode()
602
603 - def should_be_compact_paragraph(self, node):
604 if self.document.children == [node]: 605 return True 606 else: 607 return HTMLTranslator.should_be_compact_paragraph(self, node)
608
609 - def visit_document(self, node): pass
610 - def depart_document(self, node): pass
611
612 - def starttag(self, node, tagname, suffix='\n', **attributes):
613 """ 614 This modified version of starttag makes a few changes to HTML 615 tags, to prevent them from conflicting with epydoc. In particular: 616 - existing class attributes are prefixed with C{'rst-'} 617 - existing names are prefixed with C{'rst-'} 618 - hrefs starting with C{'#'} are prefixed with C{'rst-'} 619 - hrefs not starting with C{'#'} are given target='_top' 620 - all headings (C{<hM{n}>}) are given the css class C{'heading'} 621 """ 622 # Get the list of all attribute dictionaries we need to munge. 623 attr_dicts = [attributes] 624 if isinstance(node, docutils.nodes.Node): 625 attr_dicts.append(node.attributes) 626 if isinstance(node, dict): 627 attr_dicts.append(node) 628 # Munge each attribute dictionary. Unfortunately, we need to 629 # iterate through attributes one at a time because some 630 # versions of docutils don't case-normalize attributes. 631 for attr_dict in attr_dicts: 632 for (key, val) in attr_dict.items(): 633 # Prefix all CSS classes with "rst-"; and prefix all 634 # names with "rst-" to avoid conflicts. 635 if key.lower() in ('class', 'id', 'name'): 636 attr_dict[key] = 'rst-%s' % val 637 elif key.lower() in ('classes', 'ids', 'names'): 638 attr_dict[key] = ['rst-%s' % cls for cls in val] 639 elif key.lower() == 'href': 640 if attr_dict[key][:1]=='#': 641 attr_dict[key] = '#rst-%s' % attr_dict[key][1:] 642 else: 643 # If it's an external link, open it in a new 644 # page. 645 attr_dict['target'] = '_top' 646 647 # For headings, use class="heading" 648 if re.match(r'^h\d+$', tagname): 649 attributes['class'] = ' '.join([attributes.get('class',''), 650 'heading']).strip() 651 652 return HTMLTranslator.starttag(self, node, tagname, suffix, 653 **attributes)
654
655 - def visit_dotgraph(self, node):
656 if self._directory is None: return # [xx] warning? 657 658 # Generate the graph. 659 graph = node.graph(self._docindex, self._context, self._linker) 660 if graph is None: return 661 662 # Write the graph. 663 image_url = '%s.gif' % graph.uid 664 image_file = os.path.join(self._directory, image_url) 665 self.body.append(graph.to_html(image_file, image_url)) 666 raise SkipNode()
667
668 - def visit_doctest_block(self, node):
669 pysrc = node[0].astext() 670 if node.get('codeblock'): 671 self.body.append(HTMLDoctestColorizer().colorize_codeblock(pysrc)) 672 else: 673 self.body.append(doctest_to_html(pysrc)) 674 raise SkipNode()
675 676
677 -def python_code_directive(name, arguments, options, content, lineno, 678 content_offset, block_text, state, state_machine):
679 """ 680 A custom restructuredtext directive which can be used to display 681 syntax-highlighted Python code blocks. This directive takes no 682 arguments, and the body should contain only Python code. This 683 directive can be used instead of doctest blocks when it is 684 inconvenient to list prompts on each line, or when you would 685 prefer that the output not contain prompts (e.g., to make 686 copy/paste easier). 687 """ 688 required_arguments = 0 689 optional_arguments = 0 690 691 text = '\n'.join(content) 692 node = docutils.nodes.doctest_block(text, text, codeblock=True) 693 return [ node ]
694 695 python_code_directive.arguments = (0, 0, 0) 696 python_code_directive.content = True 697 698 directives.register_directive('python', python_code_directive) 699
700 -def term_role(name, rawtext, text, lineno, inliner, 701 options={}, content=[]):
702 703 text = docutils.utils.unescape(text) 704 node = docutils.nodes.emphasis(rawtext, text, **options) 705 node.attributes['classes'].append('term') 706 707 return [node], []
708 709 roles.register_local_role('term', term_role) 710 711 ###################################################################### 712 #{ Graph Generation Directives 713 ###################################################################### 714 # See http://docutils.sourceforge.net/docs/howto/rst-directives.html 715
716 -class dotgraph(docutils.nodes.image):
717 """ 718 A custom docutils node that should be rendered using Graphviz dot. 719 This node does not directly store the graph; instead, it stores a 720 pointer to a function that can be used to generate the graph. 721 This allows the graph to be built based on information that might 722 not be available yet at parse time. This graph generation 723 function has the following signature: 724 725 >>> def generate_graph(docindex, context, linker, *args): 726 ... 'generates and returns a new DotGraph' 727 728 Where C{docindex} is a docindex containing the documentation that 729 epydoc has built; C{context} is the C{APIDoc} whose docstring 730 contains this dotgraph node; C{linker} is a L{DocstringLinker} 731 that can be used to resolve crossreferences; and C{args} is any 732 extra arguments that are passed to the C{dotgraph} constructor. 733 """
734 - def __init__(self, generate_graph_func, *generate_graph_args):
735 docutils.nodes.image.__init__(self) 736 self.graph_func = generate_graph_func 737 self.args = generate_graph_args
738 - def graph(self, docindex, context, linker):
739 return self.graph_func(docindex, context, linker, *self.args)
740
741 -def _dir_option(argument):
742 """A directive option spec for the orientation of a graph.""" 743 argument = argument.lower().strip() 744 if argument == 'right': return 'LR' 745 if argument == 'left': return 'RL' 746 if argument == 'down': return 'TB' 747 if argument == 'up': return 'BT' 748 raise ValueError('%r unknown; choose from left, right, up, down' % 749 argument)
750
751 -def digraph_directive(name, arguments, options, content, lineno, 752 content_offset, block_text, state, state_machine):
753 """ 754 A custom restructuredtext directive which can be used to display 755 Graphviz dot graphs. This directive takes a single argument, 756 which is used as the graph's name. The contents of the directive 757 are used as the body of the graph. Any href attributes whose 758 value has the form <name> will be replaced by the URL of the object 759 with that name. Here's a simple example:: 760 761 .. digraph:: example_digraph 762 a -> b -> c 763 c -> a [dir=\"none\"] 764 """ 765 if arguments: title = arguments[0] 766 else: title = '' 767 return [ dotgraph(_construct_digraph, title, options.get('caption'), 768 '\n'.join(content)) ]
769 digraph_directive.arguments = (0, 1, True) 770 digraph_directive.options = {'caption': directives.unchanged} 771 digraph_directive.content = True 772 directives.register_directive('digraph', digraph_directive) 773
774 -def _construct_digraph(docindex, context, linker, title, caption, 775 body):
776 """Graph generator for L{digraph_directive}""" 777 graph = DotGraph(title, body, caption=caption) 778 graph.link(linker) 779 return graph
780
781 -def classtree_directive(name, arguments, options, content, lineno, 782 content_offset, block_text, state, state_machine):
783 """ 784 A custom restructuredtext directive which can be used to 785 graphically display a class hierarchy. If one or more arguments 786 are given, then those classes and all their descendants will be 787 displayed. If no arguments are given, and the directive is in a 788 class's docstring, then that class and all its descendants will be 789 displayed. It is an error to use this directive with no arguments 790 in a non-class docstring. 791 792 Options: 793 - C{:dir:} -- Specifies the orientation of the graph. One of 794 C{down}, C{right} (default), C{left}, C{up}. 795 """ 796 return [ dotgraph(_construct_classtree, arguments, options) ]
797 classtree_directive.arguments = (0, 1, True) 798 classtree_directive.options = {'dir': _dir_option} 799 classtree_directive.content = False 800 directives.register_directive('classtree', classtree_directive) 801
802 -def _construct_classtree(docindex, context, linker, arguments, options):
803 """Graph generator for L{classtree_directive}""" 804 if len(arguments) == 1: 805 bases = [docindex.find(name, context) for name in 806 arguments[0].replace(',',' ').split()] 807 bases = [d for d in bases if isinstance(d, ClassDoc)] 808 elif isinstance(context, ClassDoc): 809 bases = [context] 810 else: 811 log.warning("Could not construct class tree: you must " 812 "specify one or more base classes.") 813 return None 814 815 return class_tree_graph(bases, linker, context, **options)
816
817 -def packagetree_directive(name, arguments, options, content, lineno, 818 content_offset, block_text, state, state_machine):
819 """ 820 A custom restructuredtext directive which can be used to 821 graphically display a package hierarchy. If one or more arguments 822 are given, then those packages and all their submodules will be 823 displayed. If no arguments are given, and the directive is in a 824 package's docstring, then that package and all its submodules will 825 be displayed. It is an error to use this directive with no 826 arguments in a non-package docstring. 827 828 Options: 829 - C{:dir:} -- Specifies the orientation of the graph. One of 830 C{down}, C{right} (default), C{left}, C{up}. 831 """ 832 return [ dotgraph(_construct_packagetree, arguments, options) ]
833 packagetree_directive.arguments = (0, 1, True) 834 packagetree_directive.options = { 835 'dir': _dir_option, 836 'style': lambda a:directives.choice(a.lower(), ('uml', 'tree'))} 837 packagetree_directive.content = False 838 directives.register_directive('packagetree', packagetree_directive) 839
840 -def _construct_packagetree(docindex, context, linker, arguments, options):
841 """Graph generator for L{packagetree_directive}""" 842 if len(arguments) == 1: 843 packages = [docindex.find(name, context) for name in 844 arguments[0].replace(',',' ').split()] 845 packages = [d for d in packages if isinstance(d, ModuleDoc)] 846 elif isinstance(context, ModuleDoc): 847 packages = [context] 848 else: 849 log.warning("Could not construct package tree: you must " 850 "specify one or more root packages.") 851 return None 852 853 return package_tree_graph(packages, linker, context, **options)
854
855 -def importgraph_directive(name, arguments, options, content, lineno, 856 content_offset, block_text, state, state_machine):
857 return [ dotgraph(_construct_importgraph, arguments, options) ]
858 importgraph_directive.options = {'dir': _dir_option} 859 importgraph_directive.content = False 860 directives.register_directive('importgraph', importgraph_directive) 861
862 -def _construct_importgraph(docindex, context, linker, arguments, options):
863 """Graph generator for L{importgraph_directive}""" 864 modules = [d for d in docindex.root if isinstance(d, ModuleDoc)] 865 return import_graph(modules, docindex, linker, context, **options)
866
867 -def callgraph_directive(name, arguments, options, content, lineno, 868 content_offset, block_text, state, state_machine):
869 return [ dotgraph(_construct_callgraph, arguments, options) ]
870 callgraph_directive.arguments = (0, 1, True) 871 callgraph_directive.options = {'dir': _dir_option, 872 'add_callers': directives.flag, 873 'add_callees': directives.flag} 874 callgraph_directive.content = False 875 directives.register_directive('callgraph', callgraph_directive) 876
877 -def _construct_callgraph(docindex, context, linker, arguments, options):
878 """Graph generator for L{callgraph_directive}""" 879 if len(arguments) == 1: 880 docs = [docindex.find(name, context) for name in 881 arguments[0].replace(',',' ').split()] 882 docs = [doc for doc in docs if doc is not None] 883 else: 884 docs = [context] 885 return call_graph(docs, docindex, linker, context, **options)
886