Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. """ Script to convert a Nikola blog to Hugo
  4.  
  5. Requirements:
  6. - toml
  7.  
  8. Usage:
  9. ./nikola-to-hugo.py /path/to/nikola/blog
  10.  
  11. """
  12.  
  13. import os
  14. from os.path import abspath, dirname, exists, join, sep
  15. from shutil import copytree, rmtree
  16. import subprocess
  17. import sys
  18.  
  19. import toml
  20. from nikola import Nikola
  21.  
  22.  
  23. def nikola_site(path):
  24. sys.path.insert(0, path)
  25. import conf
  26. site = Nikola(**conf.__dict__)
  27. site.init_plugins()
  28. site.scan_posts()
  29. return site
  30.  
  31.  
  32. def create_hugo_site(path):
  33. hugo_path = '{}-hugo'.format(path.rstrip(sep))
  34. subprocess.call(['hugo', 'new', 'site', hugo_path])
  35. return hugo_path
  36.  
  37.  
  38. def convert_blog(path):
  39. site = nikola_site(path)
  40. hugo_path = create_hugo_site(path)
  41. update_conf(site.config, hugo_path)
  42. for post in site.timeline:
  43. convert_post(site, post, path, hugo_path)
  44. copy_static_files(path, hugo_path)
  45.  
  46.  
  47. def copy_static_files(path, hugo_path):
  48. files = join(path, 'files')
  49. static = join(hugo_path, 'static')
  50. rmtree(static, ignore_errors=True)
  51. copytree(files, static)
  52.  
  53.  
  54. def update_conf(nikola_conf, hugo_path):
  55. conf = join(hugo_path, 'config.toml')
  56. with open(conf) as f:
  57. hugo_config = toml.load(f)
  58. hugo_config['baseurl'] = nikola_conf['BASE_URL']
  59. hugo_config['title'] = nikola_conf['BLOG_TITLE']()
  60. if nikola_conf['COMMENT_SYSTEM'] == 'disqus':
  61. hugo_config['disqusShortname'] = nikola_conf['COMMENT_SYSTEM_ID']
  62. with open(conf, 'w') as g:
  63. toml.dump(hugo_config, g)
  64.  
  65.  
  66. def get_post_content(site, src):
  67. compiler = site.get_compiler(src)
  68. with open(src) as f:
  69. return compiler.split_metadata(f.read())[1]
  70.  
  71.  
  72. def toml_metadata(post):
  73. meta = post.meta['en']
  74. if 'tags' in meta:
  75. meta['tags'] = post.tags
  76. if 'date' in meta:
  77. meta['date'] = post.date
  78. if 'updated' in meta:
  79. meta['updated'] = post.updated
  80. if post.is_draft or post.is_private:
  81. meta['draft'] = True
  82. meta['type'] = 'post' if post.is_post else 'page'
  83. return toml.dumps(meta)
  84.  
  85.  
  86. def convert_post(site, post, path, hugo_path):
  87. src = join(path, post.source_path)
  88. dst = join(hugo_path, 'content', post.source_path)
  89. if not exists(dirname(dst)):
  90. os.mkdir(dirname(dst))
  91. meta = "+++\n{}\n+++\n\n".format(toml_metadata(post))
  92. content = get_post_content(site, src)
  93. with open(dst, 'w') as f:
  94. f.write(meta)
  95. f.write(content)
  96.  
  97.  
  98. def main():
  99. if len(sys.argv) != 2:
  100. print(__doc__)
  101. sys.exit(1)
  102. path = abspath(sys.argv[1])
  103. convert_blog(path)
  104.  
  105.  
  106. if __name__ == '__main__':
  107. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement