##// END OF EJS Templates
minirst: parse field lists
Martin Geisler -
r9293:e48a48b7 default
parent child Browse files
Show More
@@ -22,6 +22,8 b' It only supports a small subset of reStr'
22
22
23 - lists (items must start with '-')
23 - lists (items must start with '-')
24
24
25 - field lists (colons cannot be escaped)
26
25 - literal blocks
27 - literal blocks
26
28
27 - option lists (supports only long options without arguments)
29 - option lists (supports only long options without arguments)
@@ -188,6 +190,43 b' def findoptionlists(blocks):'
188 return blocks
190 return blocks
189
191
190
192
193 _fieldre = re.compile(r':(?![: ])([^:]*)(?<! ):( +)(.*)')
194 def findfieldlists(blocks):
195 """Finds fields lists.
196
197 The blocks must have a 'type' field, i.e., they should have been
198 run through findliteralblocks first.
199 """
200 i = 0
201 while i < len(blocks):
202 # Searching for a paragraph that looks like this:
203 #
204 #
205 # +--------------------+----------------------+
206 # | ":" field name ":" | field body |
207 # +-------+------------+ |
208 # | (body elements)+ |
209 # +-----------------------------------+
210 if (blocks[i]['type'] == 'paragraph' and
211 _fieldre.match(blocks[i]['lines'][0])):
212 indent = blocks[i]['indent']
213 fields = []
214 for line in blocks[i]['lines']:
215 m = _fieldre.match(line)
216 if m:
217 key, spaces, rest = m.groups()
218 width = 2 + len(key) + len(spaces)
219 fields.append(dict(type='field', lines=[],
220 indent=indent, width=width))
221 # Turn ":foo: bar" into "foo bar".
222 line = '%s %s%s' % (key, spaces, rest)
223 fields[-1]['lines'].append(line)
224 blocks[i:i+1] = fields
225 i += len(fields) - 1
226 i += 1
227 return blocks
228
229
191 def finddefinitionlists(blocks):
230 def finddefinitionlists(blocks):
192 """Finds definition lists.
231 """Finds definition lists.
193
232
@@ -230,7 +269,7 b' def addmargins(blocks):'
230 i = 1
269 i = 1
231 while i < len(blocks):
270 while i < len(blocks):
232 if (blocks[i]['type'] == blocks[i-1]['type'] and
271 if (blocks[i]['type'] == blocks[i-1]['type'] and
233 blocks[i]['type'] in ('bullet', 'option', 'definition')):
272 blocks[i]['type'] in ('bullet', 'option', 'field', 'definition')):
234 i += 1
273 i += 1
235 else:
274 else:
236 blocks.insert(i, dict(lines=[''], indent=0, type='margin'))
275 blocks.insert(i, dict(lines=[''], indent=0, type='margin'))
@@ -261,7 +300,7 b' def formatblock(block, width):'
261 if block['type'] == 'bullet':
300 if block['type'] == 'bullet':
262 initindent = indent + '- '
301 initindent = indent + '- '
263 subindent = indent + ' '
302 subindent = indent + ' '
264 elif block['type'] == 'option':
303 elif block['type'] in ('option', 'field'):
265 subindent = indent + block['width'] * ' '
304 subindent = indent + block['width'] * ' '
266
305
267 return textwrap.fill(text, width=width,
306 return textwrap.fill(text, width=width,
@@ -276,6 +315,7 b' def format(text, width):'
276 blocks = findsections(blocks)
315 blocks = findsections(blocks)
277 blocks = findbulletlists(blocks)
316 blocks = findbulletlists(blocks)
278 blocks = findoptionlists(blocks)
317 blocks = findoptionlists(blocks)
318 blocks = findfieldlists(blocks)
279 blocks = finddefinitionlists(blocks)
319 blocks = finddefinitionlists(blocks)
280 blocks = addmargins(blocks)
320 blocks = addmargins(blocks)
281 return '\n'.join(formatblock(b, width) for b in blocks)
321 return '\n'.join(formatblock(b, width) for b in blocks)
@@ -297,6 +337,7 b' if __name__ == "__main__":'
297 blocks = debug(findsections, blocks)
337 blocks = debug(findsections, blocks)
298 blocks = debug(findbulletlists, blocks)
338 blocks = debug(findbulletlists, blocks)
299 blocks = debug(findoptionlists, blocks)
339 blocks = debug(findoptionlists, blocks)
340 blocks = debug(findfieldlists, blocks)
300 blocks = debug(finddefinitionlists, blocks)
341 blocks = debug(finddefinitionlists, blocks)
301 blocks = debug(addmargins, blocks)
342 blocks = debug(addmargins, blocks)
302 print '\n'.join(formatblock(b, 30) for b in blocks)
343 print '\n'.join(formatblock(b, 30) for b in blocks)
@@ -137,3 +137,17 b' marker after the option. It is treated a'
137
137
138 debugformat('options', options, 60)
138 debugformat('options', options, 60)
139 debugformat('options', options, 30)
139 debugformat('options', options, 30)
140
141
142 fields = """
143 Field lists give a simple two-column layout:
144
145 :key: The whitespace following the key is
146 significant for the wrapping of this text.
147 :another key: More text.
148 The indentation on the following
149 lines is not significant.
150 """
151
152 debugformat('fields', fields, 60)
153 debugformat('fields', fields, 30)
@@ -209,3 +209,31 b' normal paragraph:'
209 --foo bar baz
209 --foo bar baz
210 ----------------------------------------------------------------------
210 ----------------------------------------------------------------------
211
211
212 fields formatted to fit within 60 characters:
213 ----------------------------------------------------------------------
214 Field lists give a simple two-column layout:
215
216 key The whitespace following the key is
217 significant for the wrapping of this text.
218 another key More text. The indentation on the following
219 lines is not significant.
220 ----------------------------------------------------------------------
221
222 fields formatted to fit within 30 characters:
223 ----------------------------------------------------------------------
224 Field lists give a simple two-
225 column layout:
226
227 key The whitespace
228 following the
229 key is
230 significant for
231 the wrapping of
232 this text.
233 another key More text. The
234 indentation on
235 the following
236 lines is not
237 significant.
238 ----------------------------------------------------------------------
239
General Comments 0
You need to be logged in to leave comments. Login now