inspect_petri.util
Markdown Collections
MarkdownDoc
One .md file’s parsed frontmatter and body.
class MarkdownDoc(BaseModel)Attributes
idstr-
Filename stem.
bodystr-
Text after the closing
---(stripped). metadatadict[str, Any]-
Parsed YAML frontmatter (empty if the file has none).
tagslist[str]-
The
tagslist from frontmatter, or[].
Methods
- to_markdown
-
Serialize back to frontmatter-markdown text.
def to_markdown(self, *, canary: str | None = BENCHMARK_CANARY) -> strcanarystr | None-
Comment lines prepended inside the YAML frontmatter block so the file is detectable as benchmark data. Defaults to :data:`BENCHMARK_CANARY`; pass
Noneto omit.
- save
-
Write :meth:
to_markdowntopath.def save(self, path: str | Path, *, canary: str | None = BENCHMARK_CANARY) -> Nonepathstr | Path-
Destination
.mdfile path. canarystr | None-
Forwarded to :meth:
to_markdown.
parse_markdown_file
Parse a single frontmatter .md file.
The file may optionally begin with YAML front matter delimited by --- lines. Files without front matter yield an empty metadata dict and the whole file as body.
def parse_markdown_file(path: str | Path) -> MarkdownDocpathstr | Path-
Path to a
.mdfile.
read_markdown_directory
Read all .md files from a directory, optionally filtered by tag.
Files are sorted alphabetically by name for deterministic ordering.
def read_markdown_directory(
directory: str | Path,
*,
tags: Sequence[str] | None = None,
) -> list[MarkdownDoc]directorystr | Path-
Path to a directory containing
.mdfiles. tagsSequence[str] | None-
If given, keep only docs whose frontmatter
tagslist intersects this set.
write_markdown_directory
Write a collection of :class:MarkdownDoc to <directory>/<id>.md.
def write_markdown_directory(
docs: Iterable[MarkdownDoc],
directory: str | Path,
*,
clear: bool = False,
) -> NonedocsIterable[MarkdownDoc]-
Documents to write.
directorystr | Path-
Destination directory (created if missing).
clearbool-
If
True, remove existing*.mdfiles in the directory first so deleted docs don’t linger.