@@ -454,14 +454,25 @@ def row_is_all_th(row):
454
454
while body_rows and row_is_all_th (body_rows [0 ]):
455
455
header_rows .append (body_rows .pop (0 ))
456
456
457
- header = self ._expand_colspan_rowspan (header_rows , section = "header" )
458
- body = self ._expand_colspan_rowspan (body_rows , section = "body" )
459
- footer = self ._expand_colspan_rowspan (footer_rows , section = "footer" )
457
+ header , rem = self ._expand_colspan_rowspan (header_rows , section = "header" )
458
+ body , rem = self ._expand_colspan_rowspan (
459
+ body_rows ,
460
+ section = "body" ,
461
+ remainder = rem ,
462
+ overflow = True if len (footer_rows ) > 0 else False ,
463
+ )
464
+ footer , _ = self ._expand_colspan_rowspan (
465
+ footer_rows , section = "footer" , remainder = rem , overflow = False
466
+ )
460
467
461
468
return header , body , footer
462
469
463
470
def _expand_colspan_rowspan (
464
- self , rows , section : Literal ["header" , "footer" , "body" ]
471
+ self ,
472
+ rows ,
473
+ section : Literal ["header" , "footer" , "body" ],
474
+ remainder : list [int , tuple [str | tuple , int ]] | None = None ,
475
+ overflow : bool = True ,
465
476
) -> list [list ]:
466
477
"""
467
478
Given a list of <tr>s, return a list of text rows.
@@ -471,6 +482,11 @@ def _expand_colspan_rowspan(
471
482
rows : list of node-like
472
483
List of <tr>s
473
484
section : the section that the rows belong to (header, body or footer).
485
+ remainder: list[int, tuple[str | tuple, int]] | None
486
+ Any remainder from the expansion of previous section
487
+ overflow: bool
488
+ If true, return any partial rows as 'remainder'. If not, use up any
489
+ partial rows. True by default.
474
490
475
491
Returns
476
492
-------
@@ -485,9 +501,7 @@ def _expand_colspan_rowspan(
485
501
"""
486
502
all_texts = [] # list of rows, each a list of str
487
503
text : str | tuple
488
- remainder : list [
489
- tuple [int , str | tuple , int ]
490
- ] = [] # list of (index, text, nrows)
504
+ remainder = remainder if remainder is not None else []
491
505
492
506
for tr in rows :
493
507
texts = [] # the output for this row
@@ -528,19 +542,20 @@ def _expand_colspan_rowspan(
528
542
all_texts .append (texts )
529
543
remainder = next_remainder
530
544
531
- # Append rows that only appear because the previous row had non-1
532
- # rowspan
533
- while remainder :
534
- next_remainder = []
535
- texts = []
536
- for prev_i , prev_text , prev_rowspan in remainder :
537
- texts .append (prev_text )
538
- if prev_rowspan > 1 :
539
- next_remainder .append ((prev_i , prev_text , prev_rowspan - 1 ))
540
- all_texts .append (texts )
541
- remainder = next_remainder
545
+ if not overflow :
546
+ # Append rows that only appear because the previous row had non-1
547
+ # rowspan
548
+ while remainder :
549
+ next_remainder = []
550
+ texts = []
551
+ for prev_i , prev_text , prev_rowspan in remainder :
552
+ texts .append (prev_text )
553
+ if prev_rowspan > 1 :
554
+ next_remainder .append ((prev_i , prev_text , prev_rowspan - 1 ))
555
+ all_texts .append (texts )
556
+ remainder = next_remainder
542
557
543
- return all_texts
558
+ return all_texts , remainder
544
559
545
560
def _handle_hidden_tables (self , tbl_list , attr_name : str ):
546
561
"""
0 commit comments