ari.lt/scripts/add_blog

143 lines
4.2 KiB
Text
Raw Normal View History

2021-10-22 07:40:32 +03:00
#!/usr/bin/env python3
2021-11-08 17:17:58 +02:00
""" Adds a new blog """
2021-10-22 07:40:32 +03:00
2021-11-08 17:17:58 +02:00
import os
import subprocess
import sys
2021-10-22 07:40:32 +03:00
import markdown
2021-11-09 11:57:53 +02:00
import random
2021-10-22 07:40:32 +03:00
2021-10-29 08:59:14 +03:00
from bs4 import BeautifulSoup as bs
2021-11-08 17:17:58 +02:00
CHARACTER_MAP = {"/": "_", " ": "-", "?": "_", "#": "_", "<": "_", ">": "_", "\\": "_"}
2021-10-22 07:40:32 +03:00
2021-11-08 17:17:58 +02:00
BLOG_MARKDOWN_TEMPLATE = """# %s
2021-10-22 07:40:32 +03:00
2021-11-08 17:17:58 +02:00
%s | [back](..) | [home](/) | [git](/git)
2021-10-22 09:26:20 +03:00
2021-10-22 07:40:32 +03:00
<hr/>
2021-10-22 09:26:20 +03:00
2021-11-08 17:17:58 +02:00
%s"""
2021-10-22 07:40:32 +03:00
2021-11-08 17:17:58 +02:00
BLOG_HTML_TEMPLATE = """<!DOCTYPE html>
2021-10-22 09:26:20 +03:00
<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">
2021-10-25 01:04:49 +03:00
<title>Ari::web -> Blog</title>
2021-10-23 01:05:25 +03:00
<meta name="keywords" content="website webdev linux programming ari terminal dark blog javascript opensource free">
2021-11-08 17:17:58 +02:00
<meta name="description" content="Blog on %s - %s">
2021-10-27 03:25:25 +03:00
<meta name="robots" content="follow"/>
2021-10-22 09:26:20 +03:00
</head>
<body>
<div>
%s
</div>
</body>
</html>
2021-10-23 01:45:39 +03:00
<!-- this is automatically generated by scripts/add_blog -->"""
2021-10-22 07:40:32 +03:00
2021-11-08 17:17:58 +02:00
HOME_PAGE_HTML_TEMPLATE = """<!DOCTYPE html>
2021-10-22 07:40:32 +03:00
<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">
2021-10-25 01:04:49 +03:00
<title>Ari::web -> Blog</title>
2021-10-23 01:04:16 +03:00
<meta name="keywords" content="website webdev linux programming ari terminal dark blog javascript opensource free">
2021-10-23 10:03:58 +03:00
<meta name="description" content="My blog page">
2021-10-27 03:25:25 +03:00
<meta name="robots" content="follow"/>
2021-10-22 07:40:32 +03:00
</head>
<body>
<h1>My blogs</h1>
<div>
%s
2021-10-22 09:26:20 +03:00
</div>
2021-10-22 07:40:32 +03:00
</body>
</html>
2021-10-22 07:51:17 +03:00
<!-- this is automatically generated by scripts/add_blog -->"""
2021-10-22 07:40:32 +03:00
2021-10-29 08:59:14 +03:00
2021-11-08 17:17:58 +02:00
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)
2021-11-09 11:57:53 +02:00
return f"{final[0:64]}_{random.randint(10_000, 10_000_000_000)}"
2021-11-08 17:17:58 +02:00
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")
2021-11-09 11:57:53 +02:00
with open("/tmp/blog.md", "r", encoding="utf-8") as blog_markdown:
blog_content = blog_markdown.read()
2021-11-08 17:17:58 +02:00
date = subprocess.check_output(["date", "+%F %T %Z"]).decode().rstrip("\n")
2021-11-09 12:02:28 +02:00
content = BLOG_MARKDOWN_TEMPLATE % (blog_title, date, blog_content)
2021-11-08 17:17:58 +02:00
2021-11-09 11:57:53 +02:00
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))
)
2021-11-08 17:17:58 +02:00
2021-11-09 11:57:53 +02:00
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)"
)
2021-11-08 17:17:58 +02:00
2021-11-09 11:57:53 +02:00
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 % markdown.markdown(home_page_md.read())
)
2021-10-29 08:59:14 +03:00
# Pretify the blog and the index page
2021-11-09 11:57:53 +02:00
with open("./page/blog/index.html", "r", encoding="utf-8") as home_page:
index_page = home_page.read()
2021-10-29 08:59:14 +03:00
2021-11-09 11:57:53 +02:00
with open("./page/blog/index.html", "w", encoding="utf-8") as home_page:
2021-10-29 08:59:14 +03:00
soup = bs(index_page, features="lxml")
2021-11-09 11:57:53 +02:00
home_page.write(soup.prettify())
2021-10-29 08:59:14 +03:00
2021-11-09 11:57:53 +02:00
with open(
f"./page/blog/blogs/{sanitised_blog_title}.html", "r", encoding="utf-8"
) as blog_html:
blog_page = blog_html.read()
2021-10-29 08:59:14 +03:00
2021-11-09 11:57:53 +02:00
with open(
f"./page/blog/blogs/{sanitised_blog_title}.html", "w", encoding="utf-8"
) as blog_html:
2021-10-29 08:59:14 +03:00
soup = bs(blog_page, features="lxml")
2021-11-09 11:57:53 +02:00
blog_html.write(soup.prettify())
2021-10-29 08:59:14 +03:00
2021-10-22 07:40:32 +03:00
return 0
if __name__ == "__main__":
assert main.__annotations__.get("return") is int, "main() should return an integer"
2021-11-08 17:17:58 +02:00
sys.exit(main())