diff --git a/rendercv/data_models.py b/rendercv/data_models.py index 8a9f3f0..7fc3492 100644 --- a/rendercv/data_models.py +++ b/rendercv/data_models.py @@ -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,7 +1125,8 @@ class LocaleCatalog(RenderCVBaseModel): return value -LocaleCatalog() # Initialize the locale catalog with the default values + +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, diff --git a/tests/test_data_models.py b/tests/test_data_models.py index 9da454a..c9f4577 100644 --- a/tests/test_data_models.py +++ b/tests/test_data_models.py @@ -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