inject_to_template function fixed

This commit is contained in:
Nicolas 2025-05-31 11:32:05 +02:00
parent ecc97bd9a6
commit 5ac7659f01
2 changed files with 63 additions and 4 deletions

View File

@ -277,9 +277,10 @@ def inject_to_template(fields_dict: dict[str, str],
raise Exception('No template provided') raise Exception('No template provided')
def replace_field(match: re.Match[str]) -> str: 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(): class DocumentWriter():
@ -710,7 +711,8 @@ class DocumentWriter():
content = 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_code = inject_to_template({'CONTENT': content},
latex_template_path, latex_template_path,
'templates/default_template.tex') internal_template = 'templates/default_template.tex',
fields_dict = fields_dict)
if font_family == 'sans-serif': if font_family == 'sans-serif':
latex_code = latex.inject_latex_command(latex_code, '\\renewcommand{\\familydefault}{\\sfdefault}') latex_code = latex.inject_latex_command(latex_code, '\\renewcommand{\\familydefault}{\\sfdefault}')

View File

@ -1,7 +1,7 @@
import pyladoc import pyladoc
def test_inject_to_template(): def test_inject_to_template_html():
template = """ template = """
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
@ -25,4 +25,61 @@ def test_inject_to_template():
print(result) print(result)
assert "Hello, World!" in result assert "Hello, World!" in result
assert "<!-- some comment -->" in result # Keep unrelated HTML comments
assert "<title>Test Title</title>" in result assert "<title>Test Title</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{<!--PROJECT-->}
<!--AUTHOR-->
\\date{\\today}
\begin{document}
% Title Page
\\maketitle
% <!--CONTENT-->
\\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