mirror of
https://github.com/rendercv/rendercv.git
synced 2026-05-19 14:15:40 -04:00
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.
This commit is contained in:
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user