perrotuerto.blog/scripts/make.py

86 lines
3.9 KiB
Python

import re
import sys
import json
from datetime import datetime
from pathlib import Path
root = Path(__file__).parent.parent
about = re.sub(r'\s+', ' ', """
Hola, soy perro tuerto. Mi formación académica es en Filosofía, mi
profesión es la edición de publicaciones (libros, fanzines, revistas, etc.)
y mi programación se enfoca en el desarrollo de metodologías libres para la
publicación. Soy fan de las humanidades, la paleoantropología y las
ciencias de la computación, así como soy voluntario en organizaciones sobre
edición, <i>software</i> y cultura libres, como Miau, Cuates o Wikipedia.
Doy soporte técnico a la Academia Mexicana de la Lengua y puedo ayudarte
en tus proyectos. <b>En este espacio comparto enlaces que me parecen
chéveres.</b>
""").strip()
contact = {
"site": "https://perrotuerto.blog",
"gitlab": "https://gitlab.com/perrotuerto",
"cuates": "https://git.cuates.net/perro",
"wikipedia": "https://es.wikipedia.org/wiki/Usuario:Perrotuerto",
"github": "https://github.com/perrotuerto",
"email": "hi@perrotuerto.blog",
}
links = json.loads(Path(sys.argv[1]).read_text())
data = {"acerca": about, "contacto": contact, "links": links["results"]}
index = root / "public" / "index.html"
template = (root / "src" / "template.html").read_text()
body = ""
for key, val in data.items():
if key == "contacto":
for name, url in val.items():
template = re.sub(f'#{name.upper()}#', url, template)
continue
body += f'\n<section id="{key}">'
body += f'\n<h1>{key.capitalize()}</h1>'
if isinstance(val, str):
body += f'\n{val}'
else:
body += '<ul class="list">'
for link in val:
date = "%d/%m/%Y"
url = link["url"]
created = link["date_added"]
updated = link["date_modified"]
created = datetime.fromisoformat(created).strftime(date)
updated = datetime.fromisoformat(updated).strftime(date)
tags = filter(lambda x: x != "blog", link["tag_names"])
tags = map(lambda x: f'<span class="tag">#{x}</span>', tags)
tags = map(lambda x: f'<a>{x}</a>', tags)
body += f'\n<li class="link" id="{link["id"]}">'
body += f'\n<h1><a class="anchor" href="#{link["id"]}">⚓</a>'
body += f'<a class="name" href="{url}">{link["title"]}</a></h1>'
body += f'\n<p class="description">{link["description"]}</p>'
body += f'\n<p class="tags">{" ".join(tags)}</p>'
body += '\n<p class="dates">'
body += f'<span class="created">{created}</span>'
body += f'<span class="updated">{updated}</span></p>'
if "notes" in link.keys():
blocks = map(lambda x: x.strip(), link["notes"].split("\n"))
blocks = list(filter(None, blocks))
for i, block in enumerate(blocks):
block = re.sub(r'\*\*\*(.+)\*\*\*', r'<b><i>\1</i></b>', block)
block = re.sub(r'\*\*(.+)\*\*', r'<b>\1</b>', block)
block = re.sub(r'\*(.+)\*', r'<i>\1</i>', block)
block = re.sub(r'\[(.+)\]\((.+)\)', r'<a target="_blank" href="\2">\1</a>', block)
header = r'(#+)\s*(.+)'
if re.match(header, block):
groups = re.match(header, block).groups()
tag = "h" + str(len(groups[0]))
blocks[i] = f"<{tag}>{groups[1]}</{tag}>"
else:
blocks[i] = f"<p>{block}</p>"
body += '\n<details class="info">'
body += '\n<summary>Leer más</summary>'
body += "\n".join(blocks)
body += '\n</details>'
body += '\n</li>'
body += "</ul>"
body += '\n</section>'
index.write_text(re.sub('#LINKS#', body, template))