Show More
@@ -192,6 +192,7 b' def prunecontainers(blocks, keep):' | |||
|
192 | 192 | The blocks must have a 'type' field, i.e., they should have been |
|
193 | 193 | run through findliteralblocks first. |
|
194 | 194 | """ |
|
195 | pruned = [] | |
|
195 | 196 | i = 0 |
|
196 | 197 | while i + 1 < len(blocks): |
|
197 | 198 | # Searching for a block that looks like this: |
@@ -207,6 +208,8 b' def prunecontainers(blocks, keep):' | |||
|
207 | 208 | adjustment = blocks[i + 1]['indent'] - indent |
|
208 | 209 | containertype = blocks[i]['lines'][0][15:] |
|
209 | 210 | prune = containertype not in keep |
|
211 | if prune: | |
|
212 | pruned.append(containertype) | |
|
210 | 213 | |
|
211 | 214 | # Always delete "..container:: type" block |
|
212 | 215 | del blocks[i] |
@@ -219,7 +222,7 b' def prunecontainers(blocks, keep):' | |||
|
219 | 222 | blocks[j]['indent'] -= adjustment |
|
220 | 223 | j += 1 |
|
221 | 224 | i += 1 |
|
222 | return blocks | |
|
225 | return blocks, pruned | |
|
223 | 226 | |
|
224 | 227 | |
|
225 | 228 | def findsections(blocks): |
@@ -317,19 +320,23 b' def formatblock(block, width):' | |||
|
317 | 320 | subsequent_indent=subindent) |
|
318 | 321 | |
|
319 | 322 | |
|
320 |
def format(text, width, indent=0, keep= |
|
|
323 | def format(text, width, indent=0, keep=None): | |
|
321 | 324 | """Parse and format the text according to width.""" |
|
322 | 325 | blocks = findblocks(text) |
|
323 | 326 | for b in blocks: |
|
324 | 327 | b['indent'] += indent |
|
325 | 328 | blocks = findliteralblocks(blocks) |
|
326 | blocks = prunecontainers(blocks, keep) | |
|
329 | blocks, pruned = prunecontainers(blocks, keep or []) | |
|
327 | 330 | blocks = inlineliterals(blocks) |
|
328 | 331 | blocks = splitparagraphs(blocks) |
|
329 | 332 | blocks = updatefieldlists(blocks) |
|
330 | 333 | blocks = findsections(blocks) |
|
331 | 334 | blocks = addmargins(blocks) |
|
332 |
|
|
|
335 | text = '\n'.join(formatblock(b, width) for b in blocks) | |
|
336 | if keep is None: | |
|
337 | return text | |
|
338 | else: | |
|
339 | return text, pruned | |
|
333 | 340 | |
|
334 | 341 | |
|
335 | 342 | if __name__ == "__main__": |
@@ -1,11 +1,18 b'' | |||
|
1 | 1 | #!/usr/bin/env python |
|
2 | 2 | |
|
3 | from pprint import pprint | |
|
3 | 4 | from mercurial import minirst |
|
4 | 5 | |
|
5 | 6 | def debugformat(title, text, width, **kwargs): |
|
6 | 7 | print "%s formatted to fit within %d characters:" % (title, width) |
|
7 | 8 | print "-" * 70 |
|
8 |
|
|
|
9 | formatted = minirst.format(text, width, **kwargs) | |
|
10 | if type(formatted) == tuple: | |
|
11 | print formatted[0] | |
|
12 | print "-" * 70 | |
|
13 | pprint(formatted[1]) | |
|
14 | else: | |
|
15 | print formatted | |
|
9 | 16 | print "-" * 70 |
|
10 | 17 | |
|
11 | 18 |
@@ -257,6 +257,8 b' Normal output.' | |||
|
257 | 257 | |
|
258 | 258 | Verbose output. |
|
259 | 259 | ---------------------------------------------------------------------- |
|
260 | ['debug', 'debug'] | |
|
261 | ---------------------------------------------------------------------- | |
|
260 | 262 | |
|
261 | 263 | containers (debug) formatted to fit within 60 characters: |
|
262 | 264 | ---------------------------------------------------------------------- |
@@ -264,6 +266,8 b' Normal output.' | |||
|
264 | 266 | |
|
265 | 267 | Initial debug output. |
|
266 | 268 | ---------------------------------------------------------------------- |
|
269 | ['verbose'] | |
|
270 | ---------------------------------------------------------------------- | |
|
267 | 271 | |
|
268 | 272 | containers (verbose debug) formatted to fit within 60 characters: |
|
269 | 273 | ---------------------------------------------------------------------- |
@@ -275,4 +279,6 b' Verbose output.' | |||
|
275 | 279 | |
|
276 | 280 | Debug output. |
|
277 | 281 | ---------------------------------------------------------------------- |
|
282 | [] | |
|
283 | ---------------------------------------------------------------------- | |
|
278 | 284 |
General Comments 0
You need to be logged in to leave comments.
Login now