File: docutil.py

package info (click to toggle)
python-gendoc 0.73-8
  • links: PTS
  • area: main
  • in suites: woody
  • size: 312 kB
  • ctags: 845
  • sloc: python: 2,610; makefile: 124; sh: 26
file content (297 lines) | stat: -rw-r--r-- 8,610 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#
# FILE            $Id: docutil.py,v 1.5 1996/09/06 19:14:24 omfadmin Exp $
#
# DESCRIPTION     Collect info about modules, and generate Manual page.
#
# AUTHOR          SEISY/LKSB Daniel Larsson
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted,
# provided that the above copyright notice appear in all copies and that
# both that copyright notice and this permission notice appear in
# supporting documentation, and that the name of ABB Industrial Systems
# not be used in advertising or publicity pertaining to
# distribution of the software without specific, written prior permission.
#
# ABB INDUSTRIAL SYSTEMS DISCLAIMS ALL WARRANTIES WITH REGARD TO
# THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS, IN NO EVENT SHALL ABB INDUSTRIAL SYSTEMS BE LIABLE
# FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
# 
# Copyright (C) ABB Industrial Systems AB, 1996
# Unpublished work.  All Rights Reserved.
#
# HISTORY: $Log: /Gendoc/docutil.py $
# 
# 1     98-04-01 13:15 Daniel
# Revision 1.5  1996/09/06  19:14:24  omfadmin
# Fixes by Robin Friedrich (stripped too much in stripleadingtabs)
# Various small fixes.
# NOTE: Nested lists doesn't work yet!! I know basically what the
# problem is, but I'm going to Spain now...
#
# Revision 1.4  1996/09/06  09:55:07  omfadmin
# Replaced setext markup with structured text markup.
#
# Revision 1.3  1996/09/04  14:07:40  omfadmin
# More StructuredText. Removed most of setext.
#
# Revision 1.2  1996/08/26  20:29:41  omfadmin
# Added structured text (Jim Fulton) support, at least to some
# degree.
#
# Revision 1.1  1996/07/11  16:38:17  omfadmin
# Initial revision
#

"""Utility routines for doc strings."""

__author__ = "Daniel Larsson, dlarsson@sw.seisy.abb.se"
__version__ = '$Revision: 1 $'

import string, StructuredText
import regex, regsub, docregex
from ManualPage import UN_LIST, OR_LIST, DEF_LIST

# ------  Some regular expressions used to parse the __doc__ strings  ------

# Find out how many leading tabs/spaces are used in a docstring.
_lead_tab = regex.compile('.*\n+\\( *\\)')


# The Python manuals recommend the following layout of
# docstrings:
# """One-liner describing fun/class/module
# <empty line>
# Longer description of fun/class/module"""
_oneliner 	= regex.compile('.*\(\n *\)+\n')
_oneliner_only	= regex.compile('.*')

def stripleadingtabs(s):
    """Strip leading tabs in the string ~s~.
	
    The leading tabs, such as the one on this line, will be
    stripped by this function for a more readable string"""

    s = StructuredText.untabify(s)
    if _lead_tab.match(s) != -1:
	tabs = _lead_tab.group(1)
	return string.join(string.split(s, '\n'+tabs), '\n')
    else:
	return s

def split_doc(doc):
    """Splits docstring into the oneliner part and the rest,
    assuming there is a oneliner."""

    if not doc: return '', ''

    # Evaluate doc to remove enclosing "'s
    doc = stripleadingtabs(doc)
    count = _oneliner.match(doc)
    if count != -1:
	# Strip away trailing two newlines
	oneliner = _oneliner.group(0)[:-2]

	doc = doc[count:]

    else:
	count = _oneliner_only.match(doc)

	if count == len(doc):
	    oneliner = doc
	    doc = ''
	else:
	    oneliner = ''
    return (oneliner, doc)


class rexpiter:
    def __init__(self, rexp, text):
	if type(rexp) == type(''):
	    rexp = regex.compile(rexp)
	self.rexp = rexp
	self.text = text
    def __getitem__(self, index):
	count = self.rexp.match(self.text)
	if count == -1: raise IndexError # Stop iteration
	self.text = self.text[count:]
	return self.rexp

class rexpitersearch:
    def __init__(self, rexp, text):
	if type(rexp) == type(''):
	    rexp = regex.compile(rexp)
	self.rexp = rexp
	self.text = text
    def __getitem__(self, index):
	count = self.rexp.match(self.text)
	if count == -1: raise IndexError # Stop iteration
	self.text = self.text[count:]
	return self.rexp

