Implement Edit Feature #28
3 changed files with 62 additions and 23 deletions
|
|
@ -1,14 +1,16 @@
|
||||||
import glob
|
import glob
|
||||||
import os
|
import os
|
||||||
import typer
|
from datetime import datetime
|
||||||
|
from shutil import move
|
||||||
|
from typing import Generator
|
||||||
|
|
||||||
import click
|
import click
|
||||||
|
import typer
|
||||||
from rich import print
|
from rich import print
|
||||||
from rich.markdown import Markdown
|
from rich.markdown import Markdown
|
||||||
from rich.panel import Panel
|
from rich.panel import Panel
|
||||||
from shutil import move
|
|
||||||
|
|
||||||
from datetime import datetime
|
|
||||||
|
|
||||||
|
from streamer.parse.attach_markdown import StreamFileWithMarkdown, attach_markdown
|
||||||
from streamer.parse.parse import parse_markdown_file
|
from streamer.parse.parse import parse_markdown_file
|
||||||
from streamer.query.task import find_by_markers
|
from streamer.query.task import find_by_markers
|
||||||
from streamer.settings import Settings
|
from streamer.settings import Settings
|
||||||
|
|
@ -16,28 +18,28 @@ from streamer.settings import Settings
|
||||||
app = typer.Typer()
|
app = typer.Typer()
|
||||||
|
|
||||||
|
|
||||||
@app.command()
|
def all_files() -> Generator[StreamFileWithMarkdown]:
|
||||||
def todo() -> None:
|
|
||||||
for file_name in glob.glob(f"{glob.escape(Settings().base_folder)}/*.md"):
|
for file_name in glob.glob(f"{glob.escape(Settings().base_folder)}/*.md"):
|
||||||
with open(file_name, "r") as file:
|
with open(file_name, "r") as file:
|
||||||
file_content = file.read()
|
file_content = file.read()
|
||||||
sharded_document = parse_markdown_file(file_name, file_content)
|
yield attach_markdown(
|
||||||
if sharded_document.shard:
|
parse_markdown_file(file_name, file_content), file_content
|
||||||
open_tasks = find_by_markers(sharded_document.shard, ["Task"], ["Done"])
|
)
|
||||||
|
|
||||||
for task_shard in open_tasks:
|
|
||||||
print(
|
@app.command()
|
||||||
Panel(
|
def todo() -> None:
|
||||||
Markdown(
|
for sharded_document in all_files():
|
||||||
"\n".join(
|
if sharded_document.shard:
|
||||||
file_content.splitlines()[
|
open_tasks = find_by_markers(sharded_document.shard, ["Task"], ["Done"])
|
||||||
task_shard.start_line - 1 : task_shard.end_line
|
|
||||||
]
|
for task_shard in open_tasks:
|
||||||
)
|
print(
|
||||||
),
|
Panel(
|
||||||
title=f"{file_name}:{task_shard.start_line}",
|
Markdown(task_shard.markdown_content),
|
||||||
)
|
title=f"{sharded_document.filename}:{task_shard.start_line}",
|
||||||
)
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@app.command()
|
@app.command()
|
||||||
|
|
@ -59,7 +61,9 @@ def new() -> None:
|
||||||
parsed_content = parse_markdown_file(prelimary_path, content)
|
parsed_content = parse_markdown_file(prelimary_path, content)
|
||||||
|
|
||||||
final_file_name = f"{timestamp}.md"
|
final_file_name = f"{timestamp}.md"
|
||||||
if parsed_content.shard is not None and len(markers := parsed_content.shard.markers):
|
if parsed_content.shard is not None and len(
|
||||||
|
markers := parsed_content.shard.markers
|
||||||
|
):
|
||||||
final_file_name = f"{timestamp} {' '.join(markers)}.md"
|
final_file_name = f"{timestamp} {' '.join(markers)}.md"
|
||||||
|
|
||||||
final_path = os.path.join(streamer_directory, final_file_name)
|
final_path = os.path.join(streamer_directory, final_file_name)
|
||||||
|
|
|
||||||
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"]
|
||||||
|
|
@ -1,7 +1,11 @@
|
||||||
|
from typing import TypeVar
|
||||||
|
|
||||||
from streamer.parse.shard import Shard
|
from streamer.parse.shard import Shard
|
||||||
|
|
||||||
|
T = TypeVar("T", bound="Shard")
|
||||||
|
|
||||||
def find_by_markers(shard: Shard, has: list[str], has_not: list[str]) -> list[Shard]:
|
|
||||||
|
def find_by_markers(shard: T, has: list[str], has_not: list[str]) -> list[T]:
|
||||||
found_shards = []
|
found_shards = []
|
||||||
|
|
||||||
if any(tag in has for tag in shard.markers) and not any(
|
if any(tag in has for tag in shard.markers) and not any(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue