Compare commits

..

17 Commits

Author SHA1 Message Date
Fabio Manganiello 50207d7e25 Bump version: 0.2.25 → 0.2.26 2024-06-03 13:15:40 +02:00
Fabio Manganiello 8d74cb0c26 Added `tables` to the list of markdown extensions 2024-06-03 13:14:29 +02:00
Fabio Manganiello 90d85fedb4 Bump version: 0.2.24 → 0.2.25 2024-06-01 01:08:27 +02:00
Fabio Manganiello 279d885478 Updated CHANGELOG 2024-06-01 01:08:22 +02:00
Fabio Manganiello 56417c6763 Keep a sans-serif font for the index and for the titles 2024-06-01 01:07:34 +02:00
Fabio Manganiello d78393f1b3 Bump version: 0.2.23 → 0.2.24 2024-06-01 01:02:03 +02:00
Fabio Manganiello bf80f73950 Switch from a sans-serif to a serif default font 2024-06-01 01:01:39 +02:00
Fabio Manganiello 38b079d42d Fixed base_link 2024-04-11 02:21:09 +02:00
Fabio Manganiello 43897cc961 Don't prepend images with the blog base URL if they are already full URLs 2024-04-11 02:18:56 +02:00
Fabio Manganiello bf714a30bc Bump version: 0.2.22 → 0.2.23 2024-04-11 01:24:52 +02:00
Fabio Manganiello 89ea18a805 Allow parentheses in post headers values 2024-04-11 01:24:38 +02:00
Fabio Manganiello e3b30e6a98 Bump version: 0.2.21 → 0.2.22 2024-04-11 01:10:38 +02:00
Fabio Manganiello 817b23ac69 Always cast post datetimes to dates on render 2024-04-11 01:10:27 +02:00
Fabio Manganiello d517416077 Bump version: 0.2.20 → 0.2.21 2024-04-11 01:05:13 +02:00
Fabio Manganiello 390ca758b9 More resilient logic to handle both date and datetime timestamps in a post metadata 2024-04-11 01:04:13 +02:00
Fabio Manganiello 609dd14d90 Bump version: 0.2.19 → 0.2.20 2023-05-08 12:11:16 +02:00
Fabio Manganiello 958f4106cc Fixed broken format strings in RSS feed route 2023-05-08 12:10:15 +02:00
7 changed files with 109 additions and 78 deletions

View File

@ -1,5 +1,10 @@
# Changelog
## 0.2.24
- Better default fonts - `sans-serif` style for the index and the titles,
`serif` for the articles' body.
## 0.2.19
- Added `short_feed` configuration flag to permanently disable returning the

View File

@ -1 +1 @@
__version__ = '0.2.19'
__version__ = '0.2.26'

View File

