Skip to content

Commit cd32663

Browse files
committed
Fix quoted snippets validation
1 parent 8f6152a commit cd32663

3 files changed

Lines changed: 28 additions & 10 deletions

File tree

lectures/move_semantics.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ It allocates this memory using some magic function `std::byte* AllocateMemory(st
9494
> At this point it is not important how exactly the memory allocation happens. We will talk about it in the future. We just have to remember that allocating, copying and freeing memory are all time-wise costly operations.
9595
> <!-- For the impatient, you can see example of such functions in the script, which is as always linked in the description below -->
9696
> But for the impatient, here is one way to allocate the memory that we need in real code.
97+
> <!--
98+
> `CPP_SKIP_SNIPPET`
99+
> -->
97100
> ```cpp
98101
> // 😱 Please don't do this in real code, for illustration purposes only!
99102
> // 💡 We will talk about how to properly allocate and free memory later!

lectures/object_lifecycle.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ public:
9696
9797
<!-- Talking head -->
9898
> :bulb: Note that when such default constructor is called it will leave the data uninitialized unless these data are initialized in-place:
99-
> <!-- B-roll add private part code -->
99+
> <!--
100+
> `CPP_SKIP_SNIPPET`
101+
> -->
100102
> ```cpp
101103
> class Foo {
102104
> public:

scripts/validate_code_snippets.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@
2525

2626
# See a playground here: https://regex101.com/r/Tfwjsq/1
2727
REGEX_TEMPLATE = r"""
28-
(?:\s*<!--
29-
(:?\s*`CPP_SETUP_START`\n(?P<setup>[\s\S]*?)\s*`CPP_SETUP_END`\s*)*\s*
30-
(?P<skip>`CPP_SKIP_SNIPPET`)*\s*
31-
(?:`CPP_COPY_SNIPPET`\s*(?P<copy>.*$))*\s*
32-
(?:`CPP_RUN_CMD`\s*(?:CWD:(?P<cwd>[\w/]+))*\s*(?P<cmd>.*$))*\s*
33-
-->\s*)*
34-
[> ]*```(?P<language>\w+)
28+
(?:\s*[> ]*<!--
29+
(:?\s*[> ]*`CPP_SETUP_START`\n(?P<setup>[\s\S]*?)\s*[> ]*`CPP_SETUP_END`\s*)*\s*
30+
(?P<skip>[> ]*`CPP_SKIP_SNIPPET`)*\s*
31+
(?:[> ]*`CPP_COPY_SNIPPET`\s*(?P<copy>.*$))*\s*
32+
(?:[> ]*`CPP_RUN_CMD`\s*(?:CWD:(?P<cwd>[\w/]+))*\s*(?P<cmd>.*$))*\s*
33+
[> ]*-->\s*)*
34+
(?P<quote>[> ]*)```(?P<language>\w+)
3535
(?P<code>[\s\S]*?)\n[> ]*```\s*
3636
"""
3737

@@ -163,6 +163,18 @@ def get_code_start_line(text, span):
163163
break
164164
return line_count
165165

166+
def clean_quoted_code(code, quote):
167+
if not code or not quote or ">" not in quote:
168+
return code
169+
if quote.endswith(" "):
170+
regex = "^" + re.escape(quote[:-1]) + r" ?"
171+
else:
172+
regex = "^" + re.escape(quote)
173+
lines = []
174+
for line in code.split('\n'):
175+
lines.append(re.sub(regex, "", line))
176+
return '\n'.join(lines)
177+
166178
error_count = 0
167179
temp_folder = Path(tempfile.gettempdir())
168180
file_text = file.read_text()
@@ -171,9 +183,10 @@ def get_code_start_line(text, span):
171183
span_start_line = get_start_line_of_span(file_text, span)
172184
code_start_line = span_start_line + get_code_start_line(file_text, span)
173185
found_group_dict = match.groupdict()
186+
quote = found_group_dict.get("quote", "")
174187
skip = found_group_dict["skip"]
175-
setup = found_group_dict["setup"]
176-
code = found_group_dict["code"]
188+
setup = clean_quoted_code(found_group_dict["setup"], quote)
189+
code = clean_quoted_code(found_group_dict["code"], quote)
177190
copy_destination = found_group_dict["copy"]
178191
cmd = found_group_dict["cmd"]
179192
cwd = found_group_dict["cwd"]

0 commit comments

Comments
 (0)