Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Handle unblance table errors #801

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions tests/valid/indexes.pages.text
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@


Network Working Group H. Person, Ed.
Internet-Draft June 20, 2022
Internet-Draft June 22, 2022
Intended status: Experimental
Expires: December 22, 2022
Expires: December 24, 2022


xml2rfc index tests
Expand All @@ -26,7 +26,7 @@ Status of This Memo
time. It is inappropriate to use Internet-Drafts as reference
material or to cite them other than as "work in progress."

This Internet-Draft will expire on December 22, 2022.
This Internet-Draft will expire on December 24, 2022.

Copyright Notice

Expand All @@ -53,7 +53,7 @@ Table of Contents



Person Expires December 22, 2022 [Page 1]
Person Expires December 24, 2022 [Page 1]

Internet-Draft xml2rfc index tests June 2022

Expand Down Expand Up @@ -109,7 +109,7 @@ Index



Person Expires December 22, 2022 [Page 2]
Person Expires December 24, 2022 [Page 2]

Internet-Draft xml2rfc index tests June 2022

Expand Down Expand Up @@ -165,4 +165,4 @@ Author's Address



Person Expires December 22, 2022 [Page 3]
Person Expires December 24, 2022 [Page 3]
6 changes: 3 additions & 3 deletions tests/valid/indexes.prepped.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version='1.0' encoding='utf-8'?>
<rfc xmlns:xi="http://www.w3.org/2001/XInclude" version="3" category="exp" docName="indexes-00" indexInclude="true" ipr="trust200902" prepTime="2022-06-20T23:09:07" scripts="Common,Latin" sortRefs="true" submissionType="independent" symRefs="true" tocDepth="3" tocInclude="true">
<rfc xmlns:xi="http://www.w3.org/2001/XInclude" version="3" category="exp" docName="indexes-00" indexInclude="true" ipr="trust200902" prepTime="2022-06-22T06:59:07" scripts="Common,Latin" sortRefs="true" submissionType="independent" symRefs="true" tocDepth="3" tocInclude="true">
<!-- xml2rfc v2v3 conversion 3.12.10 -->


Expand All @@ -20,7 +20,7 @@
</postal>
</address>
</author>
<date day="20" month="06" year="2022"/>
<date day="22" month="06" year="2022"/>
<boilerplate>
<section anchor="status-of-memo" numbered="false" removeInRFC="false" toc="exclude" pn="section-boilerplate.1">
<name slugifiedName="name-status-of-this-memo">Status of This Memo</name>
Expand All @@ -41,7 +41,7 @@
material or to cite them other than as "work in progress."
</t>
<t indent="0" pn="section-boilerplate.1-4">
This Internet-Draft will expire on 22 December 2022.
This Internet-Draft will expire on 24 December 2022.
</t>
</section>
<section anchor="copyright" numbered="false" removeInRFC="false" toc="exclude" pn="section-boilerplate.2">
Expand Down
6 changes: 3 additions & 3 deletions tests/valid/indexes.text
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@


Network Working Group H. Person, Ed.
Internet-Draft June 20, 2022
Internet-Draft June 22, 2022
Intended status: Experimental
Expires: December 22, 2022
Expires: December 24, 2022


xml2rfc index tests
Expand All @@ -26,7 +26,7 @@ Status of This Memo
time. It is inappropriate to use Internet-Drafts as reference
material or to cite them other than as "work in progress."

This Internet-Draft will expire on December 22, 2022.
This Internet-Draft will expire on December 24, 2022.

Copyright Notice

Expand Down
8 changes: 4 additions & 4 deletions tests/valid/indexes.v3.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</tr></thead>
<tfoot><tr>
<td class="left">Person</td>
<td class="center">Expires December 22, 2022</td>
<td class="center">Expires December 24, 2022</td>
<td class="right">[Page]</td>
</tr></tfoot>
</table>
Expand All @@ -36,12 +36,12 @@
<dd class="internet-draft">indexes-00</dd>
<dt class="label-published">Published:</dt>
<dd class="published">
<time datetime="2022-06-20" class="published">June 20, 2022</time>
<time datetime="2022-06-22" class="published">June 22, 2022</time>
</dd>
<dt class="label-intended-status">Intended Status:</dt>
<dd class="intended-status">Experimental</dd>
<dt class="label-expires">Expires:</dt>
<dd class="expires"><time datetime="2022-12-22">December 22, 2022</time></dd>
<dd class="expires"><time datetime="2022-12-24">December 24, 2022</time></dd>
<dt class="label-authors">Author:</dt>
<dd class="authors">
<div class="author">
Expand Down Expand Up @@ -71,7 +71,7 @@ <h2 id="name-status-of-this-memo">
time. It is inappropriate to use Internet-Drafts as reference
material or to cite them other than as "work in progress."<a href="#section-boilerplate.1-3" class="pilcrow">¶</a></p>
<p id="section-boilerplate.1-4">
This Internet-Draft will expire on December 22, 2022.<a href="#section-boilerplate.1-4" class="pilcrow">¶</a></p>
This Internet-Draft will expire on December 24, 2022.<a href="#section-boilerplate.1-4" class="pilcrow">¶</a></p>
</section>
</div>
<div id="copyright">
Expand Down
2 changes: 1 addition & 1 deletion tests/valid/manpage.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
xml2rfc(1) xml2rfc(1)
20 June 2022
22 June 2022


Xml2rfc Vocabulary Version 3 Schema
Expand Down
14 changes: 7 additions & 7 deletions tests/valid/sourcecode.pages.text
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@


Network Working Group H. Person, Ed.
Internet-Draft June 20, 2022
Internet-Draft June 22, 2022
Intended status: Experimental
Expires: December 22, 2022
Expires: December 24, 2022


xml2rfc sourcecode tests
Expand All @@ -26,7 +26,7 @@ Status of This Memo
time. It is inappropriate to use Internet-Drafts as reference
material or to cite them other than as "work in progress."

This Internet-Draft will expire on December 22, 2022.
This Internet-Draft will expire on December 24, 2022.

Copyright Notice

Expand All @@ -53,7 +53,7 @@ Table of Contents



Person Expires December 22, 2022 [Page 1]
Person Expires December 24, 2022 [Page 1]

Internet-Draft xml2rfc sourcecode tests June 2022

Expand Down Expand Up @@ -109,7 +109,7 @@ Internet-Draft xml2rfc sourcecode tests June 2022



Person Expires December 22, 2022 [Page 2]
Person Expires December 24, 2022 [Page 2]

Internet-Draft xml2rfc sourcecode tests June 2022

Expand Down Expand Up @@ -165,7 +165,7 @@ Internet-Draft xml2rfc sourcecode tests June 2022



Person Expires December 22, 2022 [Page 3]
Person Expires December 24, 2022 [Page 3]

Internet-Draft xml2rfc sourcecode tests June 2022

Expand Down Expand Up @@ -221,4 +221,4 @@ Author's Address



Person Expires December 22, 2022 [Page 4]
Person Expires December 24, 2022 [Page 4]
6 changes: 3 additions & 3 deletions tests/valid/sourcecode.prepped.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version='1.0' encoding='utf-8'?>
<rfc xmlns:xi="http://www.w3.org/2001/XInclude" version="3" category="exp" docName="sourcecode-00" indexInclude="true" ipr="trust200902" prepTime="2022-06-20T23:09:15" scripts="Common,Latin" sortRefs="true" submissionType="independent" symRefs="true" tocDepth="3" tocInclude="true">
<rfc xmlns:xi="http://www.w3.org/2001/XInclude" version="3" category="exp" docName="sourcecode-00" indexInclude="true" ipr="trust200902" prepTime="2022-06-22T06:59:15" scripts="Common,Latin" sortRefs="true" submissionType="independent" symRefs="true" tocDepth="3" tocInclude="true">
<!-- xml2rfc v2v3 conversion 3.12.10 -->


