#!/usr/bin/env python3 import sys import datetime import os gemlog_header = ''' # Alternativebit Gemlog ``` ............... ,XXXXXXXXXXXXXXX, ,XXXXXXXXXXXXXXX, ,XXXXXXXXXXXXXXX, ,XXXXXXXXXXXXXXX, ,XXXXXXXXXXXXXXX, ,XXXXXXXXXXXXXXX, ,XXXXXXXXXXXXXXX, ,XXXXXXXXXXXXXXX. .............. ............... ,XXXXXXXXXXXXXX, ,XXXXXXXXXXXXXXX, ,XXXXXXXXXXXXXX, ,XXXXXXXXXXXXXXX, ,XXXXXXXXXXXXXX, ,XXXXXXXXXXXXXXX, ,XXXXXXXXXXXXXX, ,XXXXXXXXXXXXXXX, ,XXXXXXXXXXXXXX, ,XXXXXXXXXXXXXXX, ,XXXXXXXXXXXXXX, ,XXXXXXXXXXXXXXX, ,XXXXXXXXXXXXXX, ,XXXXXXXXXXXXXXX, ,XXXXXXXXXXXXXX, ,XXXXXXXXXXXXXXX, ``` ## Entries ''' def entry_footer(hostname: str): return ''' => gemini://{} Gemlog Index '''.format(hostname) class LogEntry: def __init__(self, path, name, date): self.path = path self.name = name self.date = datetime.datetime.fromtimestamp(date) def pp_logentry(e): print("{} | {} | {}".format(e.name,e.path,e.date)) def findGemlogEntries(d: str) -> [LogEntry]: entries = [] for f in os.scandir(d): if f.is_file: date = f.stat().st_ctime path = f.path name = f.name entries.append(LogEntry(path, name, date)) return entries def render_article(article_filename, dst, hostname): ''' Reading an article from the gemlog source, then rendering the same entry with the entry footer in the dst dir. ''' with open(article_filename, 'r') as f: article = f.read() + entry_footer(hostname) dst_filename = os.path.join(dst, os.path.basename(article_filename)) with open(dst_filename, 'w') as f: print(article, file=f) def render_index(entries: [LogEntry], hostname: str) -> str: index = gemlog_header for e in entries: index += "=> {} [{}] {}\n".format("gemini://{}/{}".format(hostname, e.name), e.date.strftime('%d-%m-%Y'), e.name) return index if __name__ == '__main__': argv = sys.argv if len(argv) >= 4: entries = findGemlogEntries(argv[1]) dst_dir = argv[2] hostname = argv[3] entries.sort(key=(lambda x:x.date), reverse=True) if not os.path.exists(dst_dir): os.mkdir(dst_dir) [render_article(a.path, dst_dir, hostname) for a in entries] index = render_index(entries, hostname) with open(os.path.join(dst_dir, "index.gmi"), 'w') as f: print(index, file=f) else: print("Usage: ./alternativebit-gemlog gemlog_path dst_dir hostname")