Skip to content

Commit 06f0c21

Browse files
authored
Merge branch 'refactor-render-strategies' into refactor-render-strategies
2 parents d8b9cba + 98e6416 commit 06f0c21

File tree

6 files changed

+20
-24
lines changed

6 files changed

+20
-24
lines changed

src/rtflite/encoding/renderer.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def render(self, document: Any, page: PageContext) -> list[str]:
9393
)
9494
):
9595
col_idx = 0
96-
# Try to find column index for legacy reasons, though likely unused if we handle spanning cleanly
96+
# Find col index for legacy reasons; likely unused with clean spanning.
9797
if document.rtf_body.page_by and isinstance(document.df, pl.DataFrame):
9898
try:
9999
col_idx = document.df.columns.index(document.rtf_body.page_by[0])
@@ -223,15 +223,15 @@ def _render_column_headers(self, document: Any, page: PageContext) -> list[str]:
223223

224224
# Adjust col_rel_width if needed (logic from PaginatedStrategy)
225225
# Since we are using page.data which is already sliced/processed,
226-
# we might need to adjust widths if they were originally defined for full table
226+
# Might need to adjust widths if defined for full table.
227227
if document.rtf_body.col_rel_width is not None:
228228
# If body has specific widths, try to map them.
229-
# For simplicity in this refactor, if we have header text now, we proceed.
229+
# If header text exists, proceed.
230230
pass
231231

232232
# Remove columns if necessary (page_by/subline_by)
233-
# Note: If we just populated from page.data, page.data already has columns removed!
234-
# So we only need to filter if text came from original document and has extra columns.
233+
# Note: page.data already has columns removed if populated from it.
234+
# Filter only if text is from original document with extra columns.
235235
if isinstance(header_copy.text, pl.DataFrame):
236236
cols_remove = set()
237237
if document.rtf_body.page_by:
@@ -299,7 +299,7 @@ def _render_body(self, document: Any, page: PageContext) -> list[str]:
299299

300300
if page_rel_row > prev_row:
301301
segment = page_df[prev_row:page_rel_row]
302-
# We must use internal _encode method of TableAttributes (attrs are already finalized)
302+
# Use internal _encode method (attributes already finalized).
303303
# Note: We need to ensure page_attrs is the TableAttributes object
304304
elements.extend(
305305
page_attrs._encode(segment, col_widths, row_offset=prev_row)

src/rtflite/encoding/unified_encoder.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
from typing import TYPE_CHECKING, Any
1+
from __future__ import annotations
22

3-
from ..attributes import BroadcastValue
3+
from typing import Any
44

5-
if TYPE_CHECKING:
6-
from ..encode import RTFDocument
5+
from rtflite import RTFDocument
76

7+
from ..attributes import BroadcastValue
88
from ..pagination.processor import PageFeatureProcessor
99
from ..pagination.strategies import PageContext, PaginationContext, StrategyRegistry
1010
from ..pagination.strategies.defaults import DefaultPaginationStrategy
@@ -42,7 +42,7 @@ def encode(self, document: Any) -> str:
4242
# 1. Figure-only handling
4343
if document.df is None:
4444
# Reuse the logic from previous implementation, adapted slightly
45-
# For now, we can't easily unify figure-only documents into the same pipeline
45+
# Unifying figure-only documents into one pipeline is not straightforward.
4646
# without a "FigurePaginationStrategy".
4747
# So we defer to a helper method similar to the old one.
4848
return self._encode_figure_only(document)
@@ -114,7 +114,7 @@ def encode(self, document: Any) -> str:
114114

115115
# Handle case where no pages are generated (e.g. empty dataframe)
116116
if not pages:
117-
# Create a single empty page to ensure document structure (title, etc.) is rendered
117+
# Create empty page to ensure document structure (title, etc.) is rendered.
118118
# We use processed_df which might be empty but has correct schema
119119
pages = [
120120
PageContext(
@@ -130,7 +130,7 @@ def encode(self, document: Any) -> str:
130130
]
131131

132132
# Post-pagination fixup: Replace data with processed data (sliced correctly)
133-
# The strategy used original_df for calculation, but we want to render processed_df
133+
# Strategy used original_df, but we render processed_df.
134134
# (which has removed columns).
135135
if is_single_body(document.rtf_body):
136136
self._apply_data_post_processing(pages, processed_df, document.rtf_body)
@@ -208,11 +208,11 @@ def _apply_data_post_processing(self, pages, processed_df, rtf_body):
208208
p.data = restored.slice(curr, rows)
209209
curr += rows
210210

211-
def _encode_figure_only(self, document):
211+
def _encode_figure_only(self, document: RTFDocument):
212212
# (Legacy support for figure only)
213213
# For brevity, I will rely on the existing FigureService logic if possible
214214
# or just reproduce the simple loop.
215-
# ... (Implementation omitted for brevity, would match PaginatedStrategy._encode_figure_only_document_with_pagination)
215+
# Matches PaginatedStrategy._encode_figure_only_document_with_pagination.
216216
# Since the user wants a WORKING system, I must implement it.
217217
from copy import deepcopy
218218

@@ -322,7 +322,7 @@ def _encode_figure_only(self, document):
322322
parts.append("\n\n}")
323323
return "".join([p for p in parts if p])
324324

325-
def _encode_multi_section(self, document: "RTFDocument") -> str:
325+
def _encode_multi_section(self, document: RTFDocument) -> str:
326326
"""Encode a multi-section document where sections are concatenated row by row.
327327
328328
Args:

src/rtflite/pagination/processor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ def process(self, document: Any, page: PageContext) -> PageContext:
1919

2020
def _should_show_element(self, element_location: str, page: PageContext) -> bool:
2121
"""Determine if an element should be shown on a specific page."""
22-
rules = {
22+
logic = {
2323
"all": True,
2424
"first": page.is_first_page,
2525
"last": page.is_last_page,
2626
}
27-
return rules.get(element_location, False)
27+
return logic.get(element_location, False)
2828

2929
def _apply_pagination_borders(self, document, page: PageContext) -> Any:
3030
"""Apply proper borders for paginated context following r2rtf design."""

src/rtflite/pagination/strategies/defaults.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
from ..core import PageBreakCalculator, RTFPagination
32
from .base import PageContext, PaginationContext, PaginationStrategy
43

src/rtflite/pagination/strategies/grouping.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ def paginate(self, context: PaginationContext) -> list[PageContext]:
3939
page_df = context.df.slice(start_row, end_row - start_row + 1)
4040

4141
is_first = page_num == 0
42-
# Logic for repeating headers: if pageby_header is True,
43-
# or if it's the first page
42+
# Repeating headers: if pageby_header is True, or if it's the first page.
4443
needs_header = context.rtf_body.pageby_header or is_first
4544

4645
page_ctx = PageContext(
@@ -120,8 +119,7 @@ class SublineStrategy(PageByStrategy):
120119
"""Pagination strategy for subline_by (forces new pages and special headers)."""
121120

122121
def paginate(self, context: PaginationContext) -> list[PageContext]:
123-
# Subline strategy acts like page_by but uses subline_by columns and forces
124-
# new_page=True
122+
# Subline strategy uses subline_by columns and forces new_page=True.
125123
subline_by = context.rtf_body.subline_by
126124

127125
# Initialize calculator

src/rtflite/pagination/strategies/registry.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
from .base import PaginationStrategy
32

43

0 commit comments

Comments
 (0)