class StructuredTextParser:
    def __init__(self, manpage, section, text):
	self.text = text
	self.manpage = manpage
	self.topsection = self.subsection = self.section = section
	self.list = None

    def parse(self):
	paragraphs = regsub.split(self.text, StructuredText.paragraph_divider)
	paragraphs = map(StructuredText.indent_level, paragraphs)

	structure = StructuredText.structure(paragraphs)
	self._walk(structure) # level = 1

    def _walk(self, structure, level=1, in_list=0, pre=0):
	r=''
	listtype = None
	list = []
	for par, sub in structure:

	    # Check for hyperlink definition
	    found = 0
	    for rexp in rexpiter(docregex.hyperdef_regex, par):
		found = 1
		if listtype:
		    self.end_list(listtype, list, in_list)
		    list = []
		listtype = None
		text, url = rexp.group(1,2)
		self.visit_hyperdef(url, text)

	    if found: continue

	    # Check for bullet lists
	    if docregex.bullet_regex.match(par) >= 0:
		first_item = listtype != UN_LIST
		if first_item and listtype:
		    self.end_list(listtype, list, in_list)
		    list = []
		listtype = UN_LIST

		# Allow bullet list without intervening '\n\n'
		l = string.splitfields(par, docregex.bullet_regex.group(1))[1:]
		list = list + l

		sublist = self._walk(sub, level+1, in_list=1)
		if sublist[0]:
		    list.append(sublist)
		continue

	    # Check for ordered lists
	    elif docregex.ol_regex.match(par) >= 0:
		first_item = listtype != OR_LIST
		if first_item and listtype:
		    self.end_list(listtype, list, in_list)
		    list = []
		listtype = OR_LIST

		text = docregex.ol_regex.group(3)
		list.append(text)
		sublist = self._walk(sub, level+1, in_list=1)
		if sublist[0]:
		    list.append(sublist)

	    # Check for ordered lists (alternative syntax)
	    elif docregex.olp_regex.match(par) >= 0:
		first_item = listtype != OR_LIST
		if first_item and listtype:
		    self.end_list(listtype, list, in_list)
		    list = []
		listtype = OR_LIST

		text = docregex.olp_regex.group(3)
		list.append(text)
		sublist = self._walk(sub, level+1, in_list=1)
		if sublist[0]:
		    list.append(sublist)

	    # Check for definition lists
	    elif docregex.dl_regex.match(par) >= 0:
		t,d = docregex.dl_regex.group(1,2)
		first_item = listtype != DEF_LIST
		if first_item and listtype:
		    self.end_list(listtype, list, in_list)
		    list = []
		listtype = DEF_LIST
		#
		count = docregex.dl_regex.search(d)
		while count >= 0:
		    list.append((t, d[:count-1]))
		    t,d = docregex.dl_regex.group(1,2)
		    count = docregex.dl_regex.search(d)
		    
		list.append((t,d))
		sublist = self._walk(sub, level+1, in_list=1)
		if sublist[0]:
		    list.append(sublist)

	    # Check for 'example' headings
	    elif docregex.example_regex.search(par) >= 0 and sub:
		if listtype:
		    self.end_list(listtype, list, in_list)
		    list = []
		listtype = None
		self.visit_head(par, level)
		self._walk(sub, level+1, in_list, pre=1)

	    # Check for standard headings
	    elif docregex.nl_regex.search(par) < 0 and sub:
		# Treat as a heading
		if listtype:
		    self.end_list(listtype, list, in_list)
		    list = []
		listtype = None
		self.visit_head(par, level)
		self._walk(sub, level+1, in_list, pre)

	    else:
		if listtype:
		    self.end_list(listtype, list, in_list)
		    list = []
		listtype = None
		if pre:
		    self.visit_pre(par, level)
		else:
		    self.visit_normal(par, level)
		self._walk(sub, level+1, in_list)

	if listtype:
	    self.end_list(listtype, list, in_list)
	return listtype, list

    def end_list(self, listtype, list, in_list):
	if not in_list:
	    self.manpage.list(listtype, list[:], self.subsection)

    def visit_head(self, heading, level):
	if level > 1:
	    self.subsection = self.manpage.section(heading, self.section)
	else:
	    self.section = self.manpage.section(heading, self.topsection)
	    self.subsection = self.section
	
    def visit_hyperdef(self, url, text):
	self.manpage.add_hyperlink(url, text)

    def visit_normal(self, text, level):
	self.manpage.paragraph(text, self.subsection)

    def visit_pre(self, text, level):
	self.manpage.code(text, self.subsection)

	
def docregex_parse(manpage, section, doc):
    """Parse doc for docregex markup.

    Looks for markups in *doc*, and inserts the
    appropriate paragraphs/headings, etc, into *section*
    of *manpage*."""
    
    StructuredTextParser(manpage, section, doc).parse()
    #print manpage._doctree_