From 53ae7026407ea591d6227dd78fb1cafc916be4ba Mon Sep 17 00:00:00 2001 From: milde Date: Wed, 11 Jan 2012 20:28:57 +0000 Subject: [PATCH] Report table parsing errors with correct line number. git-svn-id: https://docutils.svn.sourceforge.net/svnroot/docutils/trunk/docutils@7313 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/parsers/rst/states.py | 10 +++--- docutils/parsers/rst/tableparser.py | 37 +++++++++++++++------- .../test_rst/test_SimpleTableParser.py | 8 ++--- test/test_parsers/test_rst/test_TableParser.py | 8 ++--- test/test_parsers/test_rst/test_east_asian_text.py | 4 +-- test/test_parsers/test_rst/test_tables.py | 16 +++++----- 6 files changed, 49 insertions(+), 34 deletions(-) diff --git a/docutils/parsers/rst/states.py b/docutils/parsers/rst/states.py index 4d822a3e1..f2955a32d 100644 --- a/docutils/parsers/rst/states.py +++ b/docutils/parsers/rst/states.py @@ -1627,9 +1627,9 @@ class Body(RSTState): + 1) table = self.build_table(tabledata, tableline) nodelist = [table] + messages - except tableparser.TableMarkupError, detail: - nodelist = self.malformed_table( - block, ' '.join(detail.args)) + messages + except tableparser.TableMarkupError, err: + nodelist = self.malformed_table(block, ' '.join(err.args), + offset=err.offset) + messages else: nodelist = messages return nodelist, blank_finish @@ -1715,7 +1715,7 @@ class Body(RSTState): block.pad_double_width(self.double_width_pad_char) return block, [], end == limit or not lines[end+1].strip() - def malformed_table(self, block, detail=''): + def malformed_table(self, block, detail='', offset=0): block.replace(self.double_width_pad_char, '') data = '\n'.join(block) message = 'Malformed table.' @@ -1723,7 +1723,7 @@ class Body(RSTState): if detail: message += '\n' + detail error = self.reporter.error(message, nodes.literal_block(data, data), - line=startline) + line=startline+offset) return [error] def build_table(self, tabledata, tableline, stub_columns=0): diff --git a/docutils/parsers/rst/tableparser.py b/docutils/parsers/rst/tableparser.py index a8c6be355..63a6caf92 100644 --- a/docutils/parsers/rst/tableparser.py +++ b/docutils/parsers/rst/tableparser.py @@ -26,7 +26,18 @@ from docutils import DataError from docutils.utils import strip_combining_chars -class TableMarkupError(DataError): pass +class TableMarkupError(DataError): + + """ + Raise if there is any problem with table markup. + + The keyword argument `offset` denotes the offset of the problem + from the table's start line. + """ + + def __init__(self, *args, **kwargs): + self.offset = kwargs.pop('offset', 0) + DataError.__init__(self, *args) class TableParser: @@ -64,16 +75,17 @@ class TableParser: if self.head_body_separator_pat.match(line): if self.head_body_sep: raise TableMarkupError( - 'Multiple head/body row separators in table (at line ' - 'offset %s and %s); only one allowed.' - % (self.head_body_sep, i)) + 'Multiple head/body row separators ' + '(table lines %s and %s); only one allowed.' + % (self.head_body_sep+1, i+1), offset=i) else: self.head_body_sep = i self.block[i] = line.replace('=', '-') if self.head_body_sep == 0 or self.head_body_sep == (len(self.block) - 1): raise TableMarkupError('The head/body row separator may not be ' - 'the first or last line of the table.') + 'the first or last line of the table.', + offset=i) class GridTableParser(TableParser): @@ -425,8 +437,9 @@ class SimpleTableParser(TableParser): cols.append((begin, end)) if self.columns: if cols[-1][1] != self.border_end: - raise TableMarkupError('Column span incomplete at line ' - 'offset %s.' % offset) + raise TableMarkupError('Column span incomplete in table ' + 'line %s.' % (offset+1), + offset=offset) # Allow for an unbounded rightmost column: cols[-1] = (cols[-1][0], self.columns[-1][1]) return cols @@ -442,8 +455,9 @@ class SimpleTableParser(TableParser): i += 1 morecols += 1 except (AssertionError, IndexError): - raise TableMarkupError('Column span alignment problem at ' - 'line offset %s.' % (offset + 1)) + raise TableMarkupError('Column span alignment problem ' + 'in table line %s.' % (offset+2), + offset=offset+1) cells.append([0, morecols, offset, []]) i += 1 return cells @@ -502,8 +516,9 @@ class SimpleTableParser(TableParser): if new_end > main_end: self.columns[-1] = (main_start, new_end) elif line[end:nextstart].strip(): - raise TableMarkupError('Text in column margin at line ' - 'offset %s.' % (first_line + offset)) + raise TableMarkupError('Text in column margin ' + 'in table line %s.' % (first_line+offset+1), + offset=first_line+offset) offset += 1 columns.pop() diff --git a/test/test_parsers/test_rst/test_SimpleTableParser.py b/test/test_parsers/test_rst/test_SimpleTableParser.py index b755e33d9..61a808539 100755 --- a/test/test_parsers/test_rst/test_SimpleTableParser.py +++ b/test/test_parsers/test_rst/test_SimpleTableParser.py @@ -82,14 +82,14 @@ A bad table cell 2 cell 3 cell 4 ============ ====== """, -'TableMarkupError: Text in column margin at line offset 1.'], +'TableMarkupError: Text in column margin in table line 2.'], ["""\ ====== ===== ====== row one Another bad table ====== ===== ====== """, -'TableMarkupError: Text in column margin at line offset 2.'], +'TableMarkupError: Text in column margin in table line 3.'], ["""\ =========== ================ A table with two header rows, @@ -116,8 +116,8 @@ row separators. That's bad. ============ ============= """, -'TableMarkupError: Multiple head/body row separators in table ' -'(at line offset 2 and 4); only one allowed.'], +'TableMarkupError: Multiple head/body row separators ' +'(table lines 3 and 5); only one allowed.'], ["""\ ============ ============ ============ ============ diff --git a/test/test_parsers/test_rst/test_TableParser.py b/test/test_parsers/test_rst/test_TableParser.py index 861e241de..874efae56 100755 --- a/test/test_parsers/test_rst/test_TableParser.py +++ b/test/test_parsers/test_rst/test_TableParser.py @@ -197,10 +197,10 @@ totest['grid_tables'] = [ | That's bad. | | +-------------+-----------------+ """, -'TableMarkupError: Multiple head/body row separators in table ' -'(at line offset 2 and 4); only one allowed.', -'TableMarkupError: Multiple head/body row separators in table ' -'(at line offset 2 and 4); only one allowed.'], +'TableMarkupError: Multiple head/body row separators ' +'(table lines 3 and 5); only one allowed.', +'TableMarkupError: Multiple head/body row separators ' +'(table lines 3 and 5); only one allowed.'], ["""\ +-------------------------------------+ | | diff --git a/test/test_parsers/test_rst/test_east_asian_text.py b/test/test_parsers/test_rst/test_east_asian_text.py index 5cfb7c92c..953bfbaa7 100755 --- a/test/test_parsers/test_rst/test_east_asian_text.py +++ b/test/test_parsers/test_rst/test_east_asian_text.py @@ -149,10 +149,10 @@ u"""\ ダイ2ラン - + Malformed table. - Text in column margin at line offset 1. + Text in column margin in table line 2. ======== ========= ダイ1ラン ダイ2ラン diff --git a/test/test_parsers/test_rst/test_tables.py b/test/test_parsers/test_rst/test_tables.py index 3a8c1dfc3..1df34053b 100755 --- a/test/test_parsers/test_rst/test_tables.py +++ b/test/test_parsers/test_rst/test_tables.py @@ -896,10 +896,10 @@ cell 3 cell 4 """, """\ - + Malformed table. - Column span alignment problem at line offset 3. + Column span alignment problem in table line 4. ============== ====== A bad table cell 2 @@ -914,10 +914,10 @@ cell 3 cell 4 """, """\ - + Malformed table. - Text in column margin at line offset 1. + Text in column margin in table line 2. ======== ========= A bad table cell 2 @@ -1158,10 +1158,10 @@ A table with many row separators. """, """\ - + Malformed table. - Text in column margin at line offset 3. + Text in column margin in table line 4. == =========== =========== 1 Span columns 2 & 3 @@ -1170,10 +1170,10 @@ A table with many row separators. ------------------------ 3 == =========== =========== - + Malformed table. - Column span incomplete at line offset 4. + Column span incomplete in table line 5. == =========== =========== 1 Span cols 1&2 but not 3 -- 2.11.4.GIT