From 2e098f79093a76727f263f2bb2ed73a91c4a2b5f Mon Sep 17 00:00:00 2001 From: Mike Lynch Date: Sat, 4 May 2024 14:14:36 +1000 Subject: [PATCH] Made the colophon on each index page be a file and provided a sample. --- apub2gmi.py | 32 +++++++++++++++++++++++--------- colophon.gmi | 1 + 2 files changed, 24 insertions(+), 9 deletions(-) create mode 100644 colophon.gmi diff --git a/apub2gmi.py b/apub2gmi.py index c3417f2..32d2336 100755 --- a/apub2gmi.py +++ b/apub2gmi.py @@ -46,7 +46,7 @@ NAME_RES = [ -class GlossatoryPost(): +class MediaPost(): def __init__(self, year, month, day, file, title): self.year = year self.month = month @@ -87,7 +87,7 @@ def process_post(archive, obj): else: raise ValueError(f"Couldn't match url {url}") alt = attachment["name"] - return GlossatoryPost(year, month, day, file, alt) + return MediaPost(year, month, day, file, alt) def ensure_dir(gmdir): @@ -95,17 +95,26 @@ def ensure_dir(gmdir): gmdir.mkdir(parents=True) -def write_gemfile(gmdir, title, items): +def load_colophon(cfile): + if cfile: + with open(cfile, "r") as cfh: + colophon = cfh.readlines() + return "".join(colophon) + return None + +def write_gemfile(gmdir, colophon, title, items): ensure_dir(gmdir) gmi = gmdir / "index.gmi" with open(gmi, "w") as gfh: - gfh.write(HEADER) + if colophon: + gfh.write(colophon) + gfh.write("\n\n") gfh.write(f"# {title}\n\n") for link, text in items: gfh.write(f"=> {link} {text}\n") -def apub2gmi(archive, output): +def apub2gmi(archive, output, colophon): with open(f"{archive}/outbox.json", "r") as fh: js = json.load(fh) posts = {} @@ -127,12 +136,12 @@ def apub2gmi(archive, output): print(f"Processing failed: {i}: {e}") years = [ ( f"{year}/", year ) for year in posts ] - write_gemfile(Path(output), "Glossatory", years) + write_gemfile(Path(output), colophon, "Glossatory", years) for year in posts: ydir = Path(output) / year months = [ ( f"{month}/", MNAMES[month] ) for month in posts[year] ] - write_gemfile(ydir, year, months) + write_gemfile(ydir, colophon, year, months) for month in posts[year]: mname = MNAMES[month] mdir = ydir / month @@ -148,7 +157,7 @@ def apub2gmi(archive, output): for day in posts[year][month]: for post in posts[year][month][day]: links.append((f"{day}/{post.fname}", post.title)) - write_gemfile(mdir, f"{mname} {year}", links) + write_gemfile(mdir, colophon, f"{mname} {year}", links) if __name__ == "__main__": @@ -159,5 +168,10 @@ if __name__ == "__main__": ap.add_argument( '-o', '--output', required=True, type=str, help="Output directory" ) + ap.add_argument( + '-c', '--colophon', required=False, type=str, + help="File with text to be included at the top of each index page" + ) args = ap.parse_args() - apub2gmi(args.archive, args.output) + colophon = load_colophon(args.colophon) + apub2gmi(args.archive, args.output, colophon) diff --git a/colophon.gmi b/colophon.gmi new file mode 100644 index 0000000..a557a88 --- /dev/null +++ b/colophon.gmi @@ -0,0 +1 @@ +This is a sample of some text which can be added at the top of every gemfile in the archive.