#!/usr/bin/env python import argparse import os from pathlib import Path import re import sys from typing import Sequence, cast sys.path.append(os.path.dirname(os.path.abspath(__file__))) from fetch_discourse_md import fetch as md_fetch # from fetch_discourse_md import fetch as md_fetch CMDRUN_RE = r"" def debug(*o, **kargs): if os.getenv("DEBUG") in ["1", "on", "yes", "true", "TRUE", "True"]: return print("[DEBUG]:", *(map(str, o)), file=sys.stderr, **kargs) def transcribe(execute=False, files_with_cmdrun: Sequence[Path] = []): def handler(match: re.Match[str]) -> str: url = match.group(1) res = md_fetch(url) or "" if (s := len(res)) > 0: debug(" " * 4, "Fetched", s, "characters") return res for md_file in files_with_cmdrun: debug(md_file) # Get the discourse url with open(md_file) as f: contents = f.read() new_content = re.sub(CMDRUN_RE, handler, contents) if new_content != contents: # Report we got a file replaced if m := re.search(CMDRUN_RE, contents): print(f"{md_file}\t{m.group(1)}") del m if execute: with open(md_file, "w") as f: f.write(new_content) else: debug(" " * 4, "SKIPPED: flag '-r' is unset") if __name__ == "__main__": argparser = argparse.ArgumentParser( description="Utility to transcribe md files using '