Compare commits

...

2 Commits

Author SHA1 Message Date
Nicolas 664a08b2dc arguments for inject_to_template in "to_pdf" function and code style issues fixed 2025-05-31 11:36:37 +02:00
Nicolas 5ac7659f01 inject_to_template function fixed 2025-05-31 11:32:05 +02:00
2 changed files with 64 additions and 5 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():
@ -708,9 +709,9 @@ class DocumentWriter():
True if the PDF file was successfully created True if the PDF file was successfully created
""" """
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} | fields_dict,
latex_template_path, latex_template_path,
'templates/default_template.tex') internal_template='templates/default_template.tex')
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,62 @@ 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