fix multitags
This commit is contained in:
		
							parent
							
								
									8854c26be7
								
							
						
					
					
						commit
						cd3d7466e3
					
				
					 2 changed files with 26 additions and 16 deletions
				
			
		|  | @ -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: 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} | 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} | ||||||
|  |  | ||||||
|  | @ -9,24 +9,29 @@ from rich.console import Console | ||||||
| console = Console() | console = Console() | ||||||
| now = datetime.datetime.now() | 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*") | 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 |     return tag, style_override | ||||||
|     """ |     """ | ||||||
|     if not tag: |     if tag == "by": | ||||||
|         return "", "" |  | ||||||
|     name, val = tag.strip("{}").split(":", 1) |  | ||||||
|     if name == "by": |  | ||||||
|         dval = parser.parse(val) |         dval = parser.parse(val) | ||||||
|         days_left = dval - now |         days_left = dval - now | ||||||
|         style = "red" if days_left.days <= 0 else "yellow" |         style = "red" if days_left.days <= 0 else "yellow" | ||||||
|         return f"by {dval.date()} ({days_left.days})", style |         return f"by {dval.date()} ({days_left.days})", style | ||||||
|     else: |     else: | ||||||
|         return f"{name}:{val}", "" |         return f"{tag}:{val}", "" | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def scan_contents(file: pathlib.Path) -> dict: | def scan_contents(file: pathlib.Path) -> dict: | ||||||
|  | @ -38,17 +43,22 @@ def scan_contents(file: pathlib.Path) -> dict: | ||||||
| def pull_todos(file: pathlib.Path): | def pull_todos(file: pathlib.Path): | ||||||
|     text = file.read_text() |     text = file.read_text() | ||||||
|     todos = ALL_TODO_RE.findall(text) |     todos = ALL_TODO_RE.findall(text) | ||||||
|     for t in todos: |     for todo in todos: | ||||||
|         tags, style = parse_todo_tag(t[2]) |         tag_strs = [] | ||||||
|         if t[0] == "DONE": |         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" |             style = "#999999" | ||||||
|         elif t[0] == "IDEA": |         elif status == "IDEA": | ||||||
|             style = "blue" |             style = "blue" | ||||||
|         yield { |         yield { | ||||||
|             "file": file.name, |             "file": file.name, | ||||||
|             "status": t[0], |             "status": status, | ||||||
|             "description": t[1], |             "description": description, | ||||||
|             "tags": tags, |             "tags": " | ".join(tag_strs), | ||||||
|             "style": style, |             "style": style, | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue