feat: add all_files iterator to simplify searching
Signed-off-by: Konstantin Fickel <mail@konstantinfickel.de>
This commit is contained in:
parent
1ce0790c0c
commit
ee91b2e8db
3 changed files with 62 additions and 23 deletions
31
src/streamer/parse/attach_markdown.py
Normal file
31
src/streamer/parse/attach_markdown.py
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
from streamer.parse.shard import Shard, StreamFile
|
||||
|
||||
|
||||
class ShardWithMarkdown(Shard):
|
||||
children: list[ShardWithMarkdown]
|
||||
markdown_content: str
|
||||
|
||||
|
||||
class StreamFileWithMarkdown(StreamFile):
|
||||
shard: ShardWithMarkdown | None = None # pyright: ignore[reportIncompatibleVariableOverride]
|
||||
|
||||
|
||||
def attach_markdown_shard(shard: Shard, markdown_text: str) -> ShardWithMarkdown:
|
||||
lines = markdown_text.splitlines()
|
||||
markdown_content = "\n".join(lines[shard.start_line - 1 : shard.end_line])
|
||||
return ShardWithMarkdown(
|
||||
**shard.model_dump(exclude=["children"]),
|
||||
children=map(lambda child: attach_markdown_shard(child, markdown_text), shard.children),
|
||||
markdown_content=markdown_content,
|
||||
)
|
||||
|
||||
|
||||
def attach_markdown(file: StreamFile, markdown_text: str) -> StreamFileWithMarkdown:
|
||||
if file.shard is None:
|
||||
return StreamFileWithMarkdown(filename=file.filename, shard=None)
|
||||
|
||||
attached_shard = attach_markdown_shard(file.shard, markdown_text)
|
||||
return StreamFileWithMarkdown(filename=file.filename, shard=attached_shard)
|
||||
|
||||
|
||||
__all__ = ["attach_markdown"]
|
||||
Loading…
Add table
Add a link
Reference in a new issue