From eadec07dde4c9935db51977ef6c070b1fb368b01 Mon Sep 17 00:00:00 2001 From: Mark Nottingham Date: Mon, 19 Aug 2024 11:22:37 +1000 Subject: [PATCH] a bit prettier --- lib/dir_listing.py | 82 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 61 insertions(+), 21 deletions(-) diff --git a/lib/dir_listing.py b/lib/dir_listing.py index e6032a4..5ef57c8 100644 --- a/lib/dir_listing.py +++ b/lib/dir_listing.py @@ -7,7 +7,7 @@ ignore_prefixes = ['.', '_'] sep = "\n\n---\n\n" -def spider (directory): +def spider(directory, reverse=False): index = [] sys.stderr.write(f"* scanning {directory}...\n") dirs = [] @@ -15,16 +15,25 @@ def spider (directory): for i in os.scandir(directory): if filter_thing(i): if i.is_dir(): - dirs.append(i.name) + dirs.append(i) elif i.is_file(): - files.append(i.name) - for name in nat_sort(dirs): - index.append(f"- [{pretty_dir(name)}]({name}/README.md)") - for name in nat_sort(files): - index.append(f"- [{name}]({name})") + files.append(i) + for dir_ in nat_sort(dirs, reverse): + extra = [] + if path.exists(f"{dir_.path}/agenda.md"): + extra.append(f"[agenda]({dir_.name}/agenda.md)") + if path.exists(f"{dir_.path}/minutes.md"): + extra.append(f"[minutes]({dir_.name}/minutes.md)") + extra_str = "" + if extra: + extra_str = f": {', '.join(extra)}" + index.append(f"- [{pretty_dir(dir_.name)}]({dir_.name}/){extra_str}") + for file_ in nat_sort(files): + pretty_name, extra = pretty_file(file_) + index.append(f"- [{pretty_name}]({file_.name}) {extra}") write_index(directory, index) - for dir_name in dirs: - spider(path.join(directory, dir_name)) + for dir_ in dirs: + spider(path.join(directory, dir_.name)) def filter_thing(thing): if thing.name in ignore_things: return False @@ -32,21 +41,40 @@ def filter_thing(thing): if thing.name.startswith(ignore): return False return True -def nat_sort(l): +def nat_sort(l, reverse=False): convert = lambda text: int(text) if text.isdigit() else text.lower() - alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ] - return sorted(l, key = alphanum_key) + alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key.name) ] + out = sorted(l, key = alphanum_key) + if reverse: + out.reverse() + return out -def pretty_dir(name): - if "/" in name: +def pretty_dir(name, with_path=False): + if name.endswith("/"): + name = name[:-1] + if "/" in name and with_path: + cleaned = " : ".join(name.split("/")) + else: cleaned = name.split("/")[-1] + if cleaned[:4] == "ietf": + cleaned = f"IETF {cleaned[4:]}" else: - cleaned = name - if cleaned.startswith("ietf"): - return f"IETF {cleaned[4:]}" - if cleaned.startswith("interim-"): - return f"Interim meeting: {cleaned[8:]}" - return f"{name}/" + cleaned = cleaned.title() + return f"🗂️ {cleaned}" + +def pretty_file(file_): + name = file_.name + ext = "" + extra = "" + if "." in file_.name: + name, ext = file_.name.rsplit(".", 1) + if ext.lower() not in ['md', 'html']: + extra = f"_{ext}_" + name = re.sub(r"([a-z])-", r"\1 ", name) + name = re.sub(r"-([a-z])", r" \1", name) + if ext.lower() == "md" and "agenda" not in name and "minutes" not in name: + return extract_md_heading(file_, name), extra + return name.title().replace("Http", "HTTP"), extra def write_index(directory, index): readme_path = f"{directory}/README.md" @@ -65,8 +93,20 @@ def write_index(directory, index): fh.write(f"## {pretty_dir(directory)}\n\n") fh.write("\n".join(index)) +def extract_md_heading(fileobj, fallback): + try: + with open(fileobj.path) as fh: + lines = fh.readlines() + except IOError as why: + sys.stderr.write(f"Error reading {filename}: {why}\n") + return fallback + for line in lines: + if line.startswith("# "): + return line[1:].strip() + return fallback if __name__ == "__main__": import sys - spider(sys.argv[1]) + for directory in sys.argv[1:]: + spider(directory, True)