Expand All @@ -20,7 +20,7 @@
</postal>
</address>
</author>
<date day="20" month="06" year="2022"/>
<date day="22" month="06" year="2022"/>
<boilerplate>
<section anchor="status-of-memo" numbered="false" removeInRFC="false" toc="exclude" pn="section-boilerplate.1">
<name slugifiedName="name-status-of-this-memo">Status of This Memo</name>
Expand All @@ -41,7 +41,7 @@
material or to cite them other than as "work in progress."
</t>
<t indent="0" pn="section-boilerplate.1-4">
This Internet-Draft will expire on 22 December 2022.
This Internet-Draft will expire on 24 December 2022.
</t>
</section>
<section anchor="copyright" numbered="false" removeInRFC="false" toc="exclude" pn="section-boilerplate.2">
Expand Down
6 changes: 3 additions & 3 deletions tests/valid/sourcecode.text
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@


Network Working Group H. Person, Ed.
Internet-Draft June 20, 2022
Internet-Draft June 22, 2022
Intended status: Experimental
Expires: December 22, 2022
Expires: December 24, 2022


xml2rfc sourcecode tests
Expand All @@ -26,7 +26,7 @@ Status of This Memo
time. It is inappropriate to use Internet-Drafts as reference
material or to cite them other than as "work in progress."

This Internet-Draft will expire on December 22, 2022.
This Internet-Draft will expire on December 24, 2022.

Copyright Notice

Expand Down
8 changes: 4 additions & 4 deletions tests/valid/sourcecode.v3.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</tr></thead>
<tfoot><tr>
<td class="left">Person</td>
<td class="center">Expires December 22, 2022</td>
<td class="center">Expires December 24, 2022</td>
<td class="right">[Page]</td>
</tr></tfoot>
</table>
Expand All @@ -36,12 +36,12 @@
<dd class="internet-draft">sourcecode-00</dd>
<dt class="label-published">Published:</dt>
<dd class="published">
<time datetime="2022-06-20" class="published">June 20, 2022</time>
<time datetime="2022-06-22" class="published">June 22, 2022</time>
</dd>
<dt class="label-intended-status">Intended Status:</dt>
<dd class="intended-status">Experimental</dd>
<dt class="label-expires">Expires:</dt>
<dd class="expires"><time datetime="2022-12-22">December 22, 2022</time></dd>
<dd class="expires"><time datetime="2022-12-24">December 24, 2022</time></dd>
<dt class="label-authors">Author:</dt>
<dd class="authors">
<div class="author">
Expand Down Expand Up @@ -71,7 +71,7 @@ <h2 id="name-status-of-this-memo">
time. It is inappropriate to use Internet-Drafts as reference
material or to cite them other than as "work in progress."<a href="#section-boilerplate.1-3" class="pilcrow">¶</a></p>
<p id="section-boilerplate.1-4">
This Internet-Draft will expire on December 22, 2022.<a href="#section-boilerplate.1-4" class="pilcrow">¶</a></p>
This Internet-Draft will expire on December 24, 2022.<a href="#section-boilerplate.1-4" class="pilcrow">¶</a></p>
</section>
</div>
<div id="copyright">
Expand Down
78 changes: 48 additions & 30 deletions xml2rfc/writers/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -4137,52 +4137,67 @@ def set_colwidths(cells, rows, cols):
minspan = sys.maxsize
for j in range(cols):
cell = cells[i][j]
k, l = cell.origin
hspan = cell.rowspan+k-i if cell.rowspan else minspan
if hspan > 0 and hspan < minspan:
minspan = hspan
if hasattr(cell, 'origin'):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you may be better off with a single check_cell_has_origin(cell) that dies on error and gets called in all these places.

Clearly, the need for this is a code smell.

See also #740 why this is solving the crash, but not all of the problem.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree.
Might be worth looking to simplify the text table generation function.

k, l = cell.origin
hspan = cell.rowspan+k-i if cell.rowspan else minspan
if hspan > 0 and hspan < minspan:
minspan = hspan
else:
self.die(e, "Inconsistent table width: Found different row lengths in this table")
maxlines = 0
for j in range(cols):
cell = cells[i][j]
k, l = cell.origin
hspan = cell.rowspan+k-i if cell.rowspan else minspan
lines = len(cell.wrapped) if cell.wrapped else 0
if hspan == minspan and lines > maxlines:
maxlines = lines
if hasattr(cell, 'origin'):
k, l = cell.origin
hspan = cell.rowspan+k-i if cell.rowspan else minspan
lines = len(cell.wrapped) if cell.wrapped else 0
if hspan == minspan and lines > maxlines:
maxlines = lines
else:
self.die(e, "Inconsistent table width: Found different row lengths in this table")
for j in range(cols):
cells[i][j].lines = maxlines

# ----------------------------------------------------------------------
# Calculate total height for rowspan cells
for i in range(rows):
for j in range(cols):
cells[i][j].m = None
cells[i][j].height = None
k, l = cells[i][j].origin
cell = cells[k][l]
if cell.m is None:
cell.m = 0
cell.height = sum([ cells[n][l].lines for n in range(k, k+cell.rowspan)]) + cell.rowspan-1
if hasattr(cells[i][j], 'origin'):
cells[i][j].m = None
cells[i][j].height = None
k, l = cells[i][j].origin
cell = cells[k][l]
if cell.m is None:
cell.m = 0
cell.height = sum([ cells[n][l].lines for n in range(k, k+cell.rowspan)]) + cell.rowspan-1
else:
self.die(e, "Inconsistent table width: Found different row lengths in this table")

# ----------------------------------------------------------------------
# Calculate total width for colspan cells
for i in range(rows):
for j in range(cols):
k, l = cells[i][j].origin
cell = cells[k][l]
if cell.origin == (i,j):
cell.colwidth = sum([ cells[i][n].colwidth for n in range(j, j+cell.colspan)]) + cell.colspan-1
if hasattr(cells[i][j], 'origin'):
k, l = cells[i][j].origin
cell = cells[k][l]
if cell.origin == (i,j):
cell.colwidth = sum([ cells[i][n].colwidth for n in range(j, j+cell.colspan)]) + cell.colspan-1
else:
self.die(e, "Inconsistent table width: Found different row lengths in this table")

# ----------------------------------------------------------------------
# Calculate minimum padding per table column
minpad = [width,]*cols
for i in range(rows):
for j in range(cols):
cell = cells[i][j]
if cell.origin == (i, j):
padding = min([width] + [(cell.colwidth - textwidth(line)) for line in cell.wrapped])
if padding < minpad[j]:
minpad[j] = padding
if hasattr(cell, 'origin'):
if cell.origin == (i, j):
padding = min([width] + [(cell.colwidth - textwidth(line)) for line in cell.wrapped])
if padding < minpad[j]:
minpad[j] = padding
else:
self.die(e, "Inconsistent table width: Found different row lengths in this table")

# ----------------------------------------------------------------------
# Add cell borders
Expand All @@ -4191,12 +4206,15 @@ def set_colwidths(cells, rows, cols):
for i in range(rows):
for j in range(cols):
cell = cells[i][j]
if cell.origin == (i, j):
wrapped = (cell.wrapped + ['']*cell.height)[:cell.height]
lines = ( ([ x + cell.top*cell.colwidth + x ] if cell.top else [])
+ ([ l + justify(cell, line, minpad[j]) + l for line in wrapped ])
+ ([ x + cell.bot*cell.colwidth + x ] if cell.bot else []) )
cell.wrapped = lines
if hasattr(cell, 'origin'):
if cell.origin == (i, j):
wrapped = (cell.wrapped + ['']*cell.height)[:cell.height]
lines = ( ([ x + cell.top*cell.colwidth + x ] if cell.top else [])
+ ([ l + justify(cell, line, minpad[j]) + l for line in wrapped ])
+ ([ x + cell.bot*cell.colwidth + x ] if cell.bot else []) )
cell.wrapped = lines
else:
self.die(e, "Inconsistent table width: Found different row lengths in this table")

#show(cells, 'lines', 'before assembly')
# ----------------------------------------------------------------------
Expand Down