blob: 9825b346739b18d99fdff1a7379da821583ff1c7 [file] [log] [blame]
Tor Norbye3a2425a2013-11-04 10:16:08 -08001#
2# plaintext.py: plaintext docstring parsing
3# Edward Loper
4#
5# Created [04/10/01 12:00 AM]
6# $Id: plaintext.py 1574 2007-03-07 02:55:14Z dvarrazzo $
7#
8
9"""
10Parser for plaintext docstrings. Plaintext docstrings are rendered as
11verbatim output, preserving all whitespace.
12"""
13__docformat__ = 'epytext en'
14
15from epydoc.markup import *
16from epydoc.util import plaintext_to_html, plaintext_to_latex
17
18def 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
28class 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
55 def summary(self):
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# def concatenate(self, other):
73# if not isinstance(other, ParsedPlaintextDocstring):
74# raise ValueError, 'Could not concatenate docstrings'
75# text = self._text+other._text
76# options = self._options.copy()
77# options.update(other._options)
78# return ParsedPlaintextDocstring(text, options)