Sito personale di Elia Argentieri. https://elinvention.ovh/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

nikola.py 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/bin/python3
  2. import sys
  3. import os
  4. import dateutil.parser
  5. if len(sys.argv) < 3:
  6. print("Specify your nikola site's folder and hugo's content directory")
  7. sys.exit(-1)
  8. nikola_path = sys.argv[1]
  9. hugo_path = sys.argv[2]
  10. META_STR = '%s = "%s"'
  11. if not (os.path.isdir(nikola_path) and os.path.isdir(hugo_path)):
  12. print("Please specify 2 valid folders")
  13. sys.exit(-1)
  14. conv = 0
  15. for ls in os.listdir(nikola_path):
  16. npost = os.path.join(nikola_path, ls)
  17. hpost = os.path.join(hugo_path, os.path.split(npost)[1])
  18. print(npost, "->", hpost)
  19. with open(npost, 'r') as nfile, open(hpost, 'w') as hfile:
  20. print("+++", file=hfile)
  21. for line in nfile:
  22. line = line.strip()
  23. if line in ["", "<!--"]:
  24. continue
  25. if line.endswith("-->") or not line.startswith(".."):
  26. break
  27. k, v = [l.strip() for l in line[3:].split(':', 1)]
  28. if k in [ "title", "slug", "image", "description" ]:
  29. if v != "":
  30. print(META_STR % (k, v), file=hfile)
  31. elif k == "date":
  32. d = dateutil.parser.parse(v)
  33. print(META_STR % (k, d.isoformat()), file=hfile)
  34. elif k == "tags":
  35. tags = [ t.strip() for t in v.split(',') if t != ""]
  36. if "draft" in tags:
  37. print("draft = true", file=hfile)
  38. tags.remove("draft")
  39. if len(tags) > 0:
  40. print(k, "=", str(tags).replace("'", '"'), file=hfile)
  41. else:
  42. print("Ignored meta-tag: ", META_STR % (k, v))
  43. print("+++", file=hfile)
  44. for line in nfile:
  45. hfile.write(line)
  46. conv += 1
  47. print("%d/%d converted" % (conv, len(os.listdir(nikola_path))))