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