git @ Cat's Eye Technologies The-Glosscubator / master script / extract_ratings.py
master

Tree @master (Download .tar.gz)

extract_ratings.py @masterraw · history · blame

#!/usr/bin/env python3

# SPDX-FileCopyrightText: Chris Pressey, the original author of this work, has dedicated it to the public domain.
# For more information, please refer to <https://unlicense.org/>
# SPDX-License-Identifier: Unlicense

from argparse import ArgumentParser
import json
import re
import os
import subprocess

from feedmark.loader import read_document_from


def load_feedmark_sections(topic, filename):
    path = os.path.join(topic, "src", filename)
    if os.path.isfile(path):
        return read_document_from(path).to_json_data()["sections"]
    else:
        return []


def main(args):
    topic = args[0]
    outdir = os.path.join(topic, "src", "commentary")
    os.makedirs(outdir, exist_ok=True)
    outfilename = os.path.join(outdir, "Chris Pressey.md")
    if os.path.exists(outfilename):
        raise OSError("{} already exists".format(outfilename))

    sections = []
    sections += load_feedmark_sections(topic, "Books.md")
    sections += load_feedmark_sections(topic, "Papers.md")
    sections += load_feedmark_sections(topic, "Webpages.md")
    sections += load_feedmark_sections(topic, "Repositories.md")

    with open(outfilename, 'w') as f:

        f.write("""\
Commentary by Chris Pressey
===========================

<!--
Copyright (c) 2024 Chris Pressey, Cat's Eye Technologies.

{}: CC-BY-ND-4.0
-->

This work is distributed under a CC-BY-ND-4.0 license, with the following explicit
exception: the ratings may be freely used for any purpose with no limitations.

{}
---------------

""".format("SPDX-License-Identifier", topic))
        for entry in sections:
            f.write("### {}\n\n".format(entry["title"]))
            f.write("*   rating: {}\n\n".format(entry["properties"].get("rating", "TODO")))
            f.write(".\n\n")

    subprocess.run("sed -i '/^\*   rating/d' \"{}\"/src/*.md".format(topic), shell=True)

# sed -i '/^\*   rating/d' "$TOPIC"/src/*.md
# grep -r 'rating:' | grep -v commentary | sort

if __name__ == "__main__":
    import sys
    main(sys.argv[1:])