summaryrefslogtreecommitdiff
path: root/docutils
diff options
context:
space:
mode:
authormilde <milde@929543f6-e4f2-0310-98a6-ba3bd3dd1d04>2012-01-11 20:28:57 +0000
committermilde <milde@929543f6-e4f2-0310-98a6-ba3bd3dd1d04>2012-01-11 20:28:57 +0000
commit443edfcdef56ea1c0f7384ee534f6b1a2d1aca28 (patch)
tree67dee799566e949b7b18ae0031d0a11144e3343d /docutils
parentd2d2cfca22da8596342d3d059fec6dab67abdc78 (diff)
downloaddocutils-443edfcdef56ea1c0f7384ee534f6b1a2d1aca28.tar.gz
Report table parsing errors with correct line number.
git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk@7313 929543f6-e4f2-0310-98a6-ba3bd3dd1d04
Diffstat (limited to 'docutils')
-rw-r--r--docutils/docutils/parsers/rst/states.py10
-rw-r--r--docutils/docutils/parsers/rst/tableparser.py37
-rwxr-xr-xdocutils/test/test_parsers/test_rst/test_SimpleTableParser.py8
-rwxr-xr-xdocutils/test/test_parsers/test_rst/test_TableParser.py8
-rwxr-xr-xdocutils/test/test_parsers/test_rst/test_east_asian_text.py4
-rwxr-xr-xdocutils/test/test_parsers/test_rst/test_tables.py16
6 files changed, 49 insertions, 34 deletions
diff --git a/docutils/docutils/parsers/rst/states.py b/docutils/docutils/parsers/rst/states.py
index 4d822a3e1..f2955a32d 100644
--- a/docutils/docutils/parsers/rst/states.py
+++ b/docutils/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/docutils/parsers/rst/tableparser.py b/docutils/docutils/parsers/rst/tableparser.py
index a8c6be355..63a6caf92 100644
--- a/docutils/docutils/parsers/rst/tableparser.py
+++ b/docutils/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/docutils/test/test_parsers/test_rst/test_SimpleTableParser.py b/docutils/test/test_parsers/test_rst/test_SimpleTableParser.py
index b755e33d9..61a808539 100755
--- a/docutils/test/test_parsers/test_rst/test_SimpleTableParser.py
+++ b/docutils/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/docutils/test/test_parsers/test_rst/test_TableParser.py b/docutils/test/test_parsers/test_rst/test_TableParser.py
index 861e241de..874efae56 100755
--- a/docutils/test/test_parsers/test_rst/test_TableParser.py
+++ b/docutils/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/docutils/test/test_parsers/test_rst/test_east_asian_text.py b/docutils/test/test_parsers/test_rst/test_east_asian_text.py
index 5cfb7c92c..953bfbaa7 100755
--- a/docutils/test/test_parsers/test_rst/test_east_asian_text.py
+++ b/docutils/test/test_parsers/test_rst/test_east_asian_text.py
@@ -149,10 +149,10 @@ u"""\
<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ラン
diff --git a/docutils/test/test_parsers/test_rst/test_tables.py b/docutils/test/test_parsers/test_rst/test_tables.py
index 3a8c1dfc3..1df34053b 100755
--- a/docutils/test/test_parsers/test_rst/test_tables.py
+++ b/docutils/test/test_parsers/test_rst/test_tables.py
@@ -896,10 +896,10 @@ cell 3 cell 4
""",
"""\
<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 @@ cell 3 cell 4
""",
"""\
<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 @@ A table with many row separators.
""",
"""\
<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 @@ A table with many row separators.
------------------------
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