@ -12,11 +12,11 @@ from ._sorters import PagesSorter, PagesSortByTime
class BlogApp(Flask):
_title_header_regex = re.compile(r'^#\s*((\[(.*)\])|(.*))')
_title_header_regex = re.compile(r"^#\s*((\[(.*)\])|(.*))")
def __init__(self, *args, **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")
self.img_dir = config.default_img_dir
self.css_dir = config.default_css_dir
self.js_dir = config.default_js_dir
@ -27,73 +27,77 @@ class BlogApp(Flask):
# `config.content_dir` is treated as the root for markdown files.
self.pages_dir = config.content_dir
img_dir = os.path.join(config.content_dir, 'img')
img_dir = os.path.join(config.content_dir, "img")
if os.path.isdir(img_dir):
self.img_dir = os.path.abspath(img_dir)
else:
self.img_dir = config.content_dir
css_dir = os.path.join(config.content_dir, 'css')
css_dir = os.path.join(config.content_dir, "css")
if os.path.isdir(css_dir):
self.css_dir = os.path.abspath(css_dir)
js_dir = os.path.join(config.content_dir, 'js')
js_dir = os.path.join(config.content_dir, "js")
if os.path.isdir(js_dir):
self.js_dir = os.path.abspath(js_dir)
fonts_dir = os.path.join(config.content_dir, 'fonts')
fonts_dir = os.path.join(config.content_dir, "fonts")
if os.path.isdir(fonts_dir):
self.fonts_dir = os.path.abspath(fonts_dir)
templates_dir = os.path.join(config.content_dir, 'templates')
templates_dir = os.path.join(config.content_dir, "templates")
if os.path.isdir(templates_dir):
self.template_folder = os.path.abspath(templates_dir)
def get_page_metadata(self, page: str) -> dict:
if not page.endswith('.md'):
page = page + '.md'
if not page.endswith(".md"):
page = page + ".md"
md_file = os.path.join(self.pages_dir, page)
if not os.path.isfile(md_file):
abort(404)
metadata = {}
with open(md_file, 'r') as f:
metadata['uri'] = '/article/' + page[:-3]
with open(md_file, "r") as f:
metadata["uri"] = "/article/" + page[:-3]
for line in f.readlines():
for line in f:
if not line:
continue
if not (m := re.match(r'^\[//]: # \(([^:]+):\s*([^)]+)\)\s*$', line)):
if not (m := re.match(r"^\[//]: # \(([^:]+):\s*(.*)\)\s*$", line)):
break
if m.group(1) == 'published':
metadata[m.group(1)] = datetime.date.fromisoformat(m.group(2))
if m.group(1) == "published":
metadata[m.group(1)] = datetime.datetime.fromisoformat(
m.group(2)
).date()
else:
metadata[m.group(1)] = m.group(2)
if not metadata.get('title'):
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 = ''
with open(md_file, "r") as f:
header = ""
for line in f.readlines():
header = line
break
metadata['title_inferred'] = True
metadata["title_inferred"] = True
m = self._title_header_regex.search(header)
if m:
metadata['title'] = m.group(3) or m.group(1)
metadata["title"] = m.group(3) or m.group(1)
else:
metadata['title'] = os.path.basename(md_file)
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,
# infer it from the file's creation date
metadata['published'] = datetime.date.fromtimestamp(os.stat(md_file).st_ctime)
metadata['published_inferred'] = True
metadata["published"] = datetime.date.fromtimestamp(
os.stat(md_file).st_ctime
)
metadata["published_inferred"] = True
return metadata
@ -102,37 +106,42 @@ class BlogApp(Flask):
page: str,
title: Optional[str] = None,
skip_header: bool = False,
skip_html_head: bool = False
skip_html_head: bool = False,
):
if not page.endswith('.md'):
page = page + '.md'
if not page.endswith(".md"):
page = page + ".md"
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)
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(
'article.html',
"article.html",
config=config,
title=title,
image=metadata.get('image'),
description=metadata.get('description'),
image=metadata.get("image"),
description=metadata.get("description"),
author=(
re.match(r'(.+?)\s+<([^>]+>)', metadata['author'])[1]
if 'author' in metadata 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')
re.match(r"(.+?)\s+<([^>]+>)", metadata["author"])[1]
if "author" in metadata
else None
),
content=markdown(f.read(), extensions=['fenced_code', 'codehilite', MarkdownLatex()]),
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", "tables", MarkdownLatex()]
),
skip_header=skip_header,
skip_html_head=skip_html_head,
)
@ -145,26 +154,25 @@ class BlogApp(Flask):
sorter: Type[PagesSorter] = PagesSortByTime,
reverse: bool = True,
) -> List[Tuple[int, dict]]:
pages_dir = app.pages_dir.rstrip('/')
pages_dir = app.pages_dir.rstrip("/")
pages = [
{
'path': os.path.join(root[len(pages_dir)+1:], f),
'folder': root[len(pages_dir)+1:],
'content': (
"path": os.path.join(root[len(pages_dir) + 1 :], f),
"folder": root[len(pages_dir) + 1 :],
"content": (
self.get_page(
os.path.join(root, f),
skip_header=skip_header,
skip_html_head=skip_html_head,
)
if with_content else ''
),
**self.get_page_metadata(
os.path.join(root[len(pages_dir)+1:], f)
if with_content
else ""
),
**self.get_page_metadata(os.path.join(root[len(pages_dir) + 1 :], f)),
}
for root, _, files in os.walk(pages_dir, followlinks=True)
for f in files
if f.endswith('.md')
if f.endswith(".md")
]
sorter_func = sorter(pages)

View File

@ -1,5 +1,7 @@
import os
import re
from typing import Optional
from urllib.parse import urljoin
from flask import (
jsonify,
@ -115,8 +117,12 @@ def article_route(article: str):
@app.route("/rss", methods=["GET"])
def rss_route():
pages = app.get_pages(with_content=True, skip_header=True, skip_html_head=True)
short_description = "short" in request.args
short_description = "short" in request.args or config.short_feed
pages = app.get_pages(
with_content=not short_description,
skip_header=True,
skip_html_head=True,
)
return Response(
"""<?xml version="1.0" encoding="UTF-8" ?>
@ -149,32 +155,36 @@ def rss_route():
),
items="\n\n".join(
[
"""
(
"""
<item>
<title>{title}</title>
<link>{base_link}{link}</link>
<pubDate>{published}</pubDate>"""
+ (
"<description><![CDATA[{content}]]></description>"
if not config.short_feed
else ""
)
+ """
<media:content medium="image" url="{base_link}{image}" width="200" height="150" />
<pubDate>{published}</pubDate>
<description><![CDATA[{content}]]></description>
<media:content medium="image" url="{image}" width="200" height="150" />
</item>
""".format(
"""
).format(
base_link=config.link,
title=page.get("title", "[No Title]"),
link=page.get("uri", ""),
published=page["published"].strftime(
"%a, %d %b %Y %H:%M:%S GMT"
)
if "published" in page
else "",
content=page.get("description", "")
if short_description
else page.get("content", ""),
image=page.get("image", ""),
published=(
page["published"].strftime("%a, %d %b %Y %H:%M:%S GMT")
if "published" in page
else ""
),
content=(
page.get("description", "")
if short_description
else page.get("content", "")
),
image=(
urljoin(config.link, page["image"])
if page.get("image")
and not re.search(r"^https?://", page["image"])
else page.get("image", "")
),
)
for _, page in pages
]

View File

@ -2,7 +2,7 @@ html {
height: -webkit-fill-available;
height: -moz-available;
font-size: 20px;
font-family: -apple-system, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Fira Sans", "Open Sans", "Droid Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
font-family: Lora, "Palatino Linotype", "Book Antiqua", "New York", "DejaVu serif", serif;
font-weight: 400;
text-rendering: optimizeLegibility;
}
@ -119,6 +119,10 @@ h2 {
line-height: 1.1em;
}
h1, h2, h3, h4, h5, h6 {
font-family: -apple-system, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Fira Sans", "Open Sans", "Droid Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
}
footer {
width: 100%;
font-size: .65em;
@ -129,3 +133,7 @@ footer {
text-align: center;
box-shadow: 1px -2px 2px 0 #bbb;
}
.index {
font-family: -apple-system, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Fira Sans", "Open Sans", "Droid Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
}

View File

@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.2.19
current_version = 0.2.26
commit = True
tag = True

View File

@ -11,7 +11,7 @@ def readfile(file):
setup(
name='madblog',
version='0.2.19',
version='0.2.26',
author='Fabio Manganiello',
author_email='info@fabiomanganiello.com',
description='A minimal platform for Markdown-based blogs',