mirror of
https://git.ari.lt/ari.lt/ari.lt.git
synced 2025-02-04 17:49:24 +01:00
e07bc9774b
Signed-off-by: Ari Archer <truncateddinosour@gmail.com>
145 lines
4.2 KiB
Python
Executable file
145 lines
4.2 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
""" Adds a new blog """
|
|
|
|
import os
|
|
import random
|
|
import subprocess
|
|
import sys
|
|
|
|
import markdown
|
|
from bs4 import BeautifulSoup as bs
|
|
|
|
CHARACTER_MAP = {"/": "_", " ": "-", "?": "_", "#": "_", "<": "_", ">": "_", "\\": "_"}
|
|
|
|
BLOG_MARKDOWN_TEMPLATE = """# %s
|
|
|
|
%s | [back](..) | [home](/) | [git](/git)
|
|
|
|
<hr/>
|
|
|
|
%s"""
|
|
|
|
BLOG_HTML_TEMPLATE = """<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Ari::web -> Blog</title>
|
|
|
|
<meta name="keywords" content="website webdev linux programming ari terminal dark blog javascript opensource free">
|
|
<meta name="description" content="Blog on %s - %s">
|
|
<meta name="robots" content="follow"/>
|
|
</head>
|
|
<body>
|
|
<div>
|
|
%s
|
|
</div>
|
|
</body>
|
|
</html>
|
|
<!-- this is automatically generated by scripts/add_blog -->"""
|
|
|
|
HOME_PAGE_HTML_TEMPLATE = """<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Ari::web -> Blog</title>
|
|
|
|
<meta name="keywords" content="website webdev linux programming ari terminal dark blog javascript opensource free">
|
|
<meta name="description" content="My blog page">
|
|
<meta name="robots" content="follow"/>
|
|
</head>
|
|
<body>
|
|
<h1>My blogs</h1>
|
|
<code>Last updated on: %s</code>
|
|
<div>
|
|
%s
|
|
</div>
|
|
</body>
|
|
</html>
|
|
<!-- this is automatically generated by scripts/add_blog -->"""
|
|
|
|
|
|
def sanitise_title(title: str) -> str:
|
|
"""
|
|
This function takes a title and uses CHARACTER_MAP to
|
|
make the title path and netlify compatiple
|
|
"""
|
|
final = title
|
|
for character, replacement in CHARACTER_MAP.items():
|
|
final = final.replace(character, replacement)
|
|
|
|
return (
|
|
f"{final[0:64]}_{random.randint(-10_000_000_000, 10_000_000_000)}".capitalize()
|
|
)
|
|
|
|
|
|
def main() -> int:
|
|
"""Main function"""
|
|
|
|
open("/tmp/blog.md", "w", encoding="utf-8").close()
|
|
|
|
blog_title = input("Blog title: ")
|
|
sanitised_blog_title = sanitise_title(blog_title)
|
|
|
|
input(
|
|
"""Now this script will open your editor, enter the markdown you want
|
|
in your blog there. press enter to continue"""
|
|
)
|
|
|
|
if os.getenv("EDITOR"):
|
|
os.system(f"{os.getenv('EDITOR')} /tmp/blog.md")
|
|
else:
|
|
os.system(f"{input('What editor should I use?: ')} /tmp/blog.md")
|
|
|
|
with open("/tmp/blog.md", "r", encoding="utf-8") as blog_markdown:
|
|
blog_content = blog_markdown.read()
|
|
|
|
date = subprocess.check_output(["date", "+%F %T %Z"]).decode().rstrip("\n")
|
|
content = BLOG_MARKDOWN_TEMPLATE % (blog_title, date, blog_content)
|
|
|
|
with open(
|
|
f"./page/blog/blogs/{sanitised_blog_title}.html", "w", encoding="utf-8"
|
|
) as blog_html:
|
|
blog_html.write(
|
|
BLOG_HTML_TEMPLATE % (date, blog_title, markdown.markdown(content))
|
|
)
|
|
|
|
with open("./page/blog/index.md", "a", encoding="utf-8") as home_page_markdown:
|
|
home_page_markdown.write(
|
|
f"\n* [{blog_title}](/page/blog/blogs/{sanitised_blog_title}.html)"
|
|
)
|
|
|
|
with open("./page/blog/index.html", "w", encoding="utf-8") as home_page:
|
|
with open("./page/blog/index.md", "r", encoding="utf-8") as home_page_md:
|
|
home_page.write(
|
|
HOME_PAGE_HTML_TEMPLATE % (date, markdown.markdown(home_page_md.read()))
|
|
)
|
|
|
|
# Pretify the blog and the index page
|
|
with open("./page/blog/index.html", "r", encoding="utf-8") as home_page:
|
|
index_page = home_page.read()
|
|
|
|
with open("./page/blog/index.html", "w", encoding="utf-8") as home_page:
|
|
soup = bs(index_page, features="lxml")
|
|
home_page.write(soup.prettify())
|
|
|
|
with open(
|
|
f"./page/blog/blogs/{sanitised_blog_title}.html", "r", encoding="utf-8"
|
|
) as blog_html:
|
|
blog_page = blog_html.read()
|
|
|
|
with open(
|
|
f"./page/blog/blogs/{sanitised_blog_title}.html", "w", encoding="utf-8"
|
|
) as blog_html:
|
|
soup = bs(blog_page, features="lxml")
|
|
blog_html.write(soup.prettify())
|
|
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
assert main.__annotations__.get("return") is int, "main() should return an integer"
|
|
sys.exit(main())
|