mirror of https://github.com/eyhc1/rendercv.git
update transform_markdown_sections_to_latex_sections
This commit is contained in:
parent
e64e3388ac
commit
e740735312
|
@ -132,9 +132,10 @@ class LaTeXFile(TemplatedFile):
|
||||||
data_model: dm.RenderCVDataModel,
|
data_model: dm.RenderCVDataModel,
|
||||||
environment: jinja2.Environment,
|
environment: jinja2.Environment,
|
||||||
):
|
):
|
||||||
data_model = transform_markdown_data_model_to_latex_data_model(
|
transformed_sections = transform_markdown_sections_to_latex_sections(
|
||||||
copy.deepcopy(data_model)
|
copy.deepcopy(data_model.cv.sections_input)
|
||||||
)
|
)
|
||||||
|
data_model.cv.sections_input = transformed_sections
|
||||||
super().__init__(data_model, environment)
|
super().__init__(data_model, environment)
|
||||||
|
|
||||||
def render_templates(self) -> tuple[str, str, list[tuple[str, list[str], str]]]:
|
def render_templates(self) -> tuple[str, str, list[tuple[str, list[str], str]]]:
|
||||||
|
@ -449,79 +450,50 @@ def markdown_to_latex(markdown_string: str) -> str:
|
||||||
return latex_string
|
return latex_string
|
||||||
|
|
||||||
|
|
||||||
def transform_markdown_data_model_to_latex_data_model(
|
def transform_markdown_sections_to_latex_sections(
|
||||||
data_model: dm.RenderCVDataModel,
|
sections: Optional[dict[str, dm.SectionInput]],
|
||||||
) -> dm.RenderCVDataModel:
|
) -> Optional[dict[str, dm.SectionInput]]:
|
||||||
"""
|
"""
|
||||||
Recursively loop through a `RenderCVDataModel` and convert all the markdown strings
|
Recursively loop through sections and convert all the markdown strings (user input
|
||||||
(user input is in markdown format) to LaTeX strings. Also, escape special LaTeX
|
is in markdown format) to $\\LaTeX$ strings. Also, escape special $\\LaTeX$
|
||||||
characters.
|
characters.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
data_model (RenderCVDataModel): The data model to transform.
|
sections (Optional[dict[str, dm.SectionInput]]): Sections with markdown strings.
|
||||||
Returns:
|
Returns:
|
||||||
dict: The data model with LaTeX strings.
|
Optional[dict[str, dm.SectionInput]]: Sections with $\\LaTeX$ strings.
|
||||||
"""
|
"""
|
||||||
data_model_as_dict = data_model.model_dump()
|
if sections is None:
|
||||||
for key, value in data_model_as_dict.items():
|
return None
|
||||||
if isinstance(value, str):
|
|
||||||
# if the value is a string, then apply markdown_to_latex and
|
|
||||||
# escape_latex_characters to it:
|
|
||||||
result = markdown_to_latex(escape_latex_characters(value))
|
|
||||||
# update data_model object's attribute with the new value:
|
|
||||||
setattr(data_model, key, result)
|
|
||||||
elif isinstance(value, list):
|
|
||||||
# if the value is a list, then loop through the list and apply
|
|
||||||
# markdown_to_latex and escape_latex_characters to each item:
|
|
||||||
transformed_list = []
|
|
||||||
for index, item in enumerate(value):
|
|
||||||
if isinstance(item, str):
|
|
||||||
result = markdown_to_latex(escape_latex_characters(item))
|
|
||||||
transformed_list.append(result)
|
|
||||||
elif isinstance(item, dict):
|
|
||||||
# if the item is a dictionary, then it means it's a sub data model.
|
|
||||||
# So, call transform_markdown_data_model_to_latex_data_model again:
|
|
||||||
sub_data_model = getattr(data_model, key)[index]
|
|
||||||
transformed_sub_data_model = (
|
|
||||||
transform_markdown_data_model_to_latex_data_model(
|
|
||||||
sub_data_model
|
|
||||||
)
|
|
||||||
)
|
|
||||||
transformed_list.append(transformed_sub_data_model)
|
|
||||||
|
|
||||||
# update data_model object's attribute with the new value:
|
for key, value in sections.items():
|
||||||
setattr(data_model, key, transformed_list)
|
# loop through the list and apply markdown_to_latex and escape_latex_characters
|
||||||
elif isinstance(value, dict):
|
# to each item:
|
||||||
if key == "sections_input":
|
transformed_list = []
|
||||||
# Then it means it's the `sections` field, it is a dictionary but
|
for entry in value:
|
||||||
# not a sub data model. Therefore the same function cannot be called.
|
if isinstance(entry, str):
|
||||||
# So, loop through the dictionary and apply markdown_to_latex and
|
# Then it means it's a TextEntry.
|
||||||
# escape_latex_characters to each item:
|
result = markdown_to_latex(escape_latex_characters(entry))
|
||||||
sections = getattr(data_model, key)
|
transformed_list.append(result)
|
||||||
for section_title, entries in sections.items():
|
|
||||||
transformed_entries = []
|
|
||||||
for entry in entries:
|
|
||||||
if isinstance(entry, str):
|
|
||||||
result = markdown_to_latex(escape_latex_characters(entry))
|
|
||||||
transformed_entries.append(result)
|
|
||||||
else:
|
|
||||||
transformed_entry = (
|
|
||||||
transform_markdown_data_model_to_latex_data_model(entry)
|
|
||||||
)
|
|
||||||
transformed_entries.append(transformed_entry)
|
|
||||||
setattr(data_model, key, sections)
|
|
||||||
else:
|
else:
|
||||||
# Then it means it's a sub data model.
|
# Then it means it's one of the other entries.
|
||||||
# So, call transform_markdown_data_model_to_latex_data_model again:
|
entry_as_dict = entry.model_dump()
|
||||||
sub_data_model = getattr(data_model, key)
|
for entry_key, value in entry_as_dict.items():
|
||||||
transformed_sub_data_model = (
|
if isinstance(value, str):
|
||||||
transform_markdown_data_model_to_latex_data_model(sub_data_model)
|
result = markdown_to_latex(escape_latex_characters(value))
|
||||||
)
|
setattr(entry, entry_key, result)
|
||||||
|
elif isinstance(value, list):
|
||||||
|
for j, item in enumerate(value):
|
||||||
|
if isinstance(item, str):
|
||||||
|
value[j] = markdown_to_latex(
|
||||||
|
escape_latex_characters(item)
|
||||||
|
)
|
||||||
|
setattr(entry, entry_key, value)
|
||||||
|
transformed_list.append(entry)
|
||||||
|
|
||||||
# update data_model object's attribute with the new value:
|
sections[key] = transformed_list
|
||||||
setattr(data_model, key, transformed_sub_data_model)
|
|
||||||
|
|
||||||
return data_model
|
return sections
|
||||||
|
|
||||||
|
|
||||||
def replace_placeholders_with_actual_values(
|
def replace_placeholders_with_actual_values(
|
||||||
|
|
Loading…
Reference in New Issue