Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions docs/mkdocs/hooks/generate_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ def __post_init__(self):
self.other_files = self.determine_other_files()
self.title = self.determine_title()

@property
def is_code(self) -> bool:
return self.main_file.suffix != ".md"

def determine_main_file(self) -> Path:
"""
Determines the main file in the given path.
Expand Down Expand Up @@ -101,6 +105,12 @@ def determine_other_files(self) -> list[Path]:
return [file for file in self.path.rglob("*") if is_other_file(file)]

def determine_title(self) -> str:
if not self.is_code:
with open(self.main_file) as f:
first_line = f.readline().strip()
match = re.match(r'^#\s+(?P<title>.+)$', first_line)
if match:
return match.group('title')
return fix_case(self.path.stem.replace("_", " ").title())

def generate(self) -> str:
Expand All @@ -110,11 +120,13 @@ def generate(self) -> str:
# Use long code fence to avoid issues with
# included files containing code fences too
code_fence = "``````"
is_code = self.main_file.suffix != ".md"
if is_code:
# Skip the title from md snippets as it's been included above
start_line = 2
if self.is_code:
content += f"{code_fence}{self.main_file.suffix[1:]}\n"
content += f'--8<-- "{self.main_file}"\n'
if is_code:
start_line = 1
content += f'--8<-- "{self.main_file}:{start_line}"\n'
if self.is_code:
content += f"{code_fence}\n"
content += "\n"

Expand Down