diff --git a/src/pyladoc/__init__.py b/src/pyladoc/__init__.py index 6b69651..c057caf 100644 --- a/src/pyladoc/__init__.py +++ b/src/pyladoc/__init__.py @@ -277,9 +277,10 @@ def inject_to_template(fields_dict: dict[str, str], raise Exception('No template provided') def replace_field(match: re.Match[str]) -> str: - return fields_dict.get(match.group(1), match.string) + print('--->', match.group(0)) + return fields_dict.get(match.group(1), match.group(0)) - return re.sub(r"(?:\%+\s*)?\", replace_field, template) + return re.sub(r"(?:\%+\s*)?\", replace_field, template, 0, re.MULTILINE) class DocumentWriter(): @@ -710,7 +711,8 @@ class DocumentWriter(): content = self.to_latex(font_family, table_renderer, figure_scale) latex_code = inject_to_template({'CONTENT': content}, latex_template_path, - 'templates/default_template.tex') + internal_template = 'templates/default_template.tex', + fields_dict = fields_dict) if font_family == 'sans-serif': latex_code = latex.inject_latex_command(latex_code, '\\renewcommand{\\familydefault}{\\sfdefault}') diff --git a/tests/test_template_functions.py b/tests/test_template_functions.py index f5ccbcc..9f513b9 100644 --- a/tests/test_template_functions.py +++ b/tests/test_template_functions.py @@ -1,7 +1,7 @@ import pyladoc -def test_inject_to_template(): +def test_inject_to_template_html(): template = """ @@ -25,4 +25,61 @@ def test_inject_to_template(): print(result) assert "Hello, World!" in result + assert "" in result # Keep unrelated HTML comments assert "Test Title" in result + +def test_inject_to_template_latex(): + template = """ +\\documentclass[a4paper,12pt]{article} + +% Packages +\\usepackage[utf8]{inputenc} +\\usepackage[T1]{fontenc} +\\usepackage{lmodern} % Load Latin Modern font +\\usepackage{graphicx} % For including images +\\usepackage{amsmath} % For mathematical symbols +\\usepackage{amssymb} % For additional symbols +\\usepackage{hyperref} % For hyperlinks +\\usepackage{caption} % For customizing captions +\\usepackage{geometry} % To set margins +\\usepackage{natbib} % For citations +\\usepackage{float} % For fixing figure positions +\\usepackage{siunitx} % For scientific units +\\usepackage{booktabs} % For professional-looking tables +\\usepackage{pgf} % For using pgf grafics +\\usepackage{textcomp, gensymb} % provides \\degree symbol + +\\sisetup{ + table-align-text-post = false +} + +% Geometry Settings +\\geometry{margin=1in} % 1-inch margins + +% Title and Author Information +\\title{} + +\\date{\\today} + +\begin{document} + +% Title Page +\\maketitle + +% +\\end{document} + """ + + content = "Hello, World!" + project_name = "Test Project" + author_name = "Otto" + + result = pyladoc.inject_to_template( + {'CONTENT': content, 'PROJECT': project_name, 'AUTHOR': author_name}, + template_string=template) + + print(result) + + assert "\nOtto\n" in result + assert "\\title{Test Project}\n" in result + assert "Hello, World!" in result