blog

Personal blog
git clone git://git.laack.co/blog.git
Log | Files | Refs

link_citation.py (896B)


      1 import re
      2 import sys
      3 from pathlib import Path
      4 
      5 def fix_citations(md_text: str) -> str:
      6     """
      7     Rewrites citations so they look like:
      8     [[1] https://example.com](https://example.com)
      9     """
     10     # Match lines like: [1] https://example.com OR [1] - https://example.com
     11     pattern = re.compile(r'^\[+(\d+)\]+[ \-]*\[*([^\]\s]+)\]*', re.MULTILINE)
     12 
     13     def repl(match):
     14         num = match.group(1)
     15         url = match.group(2)
     16         return f'[[{num}] {url}]({url})'
     17 
     18     return pattern.sub(repl, md_text)
     19 
     20 if __name__ == "__main__":
     21     if len(sys.argv) != 3:
     22         print("Usage: python fix_citations.py input.md output.md")
     23         sys.exit(1)
     24 
     25     infile, outfile = Path(sys.argv[1]), Path(sys.argv[2])
     26     text = infile.read_text(encoding="utf-8")
     27     fixed_text = fix_citations(text)
     28     outfile.write_text(fixed_text, encoding="utf-8")
     29     print(f"Citations rewritten in {outfile}")