Skip to content

Commit

Permalink
a bit prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
mnot committed Aug 19, 2024
1 parent 8987f02 commit eadec07
Showing 1 changed file with 61 additions and 21 deletions.
82 changes: 61 additions & 21 deletions lib/dir_listing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,74 @@
ignore_prefixes = ['.', '_']
sep = "\n\n---\n\n"

def spider (directory):
def spider(directory, reverse=False):
index = []
sys.stderr.write(f"* scanning {directory}...\n")
dirs = []
files = []
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
for ignore in ignore_prefixes:
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"
Expand All @@ -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)

0 comments on commit eadec07

Please sign in to comment.