Use dirs for all posts

This commit is contained in:
hedy 2024-04-11 09:03:07 +08:00
parent f822e1444e
commit bdbe083640
Signed by: hedy
GPG Key ID: B51B5A8D1B176372
14 changed files with 60 additions and 1 deletions

View File

@ -20,7 +20,7 @@ def log(fn):
if DRY_RUN:
print("would", end=" ")
print(fn.__name__.replace('_', ' '),
'->'.join(p.removeprefix(GMI_DEST) for p in args))
' -> '.join(p.removeprefix(GMI_DEST) for p in args))
if not DRY_RUN:
fn(*args)
return inner

59
bin/post-dirs.py Normal file
View File

@ -0,0 +1,59 @@
#!/usr/bin/env python3
# convert content/posts/*.{gmi,md} -> content/posts/*/index.{gmi,md}
# 2024 - hedy - initial implementation with logging
# post process gemini output
import os
import shutil
import sys
from functools import wraps
from glob import glob
PREFIX = os.environ['PREFIX']
DRY_RUN = len(sys.argv) > 2 and \
sys.argv[1].lower() in ('n', '-n', '--dry-run', 'wtf', 'f')
def log(fn):
@wraps(fn)
def inner(*args):
if DRY_RUN:
print("would", end=" ")
print(fn.__name__.replace('_', ' '),
' -> '.join(p.removeprefix(PREFIX) for p in args))
if not DRY_RUN:
fn(*args)
return inner
@log
def remove(p: str):
if os.path.isdir(p):
os.rmdir(p)
else:
os.remove(p)
@log
def FORCE_remove_dir(p: str):
shutil.rmtree(p, ignore_errors=True)
@log
def rename(f: str, t: str):
os.rename(f, t)
@log
def mkdir(d: str):
os.makedirs(d, exist_ok=True)
################################################################
for file in glob(PREFIX+"/????-??-??-*.md"):
basename = file.split("/")[-1].removesuffix(".md")
dir = f"{PREFIX}/{basename}"
mkdir(dir)
rename(file, f"{dir}/index.md")
for file in glob(PREFIX+"/????-??-??-*.gmi"):
basename = file.split("/")[-1].removesuffix(".gmi")
dir = f"{PREFIX}/{basename}"
rename(file, f"{dir}/index.gmi")