Infer the title of an article/file when not specified in the header metadata

Infer the title from the first line of the file if it's a header,
otherwise use the filename.
This commit is contained in:
Fabio Manganiello 2022-06-11 23:54:18 +02:00
parent 81687c430b
commit 56a271d0b6
2 changed files with 49 additions and 15 deletions

View file

@ -12,6 +12,8 @@ from .latex import MarkdownLatex
class BlogApp(Flask): class BlogApp(Flask):
_title_header_regex = re.compile(r'^#\s*((\[(.*)\])|(.*))')
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, template_folder=config.templates_dir, **kwargs) super().__init__(*args, template_folder=config.templates_dir, **kwargs)
self.pages_dir = os.path.join(config.content_dir, 'markdown') self.pages_dir = os.path.join(config.content_dir, 'markdown')
@ -62,10 +64,27 @@ class BlogApp(Flask):
else: else:
metadata[m.group(1)] = m.group(2) metadata[m.group(1)] = m.group(2)
if not metadata.get('title'):
# If the `title` header isn't available in the file,
# infer it from the first line of the file
with open(md_file, 'r') as f:
header = ''
for line in f.readlines():
header = line
break
metadata['title_inferred'] = True
m = self._title_header_regex.search(header)
if m:
metadata['title'] = m.group(3) or m.group(1)
else:
metadata['title'] = os.path.basename(md_file)
if not metadata.get('published'): if not metadata.get('published'):
# If the `published` header isn't available in the file, # If the `published` header isn't available in the file,
# infer it from the file's creation date # infer it from the file's creation date
metadata['published'] = datetime.date.fromtimestamp(os.stat(md_file).st_ctime) metadata['published'] = datetime.date.fromtimestamp(os.stat(md_file).st_ctime)
metadata['published_inferred'] = True
return metadata return metadata
@ -74,17 +93,30 @@ class BlogApp(Flask):
page = page + '.md' page = page + '.md'
metadata = self.get_page_metadata(page) metadata = self.get_page_metadata(page)
# Don't duplicate the page title if it's been inferred
if not (title or metadata.get('title_inferred')):
title = metadata.get('title', config.title)
with open(os.path.join(self.pages_dir, page), 'r') as f: with open(os.path.join(self.pages_dir, page), 'r') as f:
return render_template( return render_template(
'article.html', 'article.html',
config=config, config=config,
title=title if title else metadata.get('title', config.title), title=title,
image=metadata.get('image'), image=metadata.get('image'),
description=metadata.get('description'), description=metadata.get('description'),
author=re.match(r'(.+?)\s+<([^>]+>)', metadata['author'])[1] if 'author' in metadata else None, author=(
author_email=re.match(r'(.+?)\s+<([^>]+)>', metadata['author'])[2] if 'author' in metadata else None, re.match(r'(.+?)\s+<([^>]+>)', metadata['author'])[1]
published=(metadata['published'].strftime('%b %d, %Y') if 'author' in metadata else None
if metadata.get('published') else None), ),
author_email=(
re.match(r'(.+?)\s+<([^>]+)>', metadata['author'])[2]
if 'author' in metadata else None
),
published=(
metadata['published'].strftime('%b %d, %Y')
if metadata.get('published') and not metadata.get('published_inferred')
else None
),
content=markdown(f.read(), extensions=['fenced_code', 'codehilite', MarkdownLatex()]), content=markdown(f.read(), extensions=['fenced_code', 'codehilite', MarkdownLatex()]),
skip_header=skip_header skip_header=skip_header
) )

View file

@ -1,13 +1,15 @@
{% with title=title or config.title or 'Blog', skip_header=skip_header or not config.header, styles=['/css/blog.css', '/css/code.css'] %} {% with title=title, skip_header=skip_header or not config.header, styles=['/css/blog.css', '/css/code.css'] %}
{% include 'common-head.html' %} {% include 'common-head.html' %}
{% endwith %} {% endwith %}
<main> <main>
<div class="container"> <div class="container">
{% if not skip_header %} {% if not skip_header %}
{% if title %}
<div class="title"> <div class="title">
<h1>{{ title }}</h1> <h1>{{ title }}</h1>
</div> </div>
{% endif %}
{% if description %} {% if description %}
<div class="description"> <div class="description">