Some refactorization

This commit is contained in:
faildev_mode 2023-05-30 22:08:53 +02:00
parent bdc705c56f
commit 3f285fcd5c
No known key found for this signature in database
GPG Key ID: 70845C70C0F5E205
3 changed files with 15 additions and 13 deletions

View File

@ -36,5 +36,5 @@ class Content:
"""Iterator of Item objects representing files in specified directory (like os.scandir)"""
for entry in os.scandir('content/'+dir):
item = Item(entry.path)
item = Item(os.path.relpath(entry.path, start='content'))
if item.title: yield item

View File

@ -14,6 +14,7 @@ import re
from functools import partial
import apis # from itself
import traceback
from item import Item # from itself
def link_this(src: str, dest: str):
"""Creates symlink to source at destination
@ -158,16 +159,9 @@ if __name__ == '__main__':
print(path)
# load content file
with open('content/'+path) as fp:
frontmatter_data, content = frontmatter.parse(fp.read())
if 'created' in frontmatter_data:
# frontmatter produces date objects if created field is in format yyyy-mm-dd
# if it contains time as in ISO 8601, we want to convert it to datetime
# https://en.wikipedia.org/wiki/ISO_8601
if type(frontmatter_data['created']) == str:
frontmatter_data['created'] = datetime.datetime.fromisoformat(
frontmatter_data['created'])
item = Item(path)
frontmatter_data = item.frontmatter_data
content = item.content
# evaluate {python code} in braces in content files, pass frontmatter_data to the function
content = re.sub(r'\{([^\s}]+([^}]*[^\s}])?)\}',
@ -181,5 +175,9 @@ if __name__ == '__main__':
template_cache[template] = fp.read()
template_data = template_cache[template]
# TODO: evaluate and apply template
# TODO: do redirections
# TODO: produce HTML version
# save results
save_this('../'+path, content)

View File

@ -7,6 +7,8 @@ class Item:
It extracts some pre-defined frontmatter fields using python-frontmatter module"""
content = ''
frontmatter_data = {}
title = None
tags = []
description = None
@ -16,9 +18,9 @@ class Item:
author = None
def __init__(self, path):
self.path = os.path.relpath(path, start='content')
self.path = path
with open(path) as fp:
with open('content/'+path) as fp:
frontmatter_data, content = frontmatter.parse(fp.read())
self.content = content
@ -34,6 +36,8 @@ class Item:
else:
self.date = str(frontmatter_data['created'])
self.frontmatter_data = frontmatter_data
if 'title' in frontmatter_data:
self.title = frontmatter_data['title']