New API function: dir, class Item
This commit is contained in:
parent
42fb70b282
commit
493c82bc05
11
_src/apis.py
11
_src/apis.py
|
@ -1,15 +1,15 @@
|
|||
#!/usr/bin/env python3
|
||||
import os
|
||||
import urllib.parse
|
||||
from item import Item
|
||||
|
||||
class Content:
|
||||
def log(dest: str) -> str:
|
||||
return 'gemlog/'+dest+'.gmi'
|
||||
|
||||
def article(dest):
|
||||
def article(dest: str) -> str:
|
||||
return 'articles/'+dest+'.gmi'
|
||||
|
||||
def res(dest, label=None):
|
||||
def res(dest: str, label=None) -> str:
|
||||
path = os.path.splitext(environment['path'])[0]
|
||||
path += '/'+dest
|
||||
|
||||
|
@ -22,3 +22,8 @@ class Content:
|
|||
return path+' '+label
|
||||
else:
|
||||
return path
|
||||
|
||||
def dir(dir: str):
|
||||
for entry in os.scandir('content/'+dir):
|
||||
item = Item(entry.path)
|
||||
if item.title: yield item
|
||||
|
|
|
@ -96,7 +96,7 @@ def evaluate_content(match: re.Match, frontmatter_data: dict, path: str) -> str:
|
|||
|
||||
# prepare globals
|
||||
global_ns = {
|
||||
'__builtins__': {}
|
||||
'__builtins__': {'sorted': sorted}
|
||||
}
|
||||
|
||||
extension = vars(apis.Content)
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
import os
|
||||
import frontmatter # from package python-frontmatter
|
||||
import datetime
|
||||
|
||||
class Item:
|
||||
content = ''
|
||||
title = None
|
||||
tags = []
|
||||
description = None
|
||||
source = None
|
||||
date = None
|
||||
time = None
|
||||
author = None
|
||||
|
||||
def __init__(self, path):
|
||||
self.path = os.path.relpath(path, start='content')
|
||||
|
||||
with open(path) as fp:
|
||||
frontmatter_data, content = frontmatter.parse(fp.read())
|
||||
self.content = content
|
||||
|
||||
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'])
|
||||
self.date = str(frontmatter_data['created'].date())
|
||||
self.time = str(frontmatter_data['created'].time())
|
||||
else:
|
||||
self.date = str(frontmatter_data['created'])
|
||||
|
||||
if 'title' in frontmatter_data:
|
||||
self.title = frontmatter_data['title']
|
||||
|
||||
if 'tags' in frontmatter_data:
|
||||
self.tags = frontmatter_data['tags']
|
||||
|
||||
if 'description' in frontmatter_data:
|
||||
self.description = frontmatter_data['description']
|
||||
|
||||
if 'source' in frontmatter_data:
|
||||
self.source = frontmatter_data['source']
|
||||
|
||||
if 'author' in frontmatter_data:
|
||||
self.author = frontmatter_data['author']
|
Reference in New Issue