1
0

Use frontmatter also for formatting

This commit is contained in:
Jon Michael Aanes 2024-09-28 14:27:59 +02:00
parent f448ee5eb8
commit b8ecb5da25
Signed by: Jmaa
SSH Key Fingerprint: SHA256:Ab0GfHGCblESJx7JRE4fj4bFy/KRpeLhi41y4pF3sNA
2 changed files with 33 additions and 24 deletions

View File

@ -13,10 +13,11 @@ class CardContents:
tags: list[str]
assignments: list[str]
card_dependencies: list[str]
url: str
def format_obsidian_link(text: str) -> str:
return f'"[[{text}]]"'
return f'[[{text}]]'
def parse_obsidian_link(text: str) -> str:
@ -34,8 +35,6 @@ class CardFileFormatter:
self.renderer = marko.md_renderer.MarkdownRenderer()
def format_card_contents(self, card: CardContents) -> str:
ls = []
# Choose frontmatter data
frontmatter_data = {}
if card.name and self.obsidian_mode:
@ -57,19 +56,8 @@ class CardFileFormatter:
for name in frontmatter_data['dependencies']
]
# Frontmatter
if frontmatter_data:
ls.append('---\n')
for key, values in frontmatter_data.items():
ls.append(key)
ls.append(':\n')
for v in values:
ls.append(' - ')
ls.append(v)
ls.append('\n')
ls.append('---\n\n')
# Card name
ls = []
if card.name:
ls.append('# ')
ls.append(card.name)
@ -78,7 +66,8 @@ class CardFileFormatter:
# Card contents
if card.description:
ls.append(card.description)
return ''.join(ls)
fm = frontmatter.Post(''.join(ls), **frontmatter_data)
return frontmatter.dumps(fm)
def parse_card_contents(self, contents: str) -> CardContents:
"""
@ -112,4 +101,5 @@ class CardFileFormatter:
tags=tags,
assignments=assignments,
card_dependencies=card_dependencies,
url='', # TODO
)

View File

@ -3,7 +3,7 @@ from favro_sync.favro_markdown import CardFileFormatter
EXAMPLE_TEXT_1 = """
---
aliases:
- Hello World
- Hello World
---
# Hello World
@ -20,15 +20,15 @@ Test description
EXAMPLE_TEXT_2 = """
---
aliases:
- Name of Card
tags:
- tag1
- tag2
- Name of Card
assignments:
- "[[Gunnar Gunnarson]]"
- "[[Alice Alicedottor]]"
- '[[Gunnar Gunnarson]]'
- '[[Alice Alicedottor]]'
dependencies:
- "[[PAR-9999]]"
- '[[PAR-9999]]'
tags:
- tag1
- tag2
---
# Name of Card
@ -36,6 +36,17 @@ dependencies:
Great news everyone! My weird program actually works!
""".strip()
EXAMPLE_TEXT_3 = """
---
aliases:
- 'Card: The Adventure of Card'
---
# Card: The Adventure of Card
Description of Card.
""".strip()
FORMATTER = CardFileFormatter()
@ -54,3 +65,11 @@ def test_parse_and_render_2():
assert card_contents.name == 'Name of Card'
assert '---' not in card_contents.description
assert FORMATTER.format_card_contents(card_contents) == EXAMPLE_TEXT_2
def test_parse_and_render_3():
card_contents = FORMATTER.parse_card_contents(EXAMPLE_TEXT_3)
print(card_contents)
assert card_contents.name == 'Card: The Adventure of Card'
assert '---' not in card_contents.description
assert FORMATTER.format_card_contents(card_contents) == EXAMPLE_TEXT_3