art_strategies.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env python3
  2. import datetime
  3. import os.path
  4. import shutil
  5. import yaml
  6. import art_lib
  7. try:
  8. from os import scandir
  9. except ImportError:
  10. from scandir import scandir
  11. def main():
  12. PATH_SRC = 'jekyll'
  13. PATH_DIST = 'dist'
  14. PATH_DATA = 'data.yml'
  15. PATH_TEXTS = 'texts'
  16. FILE_TEMPLATE = '_posts/_template.html'
  17. FILE_CONFIG = '_config.yml'
  18. FILE_CONFIG_DEFAULTS = '_config.defaults.yml'
  19. DIR_POSTS = '_posts'
  20. TEXTS_EXT = '.yml'
  21. configs = art_lib.jekyll_get_configs(
  22. PATH_DATA, os.path.join(PATH_SRC, FILE_CONFIG_DEFAULTS)
  23. )
  24. for lang, config in configs:
  25. if lang is not None:
  26. path_dir = os.path.join(PATH_DIST, lang)
  27. else:
  28. path_dir = PATH_DIST
  29. shutil.rmtree(path_dir, ignore_errors=True)
  30. shutil.copytree(PATH_SRC, path_dir)
  31. path_config = os.path.join(path_dir, FILE_CONFIG)
  32. path_template = os.path.join(PATH_SRC, FILE_TEMPLATE)
  33. path_posts = os.path.join(path_dir, DIR_POSTS)
  34. path_texts = os.path.join(PATH_TEXTS, lang)
  35. art_lib.mkdir_and_write(
  36. path_config, yaml.dump(config, default_flow_style=False)
  37. )
  38. files = []
  39. for entry in sorted(scandir(path_texts), key=lambda x: x.name):
  40. if (
  41. not entry.name.startswith('.')
  42. and entry.is_file()
  43. and entry.name.endswith(TEXTS_EXT)
  44. ):
  45. files.append((entry.path, entry.name))
  46. for file in files:
  47. with open(file[0], 'r') as f:
  48. post_data = yaml.load(f.read())
  49. post_basename, _ = os.path.splitext(file[1])
  50. post_date = datetime.datetime.strptime(
  51. post_data['date'], '%Y-%m-%d'
  52. )
  53. if 'fontsize' in post_data:
  54. post_fontsize = post_data['fontsize']
  55. else:
  56. post_fontsize = None
  57. post_context = {
  58. 'yaml': yaml.dump(
  59. {
  60. 'name': art_lib.tex_to_plain(post_data['title']),
  61. 'timestamp': post_date.strftime('%Y-%m-%d %H:%M:%S'),
  62. 'title': art_lib.tex_to_plain(post_data['title']),
  63. 'heading': art_lib.tex_to_plain(post_data['heading']),
  64. 'text': art_lib.tex_to_plain(post_data['text']),
  65. 'classname': post_fontsize,
  66. },
  67. default_flow_style=False,
  68. )
  69. }
  70. art_lib.jekyll_write_post(
  71. path_template,
  72. path_posts,
  73. post_date.strftime('%Y-%m-%d'),
  74. post_basename,
  75. post_context,
  76. )
  77. if __name__ == '__main__':
  78. main()