Use frontmatter also for formatting
This commit is contained in:
parent
f448ee5eb8
commit
b8ecb5da25
|
@ -13,10 +13,11 @@ class CardContents:
|
||||||
tags: list[str]
|
tags: list[str]
|
||||||
assignments: list[str]
|
assignments: list[str]
|
||||||
card_dependencies: list[str]
|
card_dependencies: list[str]
|
||||||
|
url: str
|
||||||
|
|
||||||
|
|
||||||
def format_obsidian_link(text: str) -> str:
|
def format_obsidian_link(text: str) -> str:
|
||||||
return f'"[[{text}]]"'
|
return f'[[{text}]]'
|
||||||
|
|
||||||
|
|
||||||
def parse_obsidian_link(text: str) -> str:
|
def parse_obsidian_link(text: str) -> str:
|
||||||
|
@ -34,8 +35,6 @@ class CardFileFormatter:
|
||||||
self.renderer = marko.md_renderer.MarkdownRenderer()
|
self.renderer = marko.md_renderer.MarkdownRenderer()
|
||||||
|
|
||||||
def format_card_contents(self, card: CardContents) -> str:
|
def format_card_contents(self, card: CardContents) -> str:
|
||||||
ls = []
|
|
||||||
|
|
||||||
# Choose frontmatter data
|
# Choose frontmatter data
|
||||||
frontmatter_data = {}
|
frontmatter_data = {}
|
||||||
if card.name and self.obsidian_mode:
|
if card.name and self.obsidian_mode:
|
||||||
|
@ -57,19 +56,8 @@ class CardFileFormatter:
|
||||||
for name in frontmatter_data['dependencies']
|
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
|
# Card name
|
||||||
|
ls = []
|
||||||
if card.name:
|
if card.name:
|
||||||
ls.append('# ')
|
ls.append('# ')
|
||||||
ls.append(card.name)
|
ls.append(card.name)
|
||||||
|
@ -78,7 +66,8 @@ class CardFileFormatter:
|
||||||
# Card contents
|
# Card contents
|
||||||
if card.description:
|
if card.description:
|
||||||
ls.append(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:
|
def parse_card_contents(self, contents: str) -> CardContents:
|
||||||
"""
|
"""
|
||||||
|
@ -112,4 +101,5 @@ class CardFileFormatter:
|
||||||
tags=tags,
|
tags=tags,
|
||||||
assignments=assignments,
|
assignments=assignments,
|
||||||
card_dependencies=card_dependencies,
|
card_dependencies=card_dependencies,
|
||||||
|
url='', # TODO
|
||||||
)
|
)
|
||||||
|
|
|
@ -21,14 +21,14 @@ EXAMPLE_TEXT_2 = """
|
||||||
---
|
---
|
||||||
aliases:
|
aliases:
|
||||||
- Name of Card
|
- Name of Card
|
||||||
|
assignments:
|
||||||
|
- '[[Gunnar Gunnarson]]'
|
||||||
|
- '[[Alice Alicedottor]]'
|
||||||
|
dependencies:
|
||||||
|
- '[[PAR-9999]]'
|
||||||
tags:
|
tags:
|
||||||
- tag1
|
- tag1
|
||||||
- tag2
|
- tag2
|
||||||
assignments:
|
|
||||||
- "[[Gunnar Gunnarson]]"
|
|
||||||
- "[[Alice Alicedottor]]"
|
|
||||||
dependencies:
|
|
||||||
- "[[PAR-9999]]"
|
|
||||||
---
|
---
|
||||||
|
|
||||||
# Name of Card
|
# Name of Card
|
||||||
|
@ -36,6 +36,17 @@ dependencies:
|
||||||
Great news everyone! My weird program actually works!
|
Great news everyone! My weird program actually works!
|
||||||
""".strip()
|
""".strip()
|
||||||
|
|
||||||
|
EXAMPLE_TEXT_3 = """
|
||||||
|
---
|
||||||
|
aliases:
|
||||||
|
- 'Card: The Adventure of Card'
|
||||||
|
---
|
||||||
|
|
||||||
|
# Card: The Adventure of Card
|
||||||
|
|
||||||
|
Description of Card.
|
||||||
|
""".strip()
|
||||||
|
|
||||||
FORMATTER = CardFileFormatter()
|
FORMATTER = CardFileFormatter()
|
||||||
|
|
||||||
|
|
||||||
|
@ -54,3 +65,11 @@ def test_parse_and_render_2():
|
||||||
assert card_contents.name == 'Name of Card'
|
assert card_contents.name == 'Name of Card'
|
||||||
assert '---' not in card_contents.description
|
assert '---' not in card_contents.description
|
||||||
assert FORMATTER.format_card_contents(card_contents) == EXAMPLE_TEXT_2
|
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
|
||||||
|
|
Loading…
Reference in New Issue
Block a user