mirror of https://github.com/Nonannet/pyladoc.git
Template injection function extended to multiple fields; tests added and adjusted
This commit is contained in:
parent
a413d771b3
commit
a65bb1c8e5
|
@ -246,30 +246,40 @@ def _create_document_writer() -> 'DocumentWriter':
|
||||||
return new_dwr
|
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 <!--CONTENT-->
|
injects content fields into a template. The placeholder <!--CONTENT-->
|
||||||
will be replaced by the content. If the placeholder is prefixed with a
|
will be replaced by the content. If the placeholder is prefixed with a
|
||||||
'%' comment character, this character will be replaced as well.
|
'%' comment character, this character will be replaced as well.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
fields_dict: A dictionary with field names as keys and content as values
|
||||||
template_path: Path to a template file
|
template_path: Path to a template file
|
||||||
internal_template: Path to a internal default template
|
internal_template: Path to a internal default template
|
||||||
|
template_string: A template string to use directly
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Template with included content
|
Template with included content
|
||||||
"""
|
"""
|
||||||
|
assert isinstance(fields_dict, dict), 'fields_dict must be a dictionary'
|
||||||
if template_path:
|
if template_path:
|
||||||
with open(template_path, 'r') as f:
|
with open(template_path, 'r') as f:
|
||||||
template = f.read()
|
template = f.read()
|
||||||
elif internal_template:
|
elif internal_template:
|
||||||
template = _get_pkgutil_string(internal_template)
|
template = _get_pkgutil_string(internal_template)
|
||||||
|
elif template_string:
|
||||||
|
template = template_string
|
||||||
else:
|
else:
|
||||||
raise Exception('No template provided')
|
raise Exception('No template provided')
|
||||||
|
|
||||||
assert '<!--CONTENT-->' in template, 'No <!--CONTENT--> expression in template located'
|
def replace_field(match: re.Match[str]) -> str:
|
||||||
prep_template = re.sub(r"\%?\s*<!--CONTENT-->", '<!--CONTENT-->', template)
|
return fields_dict.get(match.group(1), match.string)
|
||||||
return prep_template.replace('<!--CONTENT-->', content)
|
|
||||||
|
return re.sub(r"(?:\%+\s*)?\<!--(.*?)-->", replace_field, template)
|
||||||
|
|
||||||
|
|
||||||
class DocumentWriter():
|
class DocumentWriter():
|
||||||
|
@ -675,6 +685,7 @@ class DocumentWriter():
|
||||||
font_family: Literal[None, 'serif', 'sans-serif'] = None,
|
font_family: Literal[None, 'serif', 'sans-serif'] = None,
|
||||||
table_renderer: TRenderer = 'simple',
|
table_renderer: TRenderer = 'simple',
|
||||||
latex_template_path: str = '',
|
latex_template_path: str = '',
|
||||||
|
fields_dict: dict[str, str] = {},
|
||||||
figure_scale: float = 1,
|
figure_scale: float = 1,
|
||||||
engine: latex.LatexEngine = 'pdflatex') -> bool:
|
engine: latex.LatexEngine = 'pdflatex') -> bool:
|
||||||
"""
|
"""
|
||||||
|
@ -683,15 +694,21 @@ class DocumentWriter():
|
||||||
Args:
|
Args:
|
||||||
file_path: The path to save the PDF file to
|
file_path: The path to save the PDF file to
|
||||||
font_family: Overwrites the front family for figures and the template
|
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
|
latex_template_path: Path to a LaTeX template file. The
|
||||||
expression <!--CONTENT--> will be replaced by the generated content.
|
expression <!--CONTENT--> will be replaced by the generated content.
|
||||||
If no path is provided a default template is used.
|
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 <!--KEY--> in the template.
|
||||||
|
figure_scale: Scaling factor for the figure size
|
||||||
engine: LaTeX engine (pdflatex, lualatex, xelatex or tectonic)
|
engine: LaTeX engine (pdflatex, lualatex, xelatex or tectonic)
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
True if the PDF file was successfully created
|
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,
|
latex_template_path,
|
||||||
'templates/default_template.tex')
|
'templates/default_template.tex')
|
||||||
|
|
||||||
|
|
|
@ -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"
|
assert tag_type in tags, f"Tag {tag_type} not found in the html code"
|
||||||
|
|
||||||
if validate_online:
|
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)
|
validation_result = validate_html_with_w3c(test_page)
|
||||||
assert 'messages' in validation_result, 'Validate request failed'
|
assert 'messages' in validation_result, 'Validate request failed'
|
||||||
if validation_result['messages']:
|
if validation_result['messages']:
|
||||||
|
|
|
@ -158,7 +158,7 @@ def test_html_render():
|
||||||
|
|
||||||
if WRITE_RESULT_FILES:
|
if WRITE_RESULT_FILES:
|
||||||
with open('tests/out/test_html_render1.html', 'w', encoding='utf-8') as f:
|
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():
|
def test_latex_render():
|
||||||
|
|
|
@ -90,9 +90,11 @@ def test_html_render():
|
||||||
|
|
||||||
document_validation.validate_html(html_code, VALIDATE_HTML_CODE_ONLINE)
|
document_validation.validate_html(html_code, VALIDATE_HTML_CODE_ONLINE)
|
||||||
|
|
||||||
|
title = 'Test HTML Render 2'
|
||||||
|
|
||||||
if WRITE_RESULT_FILES:
|
if WRITE_RESULT_FILES:
|
||||||
with open('tests/out/test_html_render2.html', 'w', encoding='utf-8') as f:
|
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():
|
def test_latex_render():
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
import pyladoc
|
||||||
|
|
||||||
|
def test_inject_to_template():
|
||||||
|
template = """
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title><!--TITLE--></title>
|
||||||
|
</head>
|
||||||
|
<!-- some comment -->
|
||||||
|
<body>
|
||||||
|
<!--CONTENT-->
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
"""
|
||||||
|
|
||||||
|
content = "Hello, World!"
|
||||||
|
title = "Test Title"
|
||||||
|
|
||||||
|
|
||||||
|
result = pyladoc.inject_to_template({'CONTENT': content, 'TITLE': title},template_string=template)
|
||||||
|
|
||||||
|
print(result)
|
||||||
|
|
||||||
|
assert "Hello, World!" in result
|
||||||
|
assert "<title>Test Title</title>" in result
|
Loading…
Reference in New Issue