diff --git a/ods2md.py b/ods2md.py index b7dc83f..18dd95f 100755 --- a/ods2md.py +++ b/ods2md.py @@ -45,7 +45,7 @@ def display_text(cell): def display_len(s): return sum(DISPLAY_WIDTH[unicodedata.east_asian_width(c)] for c in s) -def main(odf_path): +def main(odf_path, out_file): ods = ezodf.opendoc(odf_path) for sheet in ods.sheets: @@ -53,7 +53,7 @@ def main(odf_path): if not any(column_widths): continue - print('##', sheet.name) + print('##', sheet.name, file=out_file) printed_header = False for row in sheet.rows(): @@ -61,22 +61,22 @@ def main(odf_path): if not any(contents): continue - print('|', end='') + print('|', end='', file=out_file) for m, content in enumerate(contents): column_width = column_widths[m] if not column_width: continue disp_len = column_width + len(content) - display_len(content) - print(' {0:<{1}}'.format(content, disp_len), end=' |') - print() + print(' {0:<{1}}'.format(content, disp_len), end=' |', file=out_file) + print(file=out_file) if not printed_header: printed_header = True - print('|', end='') + print('|', end='', file=out_file) for w in column_widths: if w: - print(':', '-' * (w+1), '|', sep='', end='') - print() + print(':', '-' * (w+1), '|', sep='', end='', file=out_file) + print(file=out_file) if __name__ == '__main__': - main(sys.argv[1]) + main(sys.argv[1], sys.stdout) diff --git a/test.py b/test.py new file mode 100644 index 0000000..64c96dd --- /dev/null +++ b/test.py @@ -0,0 +1,18 @@ +import ods2md + +import unittest +import os.path +import io + +class Test(unittest.TestCase): + def perform_test(self, base): + output = io.StringIO() + ods2md.main(os.path.join('test', base + '.ods'), output) + with open(os.path.join('test', base + '.md'), 'r') as f: + self.assertMultiLineEqual(f.read(), output.getvalue()) + + def test_sample(self): + self.perform_test('sample') + + def test_padded(self): + self.perform_test('padded') diff --git a/test/padded.md b/test/padded.md new file mode 100644 index 0000000..30c1a26 --- /dev/null +++ b/test/padded.md @@ -0,0 +1,8 @@ +## Sheet1 +| X | Y | +|:----|:-----------| +| 0.1 | 1999-12-31 | +| 0.2 | 2000-01-01 | +| | ! | +| 0.4 | | +| 0.7 | ? | diff --git a/test/padded.ods b/test/padded.ods new file mode 100644 index 0000000..d7e5c8f Binary files /dev/null and b/test/padded.ods differ diff --git a/test/sample.md b/test/sample.md new file mode 100644 index 0000000..eea0fd9 --- /dev/null +++ b/test/sample.md @@ -0,0 +1,12 @@ +## Sheet1 +| 版本 | 英文代號 | 中譯 | +|:-------------|:-----------------|:-------------------| +| Ubuntu 17.04 | Zesty Zapus | 熱情的北美草原跳鼠 | +| Ubuntu 16.10 | Yakkety Yak | 喋喋不休的氂牛 | +| Ubuntu 16.04 | Xenial Xerus | 好客的非洲地松鼠 | +| Ubuntu 15.10 | Wily Werewolf | 老謀深算的狼人 | +| Ubuntu 15.04 | Vivid Vervet | 活潑的長尾黑顎猴 | +| Ubuntu 14.10 | Utopic Unicorn | 烏托邦的獨角獸 | +| Ubuntu 14.04 | Trusty Tahr | 可靠的塔爾羊 | +| Ubuntu 13.10 | Saucy Salamander | 活潑的蠑螈 | +| Ubuntu 13.04 | Raring Ringtail | 卯足了勁的環尾貓熊 | diff --git a/test/sample.ods b/test/sample.ods new file mode 100644 index 0000000..ee4dcd0 Binary files /dev/null and b/test/sample.ods differ