Skip to content

Commit 0a67bf7

Browse files
committed
feat: implement unique key generation for reused tables in markdown output
1 parent 6300a1e commit 0a67bf7

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/paradoc/document.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ def __init__(
9696
db_path = self.source_dir / "data.db"
9797
self.db_manager = DbManager(db_path)
9898

99+
# Track table key usage to ensure unique keys when tables are reused with different filters/sorts
100+
self._table_key_usage_count: Dict[str, int] = {}
101+
99102
self._setup(create_dirs, clean_build_dir)
100103

101104
def _iter_md_files(self) -> Iterable[pathlib.Path]:
@@ -332,16 +335,29 @@ def _get_table_markdown_from_db(self, full_reference: str, key_clean: str, use_t
332335
props = dict(index=show_index, tablefmt="grid")
333336
tbl_str = df.to_markdown(**props)
334337

338+
# Generate unique key for this table instance
339+
# Track usage count and append numeric suffix for reused tables
340+
if key_clean not in self._table_key_usage_count:
341+
self._table_key_usage_count[key_clean] = 0
342+
unique_key = key_clean
343+
else:
344+
self._table_key_usage_count[key_clean] += 1
345+
unique_key = f"{key_clean}_{self._table_key_usage_count[key_clean]}"
346+
335347
# Add caption unless nocaption flag is set
336348
# NOTE: Do NOT include the annotation in the caption - it's only for transformation
337349
if not (annotation and annotation.no_caption):
338350
tbl_str += f"\n\nTable: {table_data.caption}"
339-
tbl_str += f" {{#tbl:{key_clean}}}"
351+
tbl_str += f" {{#tbl:{unique_key}}}"
340352

341353
return tbl_str
342354

343355
def _perform_variable_substitution(self, use_table_var_substitution):
344356
logger.info("Performing variable substitution")
357+
358+
# Reset table key usage counter for each compilation
359+
self._table_key_usage_count.clear()
360+
345361
for mdf in self.md_files_main + self.md_files_app:
346362
md_file = mdf.path
347363
os.makedirs(mdf.new_file.parent, exist_ok=True)

0 commit comments

Comments
 (0)