From 59a53f1be2348cbdaa1ea9dc42f85cc15a747b1a Mon Sep 17 00:00:00 2001 From: Nicolas Date: Fri, 2 May 2025 16:40:41 +0200 Subject: [PATCH] Linking to fig, table and equation references added for HTML output --- src/pyladoc/__init__.py | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/src/pyladoc/__init__.py b/src/pyladoc/__init__.py index 0b64d25..7301a34 100644 --- a/src/pyladoc/__init__.py +++ b/src/pyladoc/__init__.py @@ -218,7 +218,7 @@ def _fillin_reference_names(input_string: str, item_index: dict[str, int]) -> st for start, end, ref in replacements: assert ref in item_index, f"Reference {ref} does not exist in the document" ret.append(input_string[current_pos:start - 1]) - ret.append(str(item_index[ref])) + ret.append(f'{item_index[ref]}') current_pos = end return ''.join(ret) + input_string[current_pos:] @@ -349,14 +349,14 @@ class DocumentWriter(): return inline_pattern.sub(inline_repl, result) - def _get_equation_html(self, latex_equation: str, caption: str, block: bool = False) -> str: + def _get_equation_html(self, latex_equation: str, caption: str, full_ref_str: str, block: bool = False) -> str: fig = latex_to_figure(latex_equation) if block: - ret = ('
' - '
%s
' - '
%s
') % ( - figure_to_string(fig, self._figure_format, base64=self._base64_svgs), - caption) + fig_str = figure_to_string(fig, self._figure_format, base64=self._base64_svgs) + ret = ('
' + f'
{fig_str}
' + f'
{caption}
') else: ret = '' + figure_to_string(fig, self._figure_format, base64=self._base64_svgs) + '' @@ -372,6 +372,7 @@ class DocumentWriter(): self.modified_html = StringIO() self.in_latex: bool = False self.eq_caption: str = '' + self.full_ref_str: str = '' self.block: bool = False self.dw = document_writer @@ -382,6 +383,8 @@ class DocumentWriter(): self.in_latex = True attr_dict = {k: v if v else '' for k, v in attrs} self.eq_caption = attr_dict.get('caption', '') + self.full_ref_str = latex.normalize_label_text( + f"pyld-ref-{attr_dict.get('ref_type', '')}:{attr_dict.get('ref_id', '')}") self.block = attr_dict.get('type') == 'block' elif not self.in_latex: tag_text = self.get_starttag_text() @@ -391,7 +394,7 @@ class DocumentWriter(): def handle_data(self, data: str) -> None: if self.in_latex: self.modified_html.write( - self.dw._get_equation_html(data, self.eq_caption, self.block)) + self.dw._get_equation_html(data, self.eq_caption, self.full_ref_str, self.block)) else: self.modified_html.write(data) @@ -429,10 +432,12 @@ class DocumentWriter(): has an individual numbering centered: Whether to center the figure in LaTeX output """ - + def render_to_html() -> str: caption_prefix = self._add_item(ref_id, ref_type, prefix_pattern) - return '
%s%s
' % ( + full_ref_str = latex.normalize_label_text(f"pyld-ref-{ref_type}:{ref_id}") + return '
%s%s
' % ( + full_ref_str, figure_to_string(fig, self._figure_format, base64=self._base64_svgs, scale=self._fig_scale), '
' + caption_prefix + escape_html(caption) if caption else '') @@ -466,9 +471,11 @@ class DocumentWriter(): assert isinstance(styler, Styler), 'Jinja2 package is required for rendering tables' def render_to_html() -> str: + full_ref_str = latex.normalize_label_text(f"pyld-ref-{ref_type}:{ref_id}") caption_prefix = self._add_item(ref_id, ref_type, prefix_pattern) + html_string = styler.to_html(table_uuid=ref_id, caption=caption_prefix + escape_html(caption)) - return re.sub(r'.*?', '', html_string, flags=re.DOTALL) + return f'
' + re.sub(r'.*?', '', html_string, flags=re.DOTALL) + '
' def render_to_latex() -> str: self._add_item(ref_id, ref_type, prefix_pattern) @@ -582,7 +589,8 @@ class DocumentWriter(): def render_to_html() -> str: caption = self._add_item(ref_id, ref_type, '({})') - return self._get_equation_html(latex_equation, caption) + full_ref_str = latex.normalize_label_text(f"pyld-ref-{ref_type}:{ref_id}") + return self._get_equation_html(latex_equation, caption, full_ref_str) def render_to_latex() -> str: self._add_item(ref_id, ref_type, '')