mirror of
https://git.ari.lt/ari.lt/ari.lt.git
synced 2025-02-04 17:49:24 +01:00
update @ Mon 8 Nov 17:17:58 EET 2021
This commit is contained in:
parent
40082ff8fc
commit
83f897859b
1 changed files with 59 additions and 49 deletions
108
scripts/add_blog
108
scripts/add_blog
|
@ -1,46 +1,24 @@
|
|||
#!/usr/bin/env python3
|
||||
""" Adds a new blog """
|
||||
|
||||
import asyncio
|
||||
import os, subprocess
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import markdown
|
||||
|
||||
from bs4 import BeautifulSoup as bs
|
||||
|
||||
CHARACTER_MAP = {"/": "_", " ": "-", "?": "_", "#": "_", "<": "_", ">": "_", "\\": "_"}
|
||||
|
||||
async def main() -> int:
|
||||
open("/tmp/blog.md", "w").close()
|
||||
BLOG_MARKDOWN_TEMPLATE = """# %s
|
||||
|
||||
blog_title = input("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") as f:
|
||||
blog_content = f.read()
|
||||
|
||||
date = subprocess.check_output(["date", "+%F %T %Z"]).decode().rstrip("\n")
|
||||
content = f"""# %s
|
||||
|
||||
{date} | [back](..) | [home](/) | [git](/git)
|
||||
%s | [back](..) | [home](/) | [git](/git)
|
||||
|
||||
<hr/>
|
||||
|
||||
%s""" % (
|
||||
blog_title,
|
||||
blog_content,
|
||||
)
|
||||
%s"""
|
||||
|
||||
with open(
|
||||
f"./page/blog/blogs/{blog_title.replace(' ', '-').replace('/', '_')}.html", "w"
|
||||
) as f:
|
||||
f.write(
|
||||
f"""<!DOCTYPE html>
|
||||
BLOG_HTML_TEMPLATE = """<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
|
@ -49,7 +27,7 @@ async def main() -> int:
|
|||
<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 {date} - %s">
|
||||
<meta name="description" content="Blog on %s - %s">
|
||||
<meta name="robots" content="follow"/>
|
||||
</head>
|
||||
<body>
|
||||
|
@ -59,18 +37,8 @@ async def main() -> int:
|
|||
</body>
|
||||
</html>
|
||||
<!-- this is automatically generated by scripts/add_blog -->"""
|
||||
% (blog_title, markdown.markdown(content))
|
||||
)
|
||||
|
||||
with open("./page/blog/index.md", "a") as f:
|
||||
f.write(
|
||||
f"\n* [{blog_title}](/page/blog/blogs/{blog_title.replace(' ', '-').replace('/', '_')}.html)"
|
||||
)
|
||||
|
||||
with open("./page/blog/index.html", "w") as f:
|
||||
with open("./page/blog/index.md", "r") as md:
|
||||
f.write(
|
||||
"""<!DOCTYPE html>
|
||||
HOME_PAGE_HTML_TEMPLATE = """<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
|
@ -90,10 +58,53 @@ async def main() -> int:
|
|||
</body>
|
||||
</html>
|
||||
<!-- this is automatically generated by scripts/add_blog -->"""
|
||||
% markdown.markdown(md.read())
|
||||
)
|
||||
|
||||
|
||||
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 final[0:50]
|
||||
|
||||
|
||||
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") as f:
|
||||
blog_content = f.read()
|
||||
|
||||
date = subprocess.check_output(["date", "+%F %T %Z"]).decode().rstrip("\n")
|
||||
content = BLOG_MARKDOWN_TEMPLATE % (date, blog_title, blog_content)
|
||||
|
||||
with open(f"./page/blog/blogs/{sanitised_blog_title}.html", "w") as f:
|
||||
f.write(BLOG_HTML_TEMPLATE % (date, blog_title, markdown.markdown(content)))
|
||||
|
||||
with open("./page/blog/index.md", "a") as f:
|
||||
f.write(f"\n* [{blog_title}](/page/blog/blogs/{sanitised_blog_title}.html)")
|
||||
|
||||
with open("./page/blog/index.html", "w") as f:
|
||||
with open("./page/blog/index.md", "r") as md:
|
||||
f.write(HOME_PAGE_HTML_TEMPLATE % markdown.markdown(md.read()))
|
||||
|
||||
# Pretify the blog and the index page
|
||||
with open("./page/blog/index.html", "r") as f:
|
||||
|
@ -103,11 +114,10 @@ async def main() -> int:
|
|||
soup = bs(index_page, features="lxml")
|
||||
f.write(soup.prettify())
|
||||
|
||||
|
||||
with open(f"./page/blog/blogs/{blog_title.replace(' ', '-').replace('/', '_')}.html", "r") as f:
|
||||
with open(f"./page/blog/blogs/{sanitised_blog_title}.html", "r") as f:
|
||||
blog_page = f.read()
|
||||
|
||||
with open(f"./page/blog/blogs/{blog_title.replace(' ', '-').replace('/', '_')}.html", "w") as f:
|
||||
with open(f"./page/blog/blogs/{sanitised_blog_title}.html", "w") as f:
|
||||
soup = bs(blog_page, features="lxml")
|
||||
f.write(soup.prettify())
|
||||
|
||||
|
@ -116,4 +126,4 @@ async def main() -> int:
|
|||
|
||||
if __name__ == "__main__":
|
||||
assert main.__annotations__.get("return") is int, "main() should return an integer"
|
||||
exit(asyncio.run(main()))
|
||||
sys.exit(main())
|
||||
|
|
Loading…
Add table
Reference in a new issue