From: <mi...@us...> - 2012-01-11 20:29:03
|
Revision: 7313 http://docutils.svn.sourceforge.net/docutils/?rev=7313&view=rev Author: milde Date: 2012-01-11 20:28:57 +0000 (Wed, 11 Jan 2012) Log Message: ----------- Report table parsing errors with correct line number. Modified Paths: -------------- trunk/docutils/docutils/parsers/rst/states.py trunk/docutils/docutils/parsers/rst/tableparser.py trunk/docutils/test/test_parsers/test_rst/test_SimpleTableParser.py trunk/docutils/test/test_parsers/test_rst/test_TableParser.py trunk/docutils/test/test_parsers/test_rst/test_east_asian_text.py trunk/docutils/test/test_parsers/test_rst/test_tables.py Modified: trunk/docutils/docutils/parsers/rst/states.py =================================================================== --- trunk/docutils/docutils/parsers/rst/states.py 2012-01-10 02:36:13 UTC (rev 7312) +++ trunk/docutils/docutils/parsers/rst/states.py 2012-01-11 20:28:57 UTC (rev 7313) @@ -1627,9 +1627,9 @@ + 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 @@ 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 @@ 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): Modified: trunk/docutils/docutils/parsers/rst/tableparser.py =================================================================== --- trunk/docutils/docutils/parsers/rst/tableparser.py 2012-01-10 02:36:13 UTC (rev 7312) +++ trunk/docutils/docutils/parsers/rst/tableparser.py 2012-01-11 20:28:57 UTC (rev 7313) @@ -26,9 +26,20 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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() Modified: trunk/docutils/test/test_parsers/test_rst/test_SimpleTableParser.py =================================================================== --- trunk/docutils/test/test_parsers/test_rst/test_SimpleTableParser.py 2012-01-10 02:36:13 UTC (rev 7312) +++ trunk/docutils/test/test_parsers/test_rst/test_SimpleTableParser.py 2012-01-11 20:28:57 UTC (rev 7313) @@ -82,14 +82,14 @@ 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 @@ 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.'], ["""\ ============ ============ ============ ============ Modified: trunk/docutils/test/test_parsers/test_rst/test_TableParser.py =================================================================== --- trunk/docutils/test/test_parsers/test_rst/test_TableParser.py 2012-01-10 02:36:13 UTC (rev 7312) +++ trunk/docutils/test/test_parsers/test_rst/test_TableParser.py 2012-01-11 20:28:57 UTC (rev 7313) @@ -197,10 +197,10 @@ | 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.'], ["""\ +-------------------------------------+ | | Modified: trunk/docutils/test/test_parsers/test_rst/test_east_asian_text.py =================================================================== --- trunk/docutils/test/test_parsers/test_rst/test_east_asian_text.py 2012-01-10 02:36:13 UTC (rev 7312) +++ trunk/docutils/test/test_parsers/test_rst/test_east_asian_text.py 2012-01-11 20:28:57 UTC (rev 7313) @@ -149,10 +149,10 @@ <entry> <paragraph> ダイ2ラン - <system_message level="3" line="5" source="test data" type="ERROR"> + <system_message level="3" line="6" source="test data" type="ERROR"> <paragraph> Malformed table. - Text in column margin at line offset 1. + Text in column margin in table line 2. <literal_block xml:space="preserve"> ======== ========= ダイ1ラン ダイ2ラン Modified: trunk/docutils/test/test_parsers/test_rst/test_tables.py =================================================================== --- trunk/docutils/test/test_parsers/test_rst/test_tables.py 2012-01-10 02:36:13 UTC (rev 7312) +++ trunk/docutils/test/test_parsers/test_rst/test_tables.py 2012-01-11 20:28:57 UTC (rev 7313) @@ -896,10 +896,10 @@ """, """\ <document source="test data"> - <system_message level="3" line="1" source="test data" type="ERROR"> + <system_message level="3" line="4" source="test data" type="ERROR"> <paragraph> Malformed table. - Column span alignment problem at line offset 3. + Column span alignment problem in table line 4. <literal_block xml:space="preserve"> ============== ====== A bad table cell 2 @@ -914,10 +914,10 @@ """, """\ <document source="test data"> - <system_message level="3" line="1" source="test data" type="ERROR"> + <system_message level="3" line="2" source="test data" type="ERROR"> <paragraph> Malformed table. - Text in column margin at line offset 1. + Text in column margin in table line 2. <literal_block xml:space="preserve"> ======== ========= A bad table cell 2 @@ -1158,10 +1158,10 @@ """, """\ <document source="test data"> - <system_message level="3" line="1" source="test data" type="ERROR"> + <system_message level="3" line="4" source="test data" type="ERROR"> <paragraph> Malformed table. - Text in column margin at line offset 3. + Text in column margin in table line 4. <literal_block xml:space="preserve"> == =========== =========== 1 Span columns 2 & 3 @@ -1170,10 +1170,10 @@ ------------------------ 3 == =========== =========== - <system_message level="3" line="9" source="test data" type="ERROR"> + <system_message level="3" line="13" source="test data" type="ERROR"> <paragraph> Malformed table. - Column span incomplete at line offset 4. + Column span incomplete in table line 5. <literal_block xml:space="preserve"> == =========== =========== 1 Span cols 1&2 but not 3 This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |