Draft post-index.

This commit is contained in:
Sergio Martínez Portela 2022-08-03 21:11:09 +02:00
parent 9483fc1ba4
commit f3096eb5d9
3 changed files with 113 additions and 2 deletions

View File

@ -23,6 +23,7 @@ import time
import re
from typing import List
from bs4 import BeautifulSoup as bs4
import jinja2
import inotify.adapters
import yaml
@ -40,6 +41,9 @@ ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_PATH = os.path.join(ROOT_DIR, 'static')
ARTICLE_TEMPLATE_NAME = 'article.tmpl.html'
BLOG_INDEX_TEMPLATE_NAME = 'blog_index.tmpl.html'
BLOG_INDEX_PAGE_SIZE = 10
STATIC_RESOURCES = (
('style.css', 'css/style.css'),
('light-syntax.css', 'css/light-syntax.css'),
@ -54,6 +58,8 @@ JINJA_ENV = jinja2.Environment(
def update_statics():
global ARTICLE_TEMPLATE
ARTICLE_TEMPLATE = JINJA_ENV.get_template(ARTICLE_TEMPLATE_NAME)
global BLOG_INDEX_TEMPLATE
BLOG_INDEX_TEMPLATE = JINJA_ENV.get_template(BLOG_INDEX_TEMPLATE_NAME)
update_statics()
@ -74,7 +80,13 @@ def parse_nikola_date(match):
month=int(match.group(2)),
day=int(match.group(1)),
hour=int(match.group(4)),
minute=int(match.group(5)))
minute=int(match.group(5)),
# Note this final assumption is not good
# and might get you in trouble if trying
# to sort closely-published posts
# when others are in complete-date format
tzinfo=datetime.timezone.utc,
)
def parse_complete_date(match):
@ -179,9 +191,43 @@ def render_article(doc, front_matter, f):
)
f.write(result)
def summarize(doc):
return bs4(doc, features='lxml').text[:1000]
def render_index(docs, dest_top):
docs = sorted(docs.values(), key=lambda x: x[1]['date'], reverse=True)
for off in range(0, len(docs), BLOG_INDEX_PAGE_SIZE):
page = docs[off: off + BLOG_INDEX_PAGE_SIZE]
posts = [
{
"doc": doc,
"title": front_matter['title'],
"post_publication_date": front_matter['date'],
"post_tags": split_tags(front_matter['tags']),
"summary": summarize(doc),
}
for (doc, front_matter, out_path) in page
]
result = BLOG_INDEX_TEMPLATE.render(
posts=posts,
)
if off == 0:
fname = 'index.html'
else:
fname = 'index-{}.html'.format(off // BLOG_INDEX_PAGE_SIZE)
with open(os.path.join(dest_top, fname), 'wt') as f:
f.write(result)
def regen_all(source_top, dest_top, docs=None):
if docs is None:
docs = load_all(source_top)
# Render posts
for (doc, front_matter, out_path) in docs.values():
doc_full_path = os.path.join(dest_top, out_path)
os.makedirs(os.path.dirname(doc_full_path), exist_ok=True)
@ -194,7 +240,7 @@ def regen_all(source_top, dest_top, docs=None):
logging.error("Rendering failed 😿")
continue
# Render statics
for static in STATIC_RESOURCES:
src_path = static[0]
dest_path = static[1]
@ -211,6 +257,9 @@ def regen_all(source_top, dest_top, docs=None):
with open(target_dest, 'wt') as f:
f.write(data)
# Render index
render_index(docs, dest_top)
return docs

View File

@ -0,0 +1,52 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Código para llevar</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/style.css" />
<link rel="stylesheet" href="css/light-syntax.css" />
<link rel="stylesheet" href="css/dark-syntax.css" />
</head>
<body>
<div class="site-header">
<h1 class="site-name"><a href="/">Codigo para llevar</a></h1>
<nav class="site-links">
<span class="fancy-link">
<a href="https://codigoparallevar.com/">Home</a>
</span>
<span class="fancy-link">
<a href="https://github.com/kenkeiras">GitHub</a>
</span>
<span class="fancy-link">
<a href="https://gitlab.com/kenkeiras">GitLab</a>
</span>
<span class="fancy-link">
<a href="https://programaker.com/users/kenkeiras">PrograMaker</a>
</span>
</nav>
</div>
<div class="post-index content">
{% for post in posts %}
<div class="post-container">
<article class="post">
<h2 class="post-title">{{ post.title }}</h2>
<div class="post-metadata">
<time datetime="{{ post.post_publication_date }}">
{{ post.post_publication_date }}
</time>
<ul class="post-tags">
{% for post_tag in post.post_tags %}
<li class="post-tag">{{ post_tag }}</li>
{% endfor %}
</ul>
</div>
<div class="post-content">
{{ post.summary | safe }}
</div>
</article>
</div>
{% endfor %}
</div>
</body>
</html>

View File

@ -135,6 +135,16 @@ article.post {
display: inline;
}
/* Post index. */
.post-index .post-container {
/* box-shadow: 0px 2px 4px 2px rgba(0, 0, 0, 0.26); */
/* border-radius: 2px; */
/* padding: 1ex; */
margin-bottom: 1em;
padding-bottom: 1em;
border-bottom: #000 1px dashed;
}
/* Dark mode. */
@media (prefers-color-scheme: dark) {