Mercurial > hg
comparison mercurial/minirst.py @ 10983:287a5cdf7743
minirst: correctly format sections containing inline markup
Before, a section like
``foo``
-------
would be formatted as
"foo"
-------
We now recompute the length of the underline when formatting the
section.
author | Martin Geisler <mg@lazybytes.net> |
---|---|
date | Sun, 25 Apr 2010 17:48:26 +0200 |
parents | 0a2c6948f5f4 |
children | 68b7d2d668ce |
comparison
equal
deleted
inserted
replaced
10982:0a548640e012 | 10983:287a5cdf7743 |
---|---|
239 # | ------------- | | 239 # | ------------- | |
240 # +------------------------------+ | 240 # +------------------------------+ |
241 if (block['type'] == 'paragraph' and | 241 if (block['type'] == 'paragraph' and |
242 len(block['lines']) == 2 and | 242 len(block['lines']) == 2 and |
243 block['lines'][1] == '-' * len(block['lines'][0])): | 243 block['lines'][1] == '-' * len(block['lines'][0])): |
244 block['underline'] = block['lines'][1][0] | |
244 block['type'] = 'section' | 245 block['type'] = 'section' |
246 del block['lines'][1] | |
245 return blocks | 247 return blocks |
246 | 248 |
247 | 249 |
248 def inlineliterals(blocks): | 250 def inlineliterals(blocks): |
249 for b in blocks: | 251 for b in blocks: |
250 if b['type'] == 'paragraph': | 252 if b['type'] in ('paragraph', 'section'): |
251 b['lines'] = [l.replace('``', '"') for l in b['lines']] | 253 b['lines'] = [l.replace('``', '"') for l in b['lines']] |
252 return blocks | 254 return blocks |
253 | 255 |
254 | 256 |
255 _hgrolere = re.compile(r':hg:`([^`]+)`') | 257 _hgrolere = re.compile(r':hg:`([^`]+)`') |
256 | 258 |
257 def hgrole(blocks): | 259 def hgrole(blocks): |
258 for b in blocks: | 260 for b in blocks: |
259 if b['type'] == 'paragraph': | 261 if b['type'] in ('paragraph', 'section'): |
260 b['lines'] = [_hgrolere.sub(r'"hg \1"', l) for l in b['lines']] | 262 b['lines'] = [_hgrolere.sub(r'"hg \1"', l) for l in b['lines']] |
261 return blocks | 263 return blocks |
262 | 264 |
263 | 265 |
264 def addmargins(blocks): | 266 def addmargins(blocks): |
287 return '' | 289 return '' |
288 if block['type'] == 'literal': | 290 if block['type'] == 'literal': |
289 indent += ' ' | 291 indent += ' ' |
290 return indent + ('\n' + indent).join(block['lines']) | 292 return indent + ('\n' + indent).join(block['lines']) |
291 if block['type'] == 'section': | 293 if block['type'] == 'section': |
292 return indent + ('\n' + indent).join(block['lines']) | 294 underline = len(block['lines'][0]) * block['underline'] |
295 return "%s%s\n%s%s" % (indent, block['lines'][0],indent, underline) | |
293 if block['type'] == 'definition': | 296 if block['type'] == 'definition': |
294 term = indent + block['lines'][0] | 297 term = indent + block['lines'][0] |
295 hang = len(block['lines'][-1]) - len(block['lines'][-1].lstrip()) | 298 hang = len(block['lines'][-1]) - len(block['lines'][-1].lstrip()) |
296 defindent = indent + hang * ' ' | 299 defindent = indent + hang * ' ' |
297 text = ' '.join(map(str.strip, block['lines'][1:])) | 300 text = ' '.join(map(str.strip, block['lines'][1:])) |
339 blocks = findblocks(text) | 342 blocks = findblocks(text) |
340 for b in blocks: | 343 for b in blocks: |
341 b['indent'] += indent | 344 b['indent'] += indent |
342 blocks = findliteralblocks(blocks) | 345 blocks = findliteralblocks(blocks) |
343 blocks, pruned = prunecontainers(blocks, keep or []) | 346 blocks, pruned = prunecontainers(blocks, keep or []) |
347 blocks = findsections(blocks) | |
344 blocks = inlineliterals(blocks) | 348 blocks = inlineliterals(blocks) |
345 blocks = hgrole(blocks) | 349 blocks = hgrole(blocks) |
346 blocks = splitparagraphs(blocks) | 350 blocks = splitparagraphs(blocks) |
347 blocks = updatefieldlists(blocks) | 351 blocks = updatefieldlists(blocks) |
348 blocks = findsections(blocks) | |
349 blocks = addmargins(blocks) | 352 blocks = addmargins(blocks) |
350 text = '\n'.join(formatblock(b, width) for b in blocks) | 353 text = '\n'.join(formatblock(b, width) for b in blocks) |
351 if keep is None: | 354 if keep is None: |
352 return text | 355 return text |
353 else: | 356 else: |