/blog/make_autolinks.py

https://bitbucket.org/maghoff/radiosity · Python · 24 lines · 17 code · 5 blank · 2 comment · 4 complexity · 2b84b1931d0342c07e1c2970a4b8b67f MD5 · raw file

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import os, re
  4. r = re.compile('^((\d\d)-\d\d\d\d-\d\d-\d\d-.*).md$')
  5. buf = ''
  6. for f in os.listdir(os.curdir):
  7. m = r.match(f)
  8. if m:
  9. buf += '[%s]: %s.html\n' % (m.groups()[1], m.groups()[0])
  10. def should_write():
  11. try:
  12. f = open('autolinks.md', 'rb')
  13. old = f.read()
  14. f.close()
  15. return old != buf
  16. except:
  17. return True
  18. if should_write():
  19. open('autolinks.md', 'wb').write(buf)