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

Source Code for Module epydoc.markup.plaintext

 1  # 
 2  # plaintext.py: plaintext docstring parsing 
 3  # Edward Loper 
 4  # 
 5  # Created [04/10/01 12:00 AM] 
 6  # $Id: plaintext.py 956 2006-03-10 01:30:51Z edloper $ 
 7  # 
 8   
 9  """ 
10  Parser for plaintext docstrings.  Plaintext docstrings are rendered as 
11  verbatim output, preserving all whitespace. 
12  """ 
13  __docformat__ = 'epytext en' 
14   
15  from epydoc.markup import * 
16  from epydoc.util import plaintext_to_html, plaintext_to_latex 
17   
18 -def parse_docstring(docstring, errors, **options):
19 """ 20 @return: A pair C{(M{d}, M{e})}, where C{M{d}} is a 21 C{ParsedDocstring} that encodes the contents of the given 22 plaintext docstring; and C{M{e}} is a list of errors that were 23 generated while parsing the docstring. 24 @rtype: C{L{ParsedPlaintextDocstring}, C{list} of L{ParseError}} 25 """ 26 return ParsedPlaintextDocstring(docstring, **options)
27
28 -class ParsedPlaintextDocstring(ParsedDocstring):
29 - def __init__(self, text, **options):
30 self._verbatim = options.get('verbatim', 1) 31 if text is None: raise ValueError, 'Bad text value (expected a str)' 32 self._text = text
33
34 - def to_html(self, docstring_linker, **options):
35 if options.get('verbatim', self._verbatim) == 0: 36 return plaintext_to_html(self.to_plaintext(docstring_linker)) 37 else: 38 return ParsedDocstring.to_html(self, docstring_linker, **options)
39
40 - def to_latex(self, docstring_linker, **options):
41 if options.get('verbatim', self._verbatim) == 0: 42 return plaintext_to_latex(self.to_plaintext(docstring_linker)) 43 else: 44 return ParsedDocstring.to_latex(self, docstring_linker, **options)
45
46 - def to_plaintext(self, docstring_linker, **options):
47 if 'indent' in options: 48 indent = options['indent'] 49 lines = self._text.split('\n') 50 return '\n'.join([' '*indent+l for l in lines])+'\n' 51 return self._text+'\n'
52
53 - def summary(self):
54 m = re.match(r'(\s*[\w\W]*?\.)(\s|$)', self._text) 55 if m: 56 return ParsedPlaintextDocstring(m.group(1), verbatim=0) 57 else: 58 summary = self._text.split('\n', 1)[0]+'...' 59 return ParsedPlaintextDocstring(summary, verbatim=0)
60 61 # def concatenate(self, other): 62 # if not isinstance(other, ParsedPlaintextDocstring): 63 # raise ValueError, 'Could not concatenate docstrings' 64 # text = self._text+other._text 65 # options = self._options.copy() 66 # options.update(other._options) 67 # return ParsedPlaintextDocstring(text, options) 68