diff --git a/src/script_content_reader.py b/src/script_content_reader.py index 09c0dda..82ddd5f 100644 --- a/src/script_content_reader.py +++ b/src/script_content_reader.py @@ -13,8 +13,8 @@ def read(self, scripts: list[ScriptMetadata]) -> list[ScriptMetadata]: ... class ScriptContentReader: def __init__(self) -> None: - self._section_start_regex = r".*code_embedder:.*start" - self._section_end_regex = r".*code_embedder:.*end" + self._section_start_regex = r".*code_embedder:section_name start" + self._section_end_regex = r".*code_embedder:section_name end" def read(self, scripts: list[ScriptMetadata]) -> list[ScriptMetadata]: scripts_with_full_contents = self._read_full_script(scripts) @@ -68,7 +68,9 @@ def _extract_part(self, script: ScriptMetadata) -> str: start, end = self._extract_object_part(script) elif script.extraction_type == "section": - start, end = self._extract_section_part(lines) + start, end = self._extract_section_part( + lines=lines, section=script.extraction_part + ) if not self._validate_section_bounds(start, end, script): return "" @@ -96,11 +98,21 @@ def _extract_object_part(self, script: ScriptMetadata) -> tuple[int | None, int return None, None - def _extract_section_part(self, lines: list[str]) -> tuple[int | None, int | None]: + def _extract_section_part( + self, lines: list[str], section: str | None = None + ) -> tuple[int | None, int | None]: + if not section: + return None, None + + updated_section_start_regex = self._section_start_regex.replace( + "section_name", section + ) + updated_section_end_regex = self._section_end_regex.replace("section_name", section) + for i, line in enumerate(lines): - if re.search(self._section_start_regex, line): + if re.search(updated_section_start_regex, line): start = i + 1 - elif re.search(self._section_end_regex, line): + elif re.search(updated_section_end_regex, line): return start, i return None, None diff --git a/tests/data/example_section.py b/tests/data/example_section.py index edb9b08..74e6933 100644 --- a/tests/data/example_section.py +++ b/tests/data/example_section.py @@ -2,3 +2,7 @@ # code_embedder:A start print("Printing only section A") # code_embedder:A end + +# code_embedder:B start +print("Printing only section B") +# code_embedder:B end diff --git a/tests/data/expected_readme1.md b/tests/data/expected_readme1.md index b57b749..83481f8 100644 --- a/tests/data/expected_readme1.md +++ b/tests/data/expected_readme1.md @@ -13,6 +13,11 @@ name: Test description: Description ``` +This will be filled: +```python:tests/data/example_section.py:s:B +print("Printing only section B") +``` + This won't be updated: ``` print("Hello, World!") diff --git a/tests/data/readme1.md b/tests/data/readme1.md index 9eb576e..cb41805 100644 --- a/tests/data/readme1.md +++ b/tests/data/readme1.md @@ -4,7 +4,6 @@ This is a test readme. This will be filled: ```python:tests/data/example_section.py:s:A -print("Printing only section A") ``` This won't be updated: @@ -13,6 +12,10 @@ name: Test description: Description ``` +This will be filled: +```python:tests/data/example_section.py:s:B +``` + This won't be updated: ``` print("Hello, World!") diff --git a/tests/test_script_content_reader.py b/tests/test_script_content_reader.py index 39250a6..0f79ff1 100644 --- a/tests/test_script_content_reader.py +++ b/tests/test_script_content_reader.py @@ -48,6 +48,10 @@ def create_script_metadata( "# code_embedder:A start\n" 'print("Printing only section A")\n' "# code_embedder:A end\n" + "\n" + "# code_embedder:B start\n" + 'print("Printing only section B")\n' + "# code_embedder:B end" ), ), ),