# HG changeset patch # User Matt Mackall # Date 1313118357 18000 # Node ID df47381b41d67927c089825ff4af17423e569cfd # Parent bb96e12a3242f3cc4ebdc01ea6eabf741d4b0630 minirst: add simple table support This adds a subset of the 'simple table' support from RST to allow formatting of options lists through RST. Table columns are automatically sized based on contents, with line wrapping in the last column. diff -r bb96e12a3242 -r df47381b41d6 mercurial/commands.py --- a/mercurial/commands.py Thu Aug 11 22:05:31 2011 -0500 +++ b/mercurial/commands.py Thu Aug 11 22:05:57 2011 -0500 @@ -3950,13 +3950,16 @@ file states (columns) and option combinations (rows). The file states are Added [A], Clean [C], Modified [M] and Missing [!] (as reported by :hg:`status`). The actions are Warn, Remove (from - branch) and Delete (from disk):: - - A C M ! - none W RD W R - -f R RD RD R - -A W W W R - -Af R R R R + branch) and Delete (from disk): + + ======= == == == == + A C M ! + ======= == == == == + none W RD W R + -f R RD RD R + -A W W W R + -Af R R R R + ======= == == == == Note that remove never deletes files in Added [A] state from the working directory, not even if option --force is specified. diff -r bb96e12a3242 -r df47381b41d6 mercurial/minirst.py --- a/mercurial/minirst.py Thu Aug 11 22:05:31 2011 -0500 +++ b/mercurial/minirst.py Thu Aug 11 22:05:57 2011 -0500 @@ -103,6 +103,7 @@ r'((.*) +)(.*)$') _fieldre = re.compile(r':(?![: ])([^:]*)(? 4 and + _tablere.match(block['lines'][0]) and + block['lines'][0] == block['lines'][-1]): + block['type'] = 'table' + block['header'] = False + div = block['lines'][0] + columns = [x for x in xrange(len(div)) + if div[x] == '=' and (x == 0 or div[x - 1] == ' ')] + rows = [] + for l in block['lines'][1:-1]: + if l == div: + block['header'] = True + continue + row = [] + for n, start in enumerate(columns): + if n + 1 < len(columns): + row.append(l[start:columns[n + 1]].strip()) + else: + row.append(l[start:].strip()) + rows.append(row) + block['table'] = rows + + return blocks + def findsections(blocks): """Finds sections. @@ -392,6 +433,24 @@ if block['type'] == 'section': underline = encoding.colwidth(block['lines'][0]) * block['underline'] return "%s%s\n%s%s" % (indent, block['lines'][0],indent, underline) + if block['type'] == 'table': + table = block['table'] + # compute column widths + widths = [max([encoding.colwidth(e) for e in c]) for c in zip(*table)] + text = '' + span = sum(widths) + len(widths) - 1 + indent = ' ' * block['indent'] + hang = ' ' * (len(indent) + span - widths[-1]) + f = ' '.join('%%-%ds' % n for n in widths) + + for row in table: + l = f % tuple(row) + l = util.wrap(l, width=width, initindent=indent, hangindent=hang) + if not text and block['header']: + text = l + '\n' + indent + '-' * (min(width, span)) + '\n' + else: + text += l + "\n" + return text if block['type'] == 'definition': term = indent + block['lines'][0] hang = len(block['lines'][-1]) - len(block['lines'][-1].lstrip()) @@ -440,6 +499,7 @@ for b in blocks: b['indent'] += indent blocks = findliteralblocks(blocks) + blocks = findtables(blocks) blocks, pruned = prunecontainers(blocks, keep or []) blocks = findsections(blocks) blocks = inlineliterals(blocks) diff -r bb96e12a3242 -r df47381b41d6 tests/test-minirst.py --- a/tests/test-minirst.py Thu Aug 11 22:05:31 2011 -0500 +++ b/tests/test-minirst.py Thu Aug 11 22:05:57 2011 -0500 @@ -231,3 +231,15 @@ """ debugformat('comments', comments, 30) + +table = """ + === === === + a b c + === === === + 1 2 3 + foo bar baz + aa bb sdfsdfsdf this line is way too long for this cell. + === === === +""" + +debugformat('table', table, 30) diff -r bb96e12a3242 -r df47381b41d6 tests/test-minirst.py.out --- a/tests/test-minirst.py.out Thu Aug 11 22:05:31 2011 -0500 +++ b/tests/test-minirst.py.out Thu Aug 11 22:05:57 2011 -0500 @@ -388,3 +388,15 @@ Empty comment above ---------------------------------------------------------------------- +table formatted to fit within 30 characters: +---------------------------------------------------------------------- + a b c + ------------------------------ + 1 2 3 + foo bar baz + aa bb sdfsdfsdf this line + is way too long for + this cell. + +---------------------------------------------------------------------- +