Skip to content

Commit bad1ed9

Browse files
authored
fix: Fix regex for section_name lookup
fix: Fix regex for section_name lookup
2 parents 1dfc5e7 + 592a25b commit bad1ed9

5 files changed

+35
-7
lines changed

src/script_content_reader.py

+18-6
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ def read(self, scripts: list[ScriptMetadata]) -> list[ScriptMetadata]: ...
1313

1414
class ScriptContentReader:
1515
def __init__(self) -> None:
16-
self._section_start_regex = r".*code_embedder:.*start"
17-
self._section_end_regex = r".*code_embedder:.*end"
16+
self._section_start_regex = r".*code_embedder:section_name start"
17+
self._section_end_regex = r".*code_embedder:section_name end"
1818

1919
def read(self, scripts: list[ScriptMetadata]) -> list[ScriptMetadata]:
2020
scripts_with_full_contents = self._read_full_script(scripts)
@@ -68,7 +68,9 @@ def _extract_part(self, script: ScriptMetadata) -> str:
6868
start, end = self._extract_object_part(script)
6969

7070
elif script.extraction_type == "section":
71-
start, end = self._extract_section_part(lines)
71+
start, end = self._extract_section_part(
72+
lines=lines, section=script.extraction_part
73+
)
7274
if not self._validate_section_bounds(start, end, script):
7375
return ""
7476

@@ -96,11 +98,21 @@ def _extract_object_part(self, script: ScriptMetadata) -> tuple[int | None, int
9698

9799
return None, None
98100

99-
def _extract_section_part(self, lines: list[str]) -> tuple[int | None, int | None]:
101+
def _extract_section_part(
102+
self, lines: list[str], section: str | None = None
103+
) -> tuple[int | None, int | None]:
104+
if not section:
105+
return None, None
106+
107+
updated_section_start_regex = self._section_start_regex.replace(
108+
"section_name", section
109+
)
110+
updated_section_end_regex = self._section_end_regex.replace("section_name", section)
111+
100112
for i, line in enumerate(lines):
101-
if re.search(self._section_start_regex, line):
113+
if re.search(updated_section_start_regex, line):
102114
start = i + 1
103-
elif re.search(self._section_end_regex, line):
115+
elif re.search(updated_section_end_regex, line):
104116
return start, i
105117

106118
return None, None

tests/data/example_section.py

+4
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@
22
# code_embedder:A start
33
print("Printing only section A")
44
# code_embedder:A end
5+
6+
# code_embedder:B start
7+
print("Printing only section B")
8+
# code_embedder:B end

tests/data/expected_readme1.md

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ name: Test
1313
description: Description
1414
```
1515
16+
This will be filled:
17+
```python:tests/data/example_section.py:s:B
18+
print("Printing only section B")
19+
```
20+
1621
This won't be updated:
1722
```
1823
print("Hello, World!")

tests/data/readme1.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ This is a test readme.
44

55
This will be filled:
66
```python:tests/data/example_section.py:s:A
7-
print("Printing only section A")
87
```
98

109
This won't be updated:
@@ -13,6 +12,10 @@ name: Test
1312
description: Description
1413
```
1514
15+
This will be filled:
16+
```python:tests/data/example_section.py:s:B
17+
```
18+
1619
This won't be updated:
1720
```
1821
print("Hello, World!")

tests/test_script_content_reader.py

+4
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ def create_script_metadata(
4848
"# code_embedder:A start\n"
4949
'print("Printing only section A")\n'
5050
"# code_embedder:A end\n"
51+
"\n"
52+
"# code_embedder:B start\n"
53+
'print("Printing only section B")\n'
54+
"# code_embedder:B end\n"
5155
),
5256
),
5357
),

0 commit comments

Comments
 (0)