1
2
3
4
5
6
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
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
54 m = re.match(r'(\s*[\w\W]*?\.)(\s|$)', self._text)
55 if m:
56 other = self._text[m.end():]
57 return (ParsedPlaintextDocstring(m.group(1), verbatim=0),
58 other != '' and not other.isspace())
59 else:
60 parts = self._text.strip('\n').split('\n', 1)
61 if len(parts) == 1:
62 summary = parts[0]
63 other = False
64 else:
65 summary = parts[0] + '...'
66 other = True
67
68 return ParsedPlaintextDocstring(summary, verbatim=0), other
69
70
71
72
73
74
75
76
77