Mercurial > hg
comparison mercurial/minirst.py @ 22587:c3c3dd31fe1c
help: basic support for showing only specified topic sections
For instance, 'hg help config.files' will show only the Files section.
author | Matt Mackall <mpm@selenic.com> |
---|---|
date | Tue, 30 Sep 2014 16:40:10 -0500 |
parents | 19bd8bda6bb2 |
children | 3f808549d426 |
comparison
equal
deleted
inserted
replaced
22586:6e5657ce9e8c | 22587:c3c3dd31fe1c |
---|---|
646 | 646 |
647 def formatblocks(blocks, width): | 647 def formatblocks(blocks, width): |
648 text = ''.join(formatblock(b, width) for b in blocks) | 648 text = ''.join(formatblock(b, width) for b in blocks) |
649 return text | 649 return text |
650 | 650 |
651 def format(text, width=80, indent=0, keep=None, style='plain'): | 651 def format(text, width=80, indent=0, keep=None, style='plain', section=None): |
652 """Parse and format the text according to width.""" | 652 """Parse and format the text according to width.""" |
653 blocks, pruned = parse(text, indent, keep or []) | 653 blocks, pruned = parse(text, indent, keep or []) |
654 if section: | |
655 sections = getsections(blocks) | |
656 blocks = [] | |
657 for name, nest, b in sections: | |
658 if name == section: | |
659 blocks = b | |
654 if style == 'html': | 660 if style == 'html': |
655 text = formathtml(blocks) | 661 text = formathtml(blocks) |
656 else: | 662 else: |
657 text = ''.join(formatblock(b, width) for b in blocks) | 663 text = ''.join(formatblock(b, width) for b in blocks) |
658 if keep is None: | 664 if keep is None: |
663 def getsections(blocks): | 669 def getsections(blocks): |
664 '''return a list of (section name, nesting level, blocks) tuples''' | 670 '''return a list of (section name, nesting level, blocks) tuples''' |
665 nest = "" | 671 nest = "" |
666 level = 0 | 672 level = 0 |
667 secs = [] | 673 secs = [] |
674 | |
675 def getname(b): | |
676 x = b['lines'][0] | |
677 x = x.lower().strip('"') | |
678 if '(' in x: | |
679 x = x.split('(')[0] | |
680 return x | |
681 | |
668 for b in blocks: | 682 for b in blocks: |
669 if b['type'] == 'section': | 683 if b['type'] == 'section': |
670 i = b['underline'] | 684 i = b['underline'] |
671 if i not in nest: | 685 if i not in nest: |
672 nest += i | 686 nest += i |
673 level = nest.index(i) + 1 | 687 level = nest.index(i) + 1 |
674 nest = nest[:level] | 688 nest = nest[:level] |
675 secs.append((b['lines'][0], level, [b])) | 689 secs.append((getname(b), level, [b])) |
690 if b['type'] == 'definition': | |
691 i = ' ' | |
692 if i not in nest: | |
693 nest += i | |
694 level = nest.index(i) + 1 | |
695 nest = nest[:level] | |
696 secs.append((getname(b), level, [b])) | |
676 else: | 697 else: |
677 if not secs: | 698 if not secs: |
678 # add an initial empty section | 699 # add an initial empty section |
679 secs = [('', 0, [])] | 700 secs = [('', 0, [])] |
680 secs[-1][2].append(b) | 701 secs[-1][2].append(b) |