2024-09-27 14:13:03 +00:00
|
|
|
import dataclasses
|
2024-10-01 11:15:49 +00:00
|
|
|
import datetime
|
2024-10-01 14:15:03 +00:00
|
|
|
import re
|
2024-09-28 12:13:51 +00:00
|
|
|
|
2024-09-27 14:41:26 +00:00
|
|
|
import frontmatter
|
2024-09-27 14:13:03 +00:00
|
|
|
import marko
|
|
|
|
import marko.md_renderer
|
|
|
|
|
2024-10-01 09:50:33 +00:00
|
|
|
################################################################################
|
|
|
|
# FrontMatter keys
|
|
|
|
|
|
|
|
FM_KEY_TODO_LIST_COMPLETED = 'todo-list-completed'
|
|
|
|
FM_KEY_TAGS = 'tags'
|
|
|
|
FM_KEY_ASSIGNMENTS = 'assignments'
|
|
|
|
FM_KEY_DEPENDENCIES = 'dependencies'
|
|
|
|
FM_KEY_URL = 'url'
|
|
|
|
FM_KEY_ALIASES = 'aliases'
|
2024-10-01 11:15:49 +00:00
|
|
|
FM_KEY_ARCHIVED = 'archived'
|
|
|
|
FM_KEY_DUE_DATE = 'due'
|
|
|
|
FM_KEY_START_DATE = 'start-date'
|
2024-10-01 09:50:33 +00:00
|
|
|
|
|
|
|
################################################################################
|
2024-09-27 14:13:03 +00:00
|
|
|
|
2024-10-01 14:14:51 +00:00
|
|
|
|
2024-09-27 14:13:03 +00:00
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
|
|
class CardContents:
|
2024-10-01 09:12:17 +00:00
|
|
|
identifier: str | None
|
2024-09-28 10:37:19 +00:00
|
|
|
name: str | None
|
|
|
|
description: str | None
|
|
|
|
tags: list[str]
|
|
|
|
assignments: list[str]
|
2024-09-28 12:10:13 +00:00
|
|
|
card_dependencies: list[str]
|
2024-09-28 12:27:59 +00:00
|
|
|
url: str
|
2024-10-01 09:50:33 +00:00
|
|
|
todo_list_completed: bool | None
|
2024-10-02 08:31:42 +00:00
|
|
|
is_archived: bool
|
2024-10-01 11:15:49 +00:00
|
|
|
start_date: datetime.date | None
|
|
|
|
due_date: datetime.date | None
|
2024-10-02 11:34:49 +00:00
|
|
|
custom_fields: dict[str,str]
|
2024-09-28 12:10:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
def format_obsidian_link(text: str) -> str:
|
2024-09-28 12:27:59 +00:00
|
|
|
return f'[[{text}]]'
|
2024-09-28 12:10:13 +00:00
|
|
|
|
2024-09-28 12:13:51 +00:00
|
|
|
|
2024-09-28 12:10:13 +00:00
|
|
|
def parse_obsidian_link(text: str) -> str:
|
|
|
|
if m := re.match(r'^\[\[(.*)\]\]$', text):
|
|
|
|
return m.group(1)
|
|
|
|
return text
|
2024-09-27 14:13:03 +00:00
|
|
|
|
2024-09-28 10:54:34 +00:00
|
|
|
|
2024-09-28 10:37:19 +00:00
|
|
|
class CardFileFormatter:
|
2024-09-28 10:53:34 +00:00
|
|
|
"""Component for formatting and parsing card files."""
|
2024-09-27 14:13:03 +00:00
|
|
|
|
2024-09-28 10:54:34 +00:00
|
|
|
def __init__(self, obsidian_mode=True):
|
2024-10-01 09:28:44 +00:00
|
|
|
"""Initialize card formatter.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
- `obsidian_mode`: Configure formatter to enable maximum compatibility
|
|
|
|
with [Obsidian](https://obsidian.md/). Enables internal links, and
|
|
|
|
exposes certain aliases.
|
|
|
|
"""
|
2024-09-28 10:37:19 +00:00
|
|
|
self.obsidian_mode = obsidian_mode
|
|
|
|
self.markdown = marko.Markdown()
|
|
|
|
self.renderer = marko.md_renderer.MarkdownRenderer()
|
2024-09-27 14:41:26 +00:00
|
|
|
|
2024-09-28 10:37:19 +00:00
|
|
|
def format_card_contents(self, card: CardContents) -> str:
|
2024-10-01 09:28:44 +00:00
|
|
|
"""Formats card contents. Mostly the inverse of [`parse_card_contents`]."""
|
2024-09-28 10:37:19 +00:00
|
|
|
# Choose frontmatter data
|
|
|
|
frontmatter_data = {}
|
2024-10-01 09:12:17 +00:00
|
|
|
if self.obsidian_mode:
|
|
|
|
aliases = []
|
|
|
|
if card.name:
|
|
|
|
aliases.append(card.name)
|
|
|
|
if card.identifier and card.name:
|
|
|
|
aliases.append(f'{card.identifier}: {card.name}')
|
|
|
|
if aliases:
|
2024-10-01 09:50:33 +00:00
|
|
|
frontmatter_data[FM_KEY_ALIASES] = aliases
|
2024-10-01 09:12:17 +00:00
|
|
|
del aliases
|
2024-10-02 08:18:12 +00:00
|
|
|
if len(card.tags) > 0:
|
2024-10-01 09:50:33 +00:00
|
|
|
frontmatter_data[FM_KEY_TAGS] = card.tags
|
2024-10-02 08:18:12 +00:00
|
|
|
if card.url is not None and self.obsidian_mode:
|
2024-10-01 09:50:33 +00:00
|
|
|
frontmatter_data[FM_KEY_URL] = card.url
|
2024-10-02 08:18:12 +00:00
|
|
|
if len(card.assignments) > 0:
|
2024-10-01 09:50:33 +00:00
|
|
|
frontmatter_data[FM_KEY_ASSIGNMENTS] = card.assignments
|
2024-09-28 10:37:19 +00:00
|
|
|
if self.obsidian_mode:
|
2024-10-01 09:50:33 +00:00
|
|
|
frontmatter_data[FM_KEY_ASSIGNMENTS] = [
|
2024-09-28 12:13:51 +00:00
|
|
|
format_obsidian_link(name)
|
2024-10-01 09:50:33 +00:00
|
|
|
for name in frontmatter_data[FM_KEY_ASSIGNMENTS]
|
2024-09-28 12:10:13 +00:00
|
|
|
]
|
2024-10-02 08:18:12 +00:00
|
|
|
if len(card.card_dependencies) > 0:
|
2024-10-01 09:50:33 +00:00
|
|
|
frontmatter_data[FM_KEY_DEPENDENCIES] = card.card_dependencies
|
2024-09-28 12:10:13 +00:00
|
|
|
if self.obsidian_mode:
|
2024-10-01 09:50:33 +00:00
|
|
|
frontmatter_data[FM_KEY_DEPENDENCIES] = [
|
2024-09-28 12:13:51 +00:00
|
|
|
format_obsidian_link(name)
|
2024-10-01 09:50:33 +00:00
|
|
|
for name in frontmatter_data[FM_KEY_DEPENDENCIES]
|
2024-09-28 10:54:34 +00:00
|
|
|
]
|
2024-10-02 08:18:12 +00:00
|
|
|
if card.todo_list_completed is not None:
|
2024-10-01 09:50:33 +00:00
|
|
|
frontmatter_data[FM_KEY_TODO_LIST_COMPLETED] = card.todo_list_completed
|
2024-10-02 08:31:42 +00:00
|
|
|
if card.is_archived is not None:
|
|
|
|
frontmatter_data[FM_KEY_ARCHIVED] = card.is_archived
|
2024-10-02 08:18:12 +00:00
|
|
|
if card.due_date is not None:
|
2024-10-01 11:15:49 +00:00
|
|
|
frontmatter_data[FM_KEY_DUE_DATE] = card.due_date
|
2024-10-02 08:18:12 +00:00
|
|
|
if card.start_date is not None:
|
2024-10-01 11:15:49 +00:00
|
|
|
frontmatter_data[FM_KEY_DUE_DATE] = card.start_date
|
2024-10-02 11:54:30 +00:00
|
|
|
if len(card.custom_fields) > 0:
|
|
|
|
frontmatter_data |= card.custom_fields
|
2024-09-28 10:37:19 +00:00
|
|
|
|
|
|
|
# Card name
|
2024-09-28 12:27:59 +00:00
|
|
|
ls = []
|
2024-09-28 10:37:19 +00:00
|
|
|
if card.name:
|
|
|
|
ls.append('# ')
|
|
|
|
ls.append(card.name)
|
|
|
|
ls.append('\n\n')
|
2024-09-27 14:41:26 +00:00
|
|
|
|
2024-09-28 10:37:19 +00:00
|
|
|
# Card contents
|
2024-10-01 09:28:44 +00:00
|
|
|
if description := card.description:
|
|
|
|
if self.obsidian_mode:
|
2024-10-01 14:14:51 +00:00
|
|
|
description = re.sub(
|
2024-10-01 14:15:03 +00:00
|
|
|
r'\-\s*\[\s*\]',
|
|
|
|
'- [ ]',
|
|
|
|
description,
|
|
|
|
flags=re.MULTILINE,
|
2024-10-01 14:14:51 +00:00
|
|
|
)
|
2024-10-01 09:28:44 +00:00
|
|
|
ls.append(description)
|
|
|
|
del description
|
2024-09-28 12:27:59 +00:00
|
|
|
fm = frontmatter.Post(''.join(ls), **frontmatter_data)
|
2024-10-01 09:28:44 +00:00
|
|
|
|
2024-09-28 12:27:59 +00:00
|
|
|
return frontmatter.dumps(fm)
|
2024-09-27 14:13:03 +00:00
|
|
|
|
2024-09-28 10:37:19 +00:00
|
|
|
def parse_card_contents(self, contents: str) -> CardContents:
|
2024-10-01 09:28:44 +00:00
|
|
|
"""Parses card contents. Mostly the inverse of [`parse_card_contents`].
|
2024-10-01 09:12:17 +00:00
|
|
|
|
|
|
|
1. Strips frontmatter and parses certain fields from the header.
|
2024-09-28 10:37:19 +00:00
|
|
|
2. Parses header
|
|
|
|
3. Finds content.
|
|
|
|
"""
|
|
|
|
fm = frontmatter.loads(contents)
|
|
|
|
del contents
|
2024-09-27 14:41:26 +00:00
|
|
|
|
2024-09-28 10:37:19 +00:00
|
|
|
document = self.markdown.parse(fm.content.strip())
|
|
|
|
name = None
|
|
|
|
for elem in document.children:
|
|
|
|
if isinstance(elem, marko.block.Heading):
|
|
|
|
name = self.renderer.render_children(elem)
|
|
|
|
document.children.remove(elem)
|
|
|
|
break
|
2024-09-27 14:13:03 +00:00
|
|
|
|
2024-10-01 09:50:33 +00:00
|
|
|
tags: list[str] = fm.metadata.get(FM_KEY_TAGS, [])
|
2024-09-28 12:10:13 +00:00
|
|
|
|
2024-10-01 09:50:33 +00:00
|
|
|
assignments: list[str] = fm.metadata.get(FM_KEY_ASSIGNMENTS, [])
|
2024-09-28 12:10:13 +00:00
|
|
|
assignments = [parse_obsidian_link(text) for text in assignments]
|
|
|
|
|
2024-10-01 09:50:33 +00:00
|
|
|
card_dependencies: list[str] = fm.metadata.get(FM_KEY_DEPENDENCIES, [])
|
2024-09-28 12:13:51 +00:00
|
|
|
card_dependencies = [parse_obsidian_link(text) for text in card_dependencies]
|
2024-09-28 11:21:11 +00:00
|
|
|
|
2024-10-01 09:50:33 +00:00
|
|
|
url: list[str] = fm.metadata.get(FM_KEY_URL)
|
|
|
|
todo_list_completed: bool | None = fm.metadata.get(FM_KEY_TODO_LIST_COMPLETED)
|
2024-10-02 08:31:42 +00:00
|
|
|
is_archived: bool = fm.metadata.get(FM_KEY_ARCHIVED)
|
2024-10-01 11:15:49 +00:00
|
|
|
|
|
|
|
start_date: datetime.date = fm.metadata.get(FM_KEY_START_DATE)
|
|
|
|
due_date: datetime.date = fm.metadata.get(FM_KEY_DUE_DATE)
|
2024-09-30 11:57:34 +00:00
|
|
|
|
2024-09-28 10:37:19 +00:00
|
|
|
description = self.renderer.render_children(document).strip()
|
|
|
|
return CardContents(
|
2024-10-01 09:12:17 +00:00
|
|
|
None,
|
2024-09-28 10:54:34 +00:00
|
|
|
name,
|
|
|
|
description,
|
2024-09-28 12:10:13 +00:00
|
|
|
tags=tags,
|
2024-09-28 11:21:11 +00:00
|
|
|
assignments=assignments,
|
2024-09-28 12:10:13 +00:00
|
|
|
card_dependencies=card_dependencies,
|
2024-09-30 11:57:34 +00:00
|
|
|
url=url,
|
2024-10-01 09:50:33 +00:00
|
|
|
todo_list_completed=todo_list_completed,
|
2024-10-02 08:31:42 +00:00
|
|
|
is_archived=is_archived,
|
2024-10-01 11:15:49 +00:00
|
|
|
start_date=start_date,
|
|
|
|
due_date=due_date,
|
2024-10-02 11:54:30 +00:00
|
|
|
custom_fields={}, # TODO
|
2024-09-28 10:37:19 +00:00
|
|
|
)
|