From 465a59353628463db1a98ad84b7ecfb1240fcbca Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Thu, 13 Sep 2018 07:15:18 -0700 Subject: [PATCH] Add table support to rst2md --- support/rst2md.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/support/rst2md.py b/support/rst2md.py index 00ee93d9..59187ced 100644 --- a/support/rst2md.py +++ b/support/rst2md.py @@ -109,6 +109,30 @@ class Translator(nodes.NodeVisitor): def depart_image(self, node): pass + def write_row(self, row, widths): + for i, entry in enumerate(row): + text = entry[0][0] if len(entry) > 0 else '' + if i != 0: + self.write(' ') + self.write('{:{}}'.format(text, widths[i])) + + def visit_table(self, node): + table = node.children[0] + colspecs = table[:-2] + thead = table[-2] + tbody = table[-1] + widths = [int(cs['colwidth']) for cs in colspecs] + sep = ' '.join(['-' * w for w in widths]) + self.write(sep) + self.write_row(thead[0], widths) + self.write(sep) + for row in tbody: + self.write_row(row, widths) + self.write(sep) + raise nodes.StopTraversal + + def depart_table(self, node): + pass class MDWriter(writers.Writer): """GitHub-flavored markdown writer"""