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

Simplify and fix Table.set_span #165

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
56 changes: 14 additions & 42 deletions camelot/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,10 +357,6 @@ class Cell:
Whether or not cell is bounded on the top.
bottom : bool
Whether or not cell is bounded on the bottom.
hspan : bool
Whether or not cell spans horizontally.
vspan : bool
Whether or not cell spans vertically.
text : string
Text assigned to cell.

Expand All @@ -379,8 +375,6 @@ def __init__(self, x1, y1, x2, y2):
self.right = False
self.top = False
self.bottom = False
self.hspan = False
self.vspan = False
self._text = ""

def __repr__(self): # noqa D105
Expand All @@ -398,6 +392,16 @@ def text(self): # noqa D102
def text(self, t): # noqa D105
self._text = "".join([self._text, t])

@property
def hspan(self) -> bool:
"""Whether or not cell spans horizontally."""
return not self.left or not self.right

@property
def vspan(self) -> bool:
"""Whether or not cell spans vertically."""
return not self.top or not self.bottom

@property
def bound(self):
"""The number of sides on which the cell is bounded."""
Expand Down Expand Up @@ -621,39 +625,6 @@ def set_border(self):
self.cells[len(self.rows) - 1][index].bottom = True
return self

def set_span(self):
"""Set a cell's hspan or vspan attribute.

Set the cell's hspan or vspan attribute to True depending
on whether the cell spans horizontally or vertically.
"""
for row in self.cells:
for cell in row:
left = cell.left
right = cell.right
top = cell.top
bottom = cell.bottom
if cell.bound == 4:
continue
elif cell.bound == 3:
if not left and (right and top and bottom):
cell.hspan = True
elif not right and (left and top and bottom):
cell.hspan = True
elif not top and (left and right and bottom):
cell.vspan = True
elif not bottom and (left and right and top):
cell.vspan = True
elif cell.bound == 2:
if left and right and (not top and not bottom):
cell.vspan = True
elif top and bottom and (not left and not right):
cell.hspan = True
elif cell.bound in [0, 1]:
cell.vspan = True
cell.hspan = True
return self

def copy_spanning_text(self, copy_text=None):
"""Copies over text in empty spanning cells.

Expand All @@ -667,20 +638,21 @@ def copy_spanning_text(self, copy_text=None):

Returns
-------
t : camelot.core.Table
table : camelot.core.Table

"""
for f in copy_text:
if f == "h":
for i in range(len(self.cells)):
for j in range(len(self.cells[i])):
if self.cells[i][j].text.strip() == "":
if self.cells[i][j].hspan and not self.cells[i][j].left:
if not self.cells[i][j].left:
self.cells[i][j].text = self.cells[i][j - 1].text
elif f == "v":
for i in range(len(self.cells)):
for j in range(len(self.cells[i])):
if self.cells[i][j].text.strip() == "":
if self.cells[i][j].vspan and not self.cells[i][j].top:
if not self.cells[i][j].top:
self.cells[i][j].text = self.cells[i - 1][j].text
return self

Expand Down
10 changes: 4 additions & 6 deletions camelot/parsers/lattice.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,16 @@ def _reduce_index(table, idx, shift_text):
indices = []
for r_idx, c_idx, text in idx:
for d in shift_text:
if d == "l" and table.cells[r_idx][c_idx].hspan:
if d == "l":
while not table.cells[r_idx][c_idx].left:
c_idx -= 1
if d == "r" and table.cells[r_idx][c_idx].hspan:
elif d == "r":
while not table.cells[r_idx][c_idx].right:
c_idx += 1
if d == "t" and table.cells[r_idx][c_idx].vspan:
elif d == "t":
while not table.cells[r_idx][c_idx].top:
r_idx -= 1
if d == "b" and table.cells[r_idx][c_idx].vspan:
elif d == "b":
while not table.cells[r_idx][c_idx].bottom:
r_idx += 1
indices.append((r_idx, c_idx, text))
Expand Down Expand Up @@ -337,8 +337,6 @@ def _generate_table(self, table_idx, bbox, cols, rows, **kwargs):
table = table.set_edges(v_s, h_s, joint_tol=self.joint_tol)
# set table border edges to True
table = table.set_border()
# set spanning cells to True
table = table.set_span()

self.record_parse_metadata(table)
return table