From 9fc944e1dde1bc723dc72ec0562a7467b8dd2fa2 Mon Sep 17 00:00:00 2001 From: John McCardle Date: Wed, 20 Jan 2021 21:00:00 -0500 Subject: [PATCH] Initial commit --- .gitignore | 3 ++ backblogger.py | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++ template.md | 28 +++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 .gitignore create mode 100644 backblogger.py create mode 100644 template.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..546b4b7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +venv +*.swp + diff --git a/backblogger.py b/backblogger.py new file mode 100644 index 0000000..b851239 --- /dev/null +++ b/backblogger.py @@ -0,0 +1,76 @@ +# Backblogger.py : Scan directories for images, and scaffold into a blog post. +import os +import json +from datetime import datetime, date + +def markdown_date(ts): + d = datetime.fromtimestamp(ts) + return f"{d.year}-{d.month}-{d.day}" + +IMG_WIDTH = 760 +def resize_rename(imgpath, article_number, image_number): + new_fn = f"article{article_number.zfill(2)}_image{image_number.zfill(2)}.{imgpath.split('.')[-1]}" + + +class BlogScaffold: + def __init__(self, path): + self.path = os.path.abspath(path) + self.data = { "images": [], + "blogfile": None + } + # Check the path for backblog metadata + self.scanned = os.path.exists(os.path.join(self.path, "backblog.json")) + if not self.scanned: + self.scan() + return + with open(os.path.join(self.path, "backblog.json"), "r") as f: + self.data = json.loads(f.read()) + + def scan(self): + _, _, files = next(os.walk(self.path)) + for f in files: + self.data['images'].append({ + "path": f, + "date": os.stat(os.path.join(self.path, f)).st_ctime + }) + self.scanned = True + self.save() + + def save(self): + with open(os.path.join(self.path, "backblog.json"), "w") as f: + f.write(json.dumps(self.data)) + + def image_times(self): + for i in self.data['images']: + yield datetime.fromtimestamp(i['date']) + + def markdown_template(self, article_number): + replace = { + "{{TITLE}}": "Backblog basic template about " + self.path, + "{{SLUG}}": os.path.basename(self.path), + "{{CATEGORY}}": "category", + "{{EARLIESTDATE}}": markdown_date(min([i['date'] for i in self.data['images']])), + "{{TODAY}}": str(date.today()), + "{{ARTICLENUM}}": article_number.zfill(2) + } + txt = None + with open("template.md", "r") as f: + txt = f.read() + img_template = txt.split("%%%")[1] + img_txt = "" + for i, image in enumerate(self.data['images']): + img_fn = resize_rename(image['path'], article_number, i) + this_txt = img_template + this_txt.replace("{{IMG_FN}}", img_fn) + + def __repr__(self): + if not self.scanned: + return f"" + return f"" + +if __name__ == '__main__': + subdirs = os.listdir('..') + # don't scan program's directory + subdirs.remove(os.path.basename(os.path.abspath('.'))) + scaffolds = [BlogScaffold(os.path.join('..', sd)) for sd in subdirs] + diff --git a/template.md b/template.md new file mode 100644 index 0000000..1b497ee --- /dev/null +++ b/template.md @@ -0,0 +1,28 @@ +title: {{TITLE}} +slug: {{SLUG}} +category: {{CATEGORY}} +startdate: {{EARLIESTDATE}} +date: {{TODAY}} +modified: {{TODAY}} + +![alt text](images/article{{ARTICLENUM}}_image01_header.{{EXT}}) + +# TITLE + +asdf. etaoin shrdlu. LOREM IPSUM + +%%% +![alt text](images/images/article{{ARTICLENUM}}_image01_header.{{EXT}}) +%%% + +## challenges + +asdf. etaoin shrdlu. LOREM IPSUM + +## favorite part + +asdf. etaoin shrdlu. LOREM IPSUM + +## what I learned + +asdf. etaoin shrdlu. LOREM IPSUM