mjestecko/tools/article_wrapper.py

65 lines
2.1 KiB
Python
Raw Normal View History

2023-05-21 14:24:54 +00:00
#!/usr/bin/python3
# todo: Show related git history of a file?
from sys import argv, exit
2023-05-25 17:35:39 +00:00
import time, urllib.parse, re
2024-02-10 17:53:26 +00:00
import os.path as path
2023-05-21 14:24:54 +00:00
from article_utils import the_line_after_metadata, parse_metadata
from page_shares import wrap_page, MONTHS
2023-05-21 14:24:54 +00:00
if len(argv) <= 1:
print("No file was supplied")
exit(-1)
2023-05-25 16:58:10 +00:00
if len(argv) <= 2:
print("No address was supplied")
exit(-1)
2023-05-21 14:24:54 +00:00
with open(argv[1], "r") as f:
content = f.readlines()
i = the_line_after_metadata(content)
metadata = parse_metadata(argv[1])
2024-02-10 17:53:26 +00:00
directory = path.split(path.dirname(path.abspath(argv[1])))[-1]
2023-05-21 14:24:54 +00:00
2024-02-16 13:29:50 +00:00
title = metadata["Title"]
2023-05-26 09:00:48 +00:00
article_head = "\n# " + title + "\n"
2023-05-21 14:24:54 +00:00
brief = metadata.get("Brief")
2023-05-21 14:24:54 +00:00
if not brief is None:
article_head += f"*{brief}*\n\n"
2023-05-21 14:24:54 +00:00
date = metadata.get("Date")
2023-05-21 14:24:54 +00:00
if not date is None:
2023-05-22 19:03:16 +00:00
article_head += f"-- Created: *{MONTHS[date.tm_mon]} {date.tm_mday}, {date.tm_year} UTC*\n\n"
2023-05-21 14:24:54 +00:00
last_edit = metadata.get("Last Edit")
2023-05-21 14:24:54 +00:00
if not last_edit is None:
if date is None or last_edit.tm_mon != date.tm_mon or \
last_edit.tm_mday != date.tm_mday or last_edit.tm_year != date.tm_year:
2023-05-22 19:03:16 +00:00
article_head += f"-- Edited: *{MONTHS[last_edit.tm_mon]} {last_edit.tm_mday}, {last_edit.tm_year} UTC*\n\n"
2023-05-21 14:24:54 +00:00
tags = metadata.get("Tags")
2023-05-21 14:24:54 +00:00
if tags:
2024-02-16 13:29:50 +00:00
tag_links = []
for tag in tags:
tag_links.append(f"[{tag}](/tags/{urllib.parse.quote(tag.lower())}.html)")
article_head += f"""-- Tags: *{", ".join(tag_links)}*\n\n"""
article_head += "---\n\n"
2023-05-21 14:24:54 +00:00
2023-05-25 16:58:10 +00:00
header = f"""HTML header: <meta property="og:title" content="{title} on mjestečko"></meta>
<meta property="og:type" content="article"></meta>
2024-02-10 17:53:26 +00:00
<meta property="og:url" content="{argv[2]}/articles/{urllib.parse.quote(directory)}.html"></meta>
2023-05-25 16:58:10 +00:00
"""
if not brief is None:
header += f"""<meta property="og:description" content="{brief}"></meta>\n"""
2023-05-25 17:35:39 +00:00
front_image = re.compile(r"!\[.*\]\((.+?)\)", re.DOTALL).search(''.join(content[i:]))
if not front_image is None:
header += f"""<meta property="og:image" content="{argv[2]}/{urllib.parse.quote(front_image.group(1))}"></meta>\n"""
2023-05-25 16:58:10 +00:00
print(header + ''.join(content[:i]) + wrap_page(article_head + ''.join(content[i:])))