Mercurial > hg
comparison mercurial/minirst.py @ 31132:bbdd712e9adb
minirst: support passing admonitions into findadmonitions() and parse()
This will allow consumers to declare a custom list of admonitions
to parse. Without this patch, custom admonitions would get removed
when prunecomments() is run. We could add an argument controlling
whether prunecomments() is run. However, it is better to convert
the "paragraph" block to an "admonition" block so consumers don't
have to parse for custom admonitions.
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Wed, 15 Feb 2017 11:49:12 -0800 |
parents | 50a49ead4db4 |
children | 0bd32d7c9002 |
comparison
equal
deleted
inserted
replaced
31131:50a49ead4db4 | 31132:bbdd712e9adb |
---|---|
423 'note', | 423 'note', |
424 'tip', | 424 'tip', |
425 'warning', | 425 'warning', |
426 ]) | 426 ]) |
427 | 427 |
428 def findadmonitions(blocks): | 428 def findadmonitions(blocks, admonitions=None): |
429 """ | 429 """ |
430 Makes the type of the block an admonition block if | 430 Makes the type of the block an admonition block if |
431 the first line is an admonition directive | 431 the first line is an admonition directive |
432 """ | 432 """ |
433 admonitionre = re.compile(r'\.\. (%s)::' % '|'.join(sorted(_admonitions)), | 433 admonitions = admonitions or _admonitions |
434 | |
435 admonitionre = re.compile(r'\.\. (%s)::' % '|'.join(sorted(admonitions)), | |
434 flags=re.IGNORECASE) | 436 flags=re.IGNORECASE) |
435 | 437 |
436 i = 0 | 438 i = 0 |
437 while i < len(blocks): | 439 while i < len(blocks): |
438 m = admonitionre.match(blocks[i]['lines'][0]) | 440 m = admonitionre.match(blocks[i]['lines'][0]) |
640 out.append('</%s>\n' % start) | 642 out.append('</%s>\n' % start) |
641 listnest.pop() | 643 listnest.pop() |
642 | 644 |
643 return ''.join(out) | 645 return ''.join(out) |
644 | 646 |
645 def parse(text, indent=0, keep=None): | 647 def parse(text, indent=0, keep=None, admonitions=None): |
646 """Parse text into a list of blocks""" | 648 """Parse text into a list of blocks""" |
647 pruned = [] | 649 pruned = [] |
648 blocks = findblocks(text) | 650 blocks = findblocks(text) |
649 for b in blocks: | 651 for b in blocks: |
650 b['indent'] += indent | 652 b['indent'] += indent |
655 blocks = inlineliterals(blocks) | 657 blocks = inlineliterals(blocks) |
656 blocks = hgrole(blocks) | 658 blocks = hgrole(blocks) |
657 blocks = splitparagraphs(blocks) | 659 blocks = splitparagraphs(blocks) |
658 blocks = updatefieldlists(blocks) | 660 blocks = updatefieldlists(blocks) |
659 blocks = updateoptionlists(blocks) | 661 blocks = updateoptionlists(blocks) |
660 blocks = findadmonitions(blocks) | 662 blocks = findadmonitions(blocks, admonitions=admonitions) |
661 blocks = addmargins(blocks) | 663 blocks = addmargins(blocks) |
662 blocks = prunecomments(blocks) | 664 blocks = prunecomments(blocks) |
663 return blocks, pruned | 665 return blocks, pruned |
664 | 666 |
665 def formatblocks(blocks, width): | 667 def formatblocks(blocks, width): |