#! /usr/bin/python from feedgen.feed import FeedGenerator import argparse import os import sys import time from stat import S_ISREG, ST_CTIME, ST_MODE import subprocess from pathlib import Path import markdown from datetime import datetime parser = argparse.ArgumentParser(prog="pog", description="python weblog", epilog="python weblog") parser.add_argument("option", help="post [-n, --name] to make a post, title following -n with no spaces. init to initialize new blog in a directory. edit [-n, --name] to edit a post. update to rebuild site") parser.add_argument("-n", "--name", help="name of post to edit/create") args = parser.parse_args() # settings: author = "flat" # i think all options except maybe the last few are pretty self-explanatory contact = "flat@fuckup.club" website = "https://tilde.team/~flat/" bloglink = "https://tilde.team/~flat/blog/" description = 'infrequent rambles about whatever i really feel like thinking about at the moment.' title = author + "'s blog" header = f'{title}

{title}

' footer = f'

anything to say? email me!
written by {author} - generated with pog!

' dateformat = '%Y-%m-%d' exceptions = {"index.html", # all pages to ignore when building blog "pog.py", "atom.xml", "README.md", "edit.py" } pages = ["../index.html", "atom.xml"] # other pages to show on the navbar tildeMode = True # just a failsafe feature cause sometimes tilde.team bugs out with the editing, this forces nvim def update(): with open("index.html", "w") as f: f.write(f'{title}

{title}

') for page in pages: f.write(f'
  • {Path(page).stem}
  • ') f.write(f'

    {description}

    ') f.write('

    posts:

    ') files = (os.listdir("drafts/")) files = ((os.stat(f'drafts/{path}'), path) for path in files) files = ((stat[ST_CTIME], f'drafts/{path}') for stat, path in files if S_ISREG(stat[ST_MODE])) entries = [] files = sorted(files)[::-1] for cdate, path in files: if os.path.basename(path) not in exceptions: open("index.html", "a").write(f'{time.ctime(cdate)} - {Path(os.path.basename(path)).stem}
    ') markdown.markdownFromFile(input=f'drafts/{os.path.basename(path)}', output=f'{Path(path).stem}.html', output_format="html") fg = FeedGenerator() fg.title(title) fg.author( {"name" : author, "email" : contact} ) fg.subtitle(description) fg.link({"href": bloglink, "rel": "self"}) fg.link({"href": website, "rel": "alternate"}) fg.id(bloglink) for path in os.listdir("drafts/"): if path not in exceptions: fe = fg.add_entry() fe.id(bloglink + Path(path).stem + ".html") fe.title(Path(path).stem) fe.link({"href": bloglink + Path(path).stem + ".html", "rel": "self"}) fe.link({"href": bloglink, "rel": "alternate"}) fe.author({"name" : author, "email" : contact}) fe.content("".join((open(Path(path).stem + ".html").readlines()))) fe.pubDate(datetime.fromtimestamp(int(os.path.getmtime(Path(path).stem + ".html"))).strftime("%a, %d %b %Y %H:%M:%S GMT")) fe.updated(datetime.fromtimestamp(int(os.path.getmtime(Path(path).stem + ".html"))).strftime("%a, %d %b %Y %H:%M:%S GMT")) fe.source({"url": bloglink, "title": title}) fg.atom_file('atom.xml') for cdate, path in files: if os.path.basename(path) not in exceptions: with open(Path(os.path.basename(path)).stem + ".html", "r") as r: lines = r.readlines() count = 1 savelines = [] for line in lines[:-1]: savelines.append(lines[count]) count += 1 with open(Path(os.path.basename(path)).stem + ".html", "w") as r: r.write(header) for line in savelines: r.write(line) r.write(footer) with open("index.html", "a") as r: r.write("") match args.option: case "init": os.mkdir("drafts") with open("index.html", "w") as f: f.write('') for page in pages: f.write(f'
  • {Path(page).stem}
  • ') f.write(f'

    {title}

    ') f.write(f'

    {description}

    ') f.write('

    posts:

    ') update() f.write("") case "update": update() case "post": with open(f'drafts/{args.name}.md', "w") as f: date = datetime.today().strftime(dateformat) f.write("") f.write("\n" + f'## {args.name}') f.write(f'\n_{date}_') if tildeMode is False: subprocess.call(("xdg-open", f'drafts/{args.name}.md')) else: subprocess.call(("nvim", f'drafts/{args.name}.md')) opt = input("post written, what do you want to do now?\n(s) save draft (p) post (d) discard post\n> ") match opt: case "s": print(f"saved draft in drafts/{args.name}.md") case "p": update() print("post succesful") case "d": os.remove(f"drafts/{args.name}.md") print("file removed") case "edit": if tildeMode is False: subprocess.call(("xdg-open", f'drafts/{args.name}.md')) else: subprocess.call(("nvim", f'drafts/{args.name}.md')) opt = input("post written, what do you want to do now?\n(s) save draft (p) post (d) discard post\n> ") match opt: case "s": print(f"saved draft in drafts/{args.name}.md") case "p": update() print("post succesful") case "d": os.remove(f"drafts/{args.name}.md") print("file removed") case other: print("invalid command")