If no published date is available on the headers, infer it from the file creation date
This commit is contained in:
parent
672258c677
commit
dcc935c4cb
1 changed files with 19 additions and 9 deletions
|
@ -46,7 +46,8 @@ class BlogApp(Flask):
|
|||
abort(404)
|
||||
|
||||
metadata = {}
|
||||
with open(os.path.join(self.pages_dir, page), 'r') as f:
|
||||
md_file = os.path.join(self.pages_dir, page)
|
||||
with open(md_file, 'r') as f:
|
||||
metadata['uri'] = '/article/' + page[:-3]
|
||||
|
||||
for line in f.readlines():
|
||||
|
@ -61,6 +62,11 @@ class BlogApp(Flask):
|
|||
else:
|
||||
metadata[m.group(1)] = m.group(2)
|
||||
|
||||
if not metadata.get('published'):
|
||||
# If the `published` header isn't available in the file,
|
||||
# infer it from the file's creation date
|
||||
metadata['published'] = datetime.date.fromtimestamp(os.stat(md_file).st_ctime)
|
||||
|
||||
return metadata
|
||||
|
||||
def get_page(self, page: str, title: Optional[str] = None, skip_header: bool = False):
|
||||
|
@ -84,14 +90,18 @@ class BlogApp(Flask):
|
|||
)
|
||||
|
||||
def get_pages(self, with_content: bool = False, skip_header: bool = False) -> list:
|
||||
return sorted([
|
||||
return sorted(
|
||||
[
|
||||
{
|
||||
'path': path[len(app.pages_dir)+1:],
|
||||
'content': self.get_page(path[len(app.pages_dir)+1:], skip_header=skip_header) if with_content else '',
|
||||
**self.get_page_metadata(os.path.basename(path)),
|
||||
}
|
||||
for path in glob(os.path.join(app.pages_dir, '*.md'))
|
||||
], key=lambda page: page.get('published'), reverse=True)
|
||||
],
|
||||
key=lambda page: page.get('published', datetime.date.fromtimestamp(0)),
|
||||
reverse=True
|
||||
)
|
||||
|
||||
|
||||
app = BlogApp(__name__)
|
||||
|
|
Loading…
Reference in a new issue