Commits

Takayuki Shimizukawa committed c8561ef

Fix text builder did not respect wide/fullwidth charactors for table layout.

  • Participants
  • Parent commits cd09513

Comments (0)

Files changed (2)

File sphinx/writers/text.py

                 for i, cell in enumerate(line):
                     par = my_wrap(cell, width=colwidths[i])
                     if par:
-                        maxwidth = max(map(len, par))
+                        maxwidth = max(map(column_width, par))
                     else:
                         maxwidth = 0
                     realwidths[i] = max(realwidths[i], maxwidth)
                 out = ['|']
                 for i, cell in enumerate(line):
                     if cell:
-                        out.append(' ' + cell.ljust(realwidths[i]+1))
+                        adjust_len = len(cell) - column_width(cell)
+                        out.append(' ' + cell.ljust(
+                            realwidths[i] + 1 + adjust_len))
                     else:
                         out.append(' ' * (realwidths[i] + 2))
                     out.append('|')

File tests/test_build_text.py

     expect_underline = underline.replace('=', '*')
     result_underline = result.splitlines()[2].strip()
     assert expect_underline == result_underline
+
+
+@with_text_app()
+def test_multibyte_table(app):
+    text = u'\u65e5\u672c\u8a9e'
+    contents = (u"\n.. list-table::"
+                 "\n"
+                 "\n   - - spam"
+                 "\n     - egg"
+                 "\n"
+                 "\n   - - %(text)s"
+                 "\n     - %(text)s"
+                 "\n" % locals())
+
+    (app.srcdir / 'contents.rst').write_text(contents, encoding='utf-8')
+    app.builder.build_all()
+    result = (app.outdir / 'contents.txt').text(encoding='utf-8')
+
+    lines = [line.strip() for line in result.splitlines() if line.strip()]
+    line_widths = [column_width(line) for line in lines]
+    assert len(set(line_widths)) == 1  # same widths