From 304df0350fa10484978406a87ce7a119f4da7dbd Mon Sep 17 00:00:00 2001 From: Sina Atalay <79940989+sinaatalay@users.noreply.github.com> Date: Tue, 24 Mar 2026 19:45:53 +0300 Subject: [PATCH] Extract comment_out_section_sub_fields helper in sample_generator The YAML string manipulation that comments out design and locale sub-fields was inline and duplicated. Extracted into a named helper with clear docstring explaining the splitting/commenting/reassembling strategy. Uses str.partition() instead of str.split() for safer two-way splitting. --- src/rendercv/schema/sample_generator.py | 78 ++++++++++++++++--------- 1 file changed, 49 insertions(+), 29 deletions(-) diff --git a/src/rendercv/schema/sample_generator.py b/src/rendercv/schema/sample_generator.py index 678123d9..1997e26a 100644 --- a/src/rendercv/schema/sample_generator.py +++ b/src/rendercv/schema/sample_generator.py @@ -18,6 +18,42 @@ from .models.rendercv_model import RenderCVModel from .rendercv_model_builder import read_yaml +def comment_out_section_sub_fields( + yaml_string: str, *, section_header: str, next_section_header: str +) -> str: + """Comment out YAML sub-fields between a section header and the next section. + + Why: + Sample YAML files show design and locale options as comments so beginners + see the structure without being overwhelmed. This splits on exact section + boundaries, comments the lines between them, then reassembles. + + Args: + yaml_string: Full YAML string. + section_header: The section header line(s) to find (e.g., "design:\\n theme: classic\\n"). + next_section_header: The next section header that ends this section's sub-fields. + + Returns: + YAML string with sub-fields of the target section commented out. + """ + before_section, _, after_section_header = yaml_string.partition(section_header) + sub_fields, _, rest = after_section_header.partition(next_section_header) + + commented_lines = [ + f" {line.replace(' ', '# ', 1)}" + for line in sub_fields.splitlines(keepends=False) + ] + + return ( + before_section + + section_header + + "\n".join(commented_lines) + + "\n" + + next_section_header + + rest + ) + + def expand_nested_bullets(yaml_string: str) -> str: """Expand inline nested bullets in YAML list items to indented sub-items. @@ -177,39 +213,23 @@ def create_sample_yaml_input_file( yaml_string = expand_nested_bullets(dictionary_to_yaml(data_model_as_dictionary)) # Add a comment to the first line, for JSON Schema: - comment_to_add = ( + schema_comment = ( "# yaml-language-server:" f" $schema=https://raw.githubusercontent.com/rendercv/rendercv/refs/tags/v{__version__}/schema.json\n" ) - yaml_string = comment_to_add + yaml_string + yaml_string = schema_comment + yaml_string - yaml_design_theme_part = f"design:\n theme: {theme}\n" - split_yaml_string = yaml_string.split(yaml_design_theme_part) - yaml_cv_field = split_yaml_string[0] - - yaml_locale_part = f"locale:\n language: {locale}\n" - split_yaml_string = split_yaml_string[1].split(yaml_locale_part) - below_design = split_yaml_string[0].replace(yaml_design_theme_part, "") - below_design = [ - f" {line.replace(' ', '# ', 1)}" - for line in below_design.splitlines(keepends=False) - ] - yaml_design_field = yaml_design_theme_part + "\n".join(below_design) + "\n" - - # Handle locale field commenting (similar to design) - locale_and_settings = split_yaml_string[1] - settings_part = "settings:\n" - split_by_settings = locale_and_settings.split(settings_part) - below_locale = split_by_settings[0] - below_locale_commented = [ - f" {line.replace(' ', '# ', 1)}" - for line in below_locale.splitlines(keepends=False) - ] - yaml_locale_field = yaml_locale_part + "\n".join(below_locale_commented) + "\n" - yaml_settings_field = settings_part + split_by_settings[1] - - yaml_string = ( - yaml_cv_field + yaml_design_field + yaml_locale_field + yaml_settings_field + # Comment out design and locale sub-fields so beginners see the structure + # without being overwhelmed by configuration options. + yaml_string = comment_out_section_sub_fields( + yaml_string, + section_header=f"design:\n theme: {theme}\n", + next_section_header=f"locale:\n language: {locale}\n", + ) + yaml_string = comment_out_section_sub_fields( + yaml_string, + section_header=f"locale:\n language: {locale}\n", + next_section_header="settings:\n", ) if file_path is not None: