converter.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/usr/bin/env python3
  2. import os
  3. # find all files with md
  4. # for each file
  5. # read file and find the first empty line
  6. # everything following it is the body
  7. # everything before is metadata
  8. # inside metadata convert tags from list with [] to a simple list
  9. # remove pipe from summary
  10. # remove triple dot
  11. # write the new file to old_file
  12. for root, dirs, files in os.walk('.'):
  13. for filename in files:
  14. if filename.endswith(('md', 'markdown')):
  15. fullpath = os.path.join(root, filename).lstrip('./')
  16. f = open(fullpath).readlines()
  17. found_meta = False
  18. for i, l in enumerate(f):
  19. if not l.strip():
  20. meta_end, content_begin = i - 1, i + 1
  21. found_meta = True
  22. break
  23. if found_meta:
  24. newpath = fullpath.split('.')
  25. newpath.insert(-1,'-new.')
  26. newpath = ''.join(newpath)
  27. new = open(''.join(newpath), 'w')
  28. new.writelines(['---\n'])
  29. new.writelines(f[0:3])
  30. new.writelines(f[3].replace('[', '').replace(']',''))
  31. new.writelines(f[4:meta_end - 2])
  32. new.writelines(['summary: ' + f[meta_end - 1].lstrip()])
  33. new.writelines(['---\n'])
  34. new.writelines(['\n'])
  35. new.writelines(f[content_begin:])
  36. new.close()
  37. os.rename(fullpath, fullpath.replace('.','-old.'))
  38. os.rename(newpath, fullpath)
  39. print("Successfuly converted {}".format(fullpath))
  40. else:
  41. print("Something fishy with {}".format(new.name))