diff --git a/src/pyladoc/__init__.py b/src/pyladoc/__init__.py index e594daa..6b69651 100644 --- a/src/pyladoc/__init__.py +++ b/src/pyladoc/__init__.py @@ -246,30 +246,40 @@ def _create_document_writer() -> 'DocumentWriter': return new_dwr -def inject_to_template(content: str, template_path: str = '', internal_template: str = '') -> str: +def inject_to_template(fields_dict: dict[str, str], + template_path: str = '', + internal_template: str = '', + template_string: str = '', + ) -> str: """ - injects a content string into a template. The placeholder + injects content fields into a template. The placeholder will be replaced by the content. If the placeholder is prefixed with a '%' comment character, this character will be replaced as well. Args: + fields_dict: A dictionary with field names as keys and content as values template_path: Path to a template file internal_template: Path to a internal default template + template_string: A template string to use directly Returns: Template with included content """ + assert isinstance(fields_dict, dict), 'fields_dict must be a dictionary' if template_path: with open(template_path, 'r') as f: template = f.read() elif internal_template: template = _get_pkgutil_string(internal_template) + elif template_string: + template = template_string else: raise Exception('No template provided') - assert '' in template, 'No expression in template located' - prep_template = re.sub(r"\%?\s*", '', template) - return prep_template.replace('', content) + def replace_field(match: re.Match[str]) -> str: + return fields_dict.get(match.group(1), match.string) + + return re.sub(r"(?:\%+\s*)?\", replace_field, template) class DocumentWriter(): @@ -675,6 +685,7 @@ class DocumentWriter(): font_family: Literal[None, 'serif', 'sans-serif'] = None, table_renderer: TRenderer = 'simple', latex_template_path: str = '', + fields_dict: dict[str, str] = {}, figure_scale: float = 1, engine: latex.LatexEngine = 'pdflatex') -> bool: """ @@ -683,15 +694,21 @@ class DocumentWriter(): Args: file_path: The path to save the PDF file to font_family: Overwrites the front family for figures and the template + table_renderer: The renderer for tables (simple: renderer with column type + guessing for text and numbers; pandas: using the internal pandas LaTeX renderer) latex_template_path: Path to a LaTeX template file. The expression will be replaced by the generated content. If no path is provided a default template is used. + fields_dict: A dictionary with field names as keys and content as values + replacing the placeholders in the template. + figure_scale: Scaling factor for the figure size engine: LaTeX engine (pdflatex, lualatex, xelatex or tectonic) Returns: True if the PDF file was successfully created """ - latex_code = inject_to_template(self.to_latex(font_family, table_renderer, figure_scale), + content = self.to_latex(font_family, table_renderer, figure_scale) + latex_code = inject_to_template({'CONTENT': content}, latex_template_path, 'templates/default_template.tex') diff --git a/tests/document_validation.py b/tests/document_validation.py index 01ba2a2..a8de191 100644 --- a/tests/document_validation.py +++ b/tests/document_validation.py @@ -50,7 +50,7 @@ def validate_html(html_string: str, validate_online: bool = False, check_for: li assert tag_type in tags, f"Tag {tag_type} not found in the html code" if validate_online: - test_page = pyladoc.inject_to_template(html_string, internal_template='templates/test_template.html') + test_page = pyladoc.inject_to_template({'CONTENT': html_string}, internal_template='templates/test_template.html') validation_result = validate_html_with_w3c(test_page) assert 'messages' in validation_result, 'Validate request failed' if validation_result['messages']: diff --git a/tests/test_rendering_example1_doc.py b/tests/test_rendering_example1_doc.py index ad30c81..fbeb286 100644 --- a/tests/test_rendering_example1_doc.py +++ b/tests/test_rendering_example1_doc.py @@ -158,7 +158,7 @@ def test_html_render(): if WRITE_RESULT_FILES: with open('tests/out/test_html_render1.html', 'w', encoding='utf-8') as f: - f.write(pyladoc.inject_to_template(html_code, internal_template='templates/test_template.html')) + f.write(pyladoc.inject_to_template({'CONTENT': html_code}, internal_template='templates/test_template.html')) def test_latex_render(): diff --git a/tests/test_rendering_example2_doc.py b/tests/test_rendering_example2_doc.py index 2b0379d..52a9f20 100644 --- a/tests/test_rendering_example2_doc.py +++ b/tests/test_rendering_example2_doc.py @@ -90,9 +90,11 @@ def test_html_render(): document_validation.validate_html(html_code, VALIDATE_HTML_CODE_ONLINE) + title = 'Test HTML Render 2' + if WRITE_RESULT_FILES: with open('tests/out/test_html_render2.html', 'w', encoding='utf-8') as f: - f.write(pyladoc.inject_to_template(html_code, internal_template='templates/test_template.html')) + f.write(pyladoc.inject_to_template({'CONTENT': html_code}, internal_template='templates/test_template.html')) def test_latex_render(): diff --git a/tests/test_template_functions.py b/tests/test_template_functions.py new file mode 100644 index 0000000..77602f8 --- /dev/null +++ b/tests/test_template_functions.py @@ -0,0 +1,28 @@ +import pyladoc + +def test_inject_to_template(): + template = """ + + +
+ + +