data_models: allow more complex section titles (#106)

This commit is contained in:
Sina Atalay 2024-06-10 17:31:13 +03:00
parent fd0bf6f09f
commit 0a5afc7294
2 changed files with 45 additions and 2 deletions

View File

@ -1030,7 +1030,7 @@ class CurriculumVitae(RenderCVBaseModel):
sections: list[SectionBase] = []
if self.sections_input is not None:
for title, section_or_entries in self.sections_input.items():
title = title.replace("_", " ").title()
title = dictionary_key_to_proper_section_title(title)
entry_type, section_type = get_entry_and_section_type(
section_or_entries[0]
@ -1125,6 +1125,7 @@ class LocaleCatalog(RenderCVBaseModel):
return value
LocaleCatalog() # Initialize the locale catalog with the default values
# ======================================================================================
@ -1286,6 +1287,34 @@ class RenderCVDataModel(RenderCVBaseModel):
return locale_catalog
def dictionary_key_to_proper_section_title(key: str) -> str:
"""Convert a dictionary key to a proper section title.
Example:
```python
dictionary_key_to_proper_section_title("section_title")
```
will return:
`#!python "Section Title"`
Args:
key (str): The key to convert to a proper section title.
Returns:
str: The proper section title.
"""
title = key.replace("_", " ")
words = title.split(" ")
# loop through the words and if the word doesn't contain any uppercase letters,
# capitalize the first letter of the word. If the word contains uppercase letters,
# don't change the word.
proper_title = " ".join(
word.capitalize() if word.islower() else word for word in words
)
return proper_title
def set_or_update_a_value(
data_model: pydantic.BaseModel | dict | list,
key: str,

View File

@ -742,3 +742,17 @@ def test_create_a_sample_yaml_input_file(tmp_path):
def test_default_input_file_doesnt_have_local_catalog():
yaml_contents = dm.create_a_sample_yaml_input_file()
assert "locale_catalog" not in yaml_contents
@pytest.mark.parametrize(
"key, expected_section_title",
[
("this_is_a_test", "This Is A Test"),
("welcome_to_RenderCV!", "Welcome To RenderCV!"),
("\\faGraduationCap_education", "\\faGraduationCap Education"),
("Hello_World", "Hello World"),
("Hello World", "Hello World"),
],
)
def test_dictionary_key_to_proper_section_title(key, expected_section_title):
assert dm.dictionary_key_to_proper_section_title(key) == expected_section_title