update transform_markdown_sections_to_latex_sections

This commit is contained in:
Sina Atalay 2024-02-19 19:30:53 +01:00
parent e64e3388ac
commit e740735312
1 changed files with 37 additions and 65 deletions

View File

@ -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 for key, value in sections.items():
# escape_latex_characters to it: # loop through the list and apply markdown_to_latex and escape_latex_characters
result = markdown_to_latex(escape_latex_characters(value)) # to each item:
# 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 = [] transformed_list = []
for index, item in enumerate(value): for entry in 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:
setattr(data_model, key, transformed_list)
elif isinstance(value, dict):
if key == "sections_input":
# Then it means it's the `sections` field, it is a dictionary but
# not a sub data model. Therefore the same function cannot be called.
# So, loop through the dictionary and apply markdown_to_latex and
# escape_latex_characters to each item:
sections = getattr(data_model, key)
for section_title, entries in sections.items():
transformed_entries = []
for entry in entries:
if isinstance(entry, str): if isinstance(entry, str):
# Then it means it's a TextEntry.
result = markdown_to_latex(escape_latex_characters(entry)) result = markdown_to_latex(escape_latex_characters(entry))
transformed_entries.append(result) transformed_list.append(result)
else: else:
transformed_entry = ( # Then it means it's one of the other entries.
transform_markdown_data_model_to_latex_data_model(entry) entry_as_dict = entry.model_dump()
) for entry_key, value in entry_as_dict.items():
transformed_entries.append(transformed_entry) if isinstance(value, str):
setattr(data_model, key, sections) result = markdown_to_latex(escape_latex_characters(value))
else: setattr(entry, entry_key, result)
# Then it means it's a sub data model. elif isinstance(value, list):
# So, call transform_markdown_data_model_to_latex_data_model again: for j, item in enumerate(value):
sub_data_model = getattr(data_model, key) if isinstance(item, str):
transformed_sub_data_model = ( value[j] = markdown_to_latex(
transform_markdown_data_model_to_latex_data_model(sub_data_model) 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(