If no published date is available on the headers, infer it from the file creation date

This commit is contained in:
Fabio Manganiello 2022-06-11 23:02:07 +02:00
parent 672258c677
commit dcc935c4cb

View file

@ -46,7 +46,8 @@ class BlogApp(Flask):
abort(404) abort(404)
metadata = {} 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] metadata['uri'] = '/article/' + page[:-3]
for line in f.readlines(): for line in f.readlines():
@ -61,6 +62,11 @@ class BlogApp(Flask):
else: else:
metadata[m.group(1)] = m.group(2) 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 return metadata
def get_page(self, page: str, title: Optional[str] = None, skip_header: bool = False): 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: def get_pages(self, with_content: bool = False, skip_header: bool = False) -> list:
return sorted([ return sorted(
[
{ {
'path': path[len(app.pages_dir)+1:], '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 '', '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)), **self.get_page_metadata(os.path.basename(path)),
} }
for path in glob(os.path.join(app.pages_dir, '*.md')) 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__) app = BlogApp(__name__)