From cd3d7466e39622c44f8af77d896ed5dc8a655914 Mon Sep 17 00:00:00 2001 From: James Turk Date: Sat, 27 Jul 2024 01:03:01 -0400 Subject: [PATCH] fix multitags --- README.md | 4 ++-- src/maddog/app.py | 38 ++++++++++++++++++++++++-------------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index cddb021..6760d81 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,8 @@ IDEA: just markdown? todo logic might be useful outside that? TODO: decide on a more permanent name, maddog/md/mddir -TODO: this is an example of a todo due in the past {by:2024-07} +TODO: this is an example of a todo due in the past {by:2024-07-01} TODO: this is an example of a todo with other tags {tag:example} -DONE: this is an example of a todo with other tags {tag:example} {by:2024-01} +DONE: two tags fields and status is done {tag:example} {by:2024-01-01} diff --git a/src/maddog/app.py b/src/maddog/app.py index 4e96d74..4be4108 100644 --- a/src/maddog/app.py +++ b/src/maddog/app.py @@ -9,24 +9,29 @@ from rich.console import Console console = Console() now = datetime.datetime.now() -ALL_TODO_RE = re.compile(r"^(TODO|IDEA|DONE):?\s*([^\{\n]+)(\{.*\})?", re.MULTILINE) +ALL_TODO_RE = re.compile( + r"""^(TODO|IDEA|DONE):? # label starts a line + \s*([^\{\n]+) # body ends at { or newline + (?:\s*(\{.*\}))? # repeated variations of {...} tags + """, + re.MULTILINE | re.VERBOSE, +) + +TAG_SPLIT_RE = re.compile(r"\{([^:]+):([^}]+)\}") TODO_TODO_RE = re.compile(r"^(TODO):?\s*") -def parse_todo_tag(tag) -> tuple[str, str]: +def parse_todo_tag(tag, val) -> tuple[str, str]: """ return tag, style_override """ - if not tag: - return "", "" - name, val = tag.strip("{}").split(":", 1) - if name == "by": + if tag == "by": dval = parser.parse(val) days_left = dval - now style = "red" if days_left.days <= 0 else "yellow" return f"by {dval.date()} ({days_left.days})", style else: - return f"{name}:{val}", "" + return f"{tag}:{val}", "" def scan_contents(file: pathlib.Path) -> dict: @@ -38,17 +43,22 @@ def scan_contents(file: pathlib.Path) -> dict: def pull_todos(file: pathlib.Path): text = file.read_text() todos = ALL_TODO_RE.findall(text) - for t in todos: - tags, style = parse_todo_tag(t[2]) - if t[0] == "DONE": + for todo in todos: + tag_strs = [] + style = "" + status, description, tags = todo + for tag, val in TAG_SPLIT_RE.findall(tags): + ts, style = parse_todo_tag(tag, val) + tag_strs.append(ts) + if status == "DONE": style = "#999999" - elif t[0] == "IDEA": + elif status == "IDEA": style = "blue" yield { "file": file.name, - "status": t[0], - "description": t[1], - "tags": tags, + "status": status, + "description": description, + "tags": " | ".join(tag_strs), "style": style, }