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.

136 lines
3.9 KiB

  1. #!/usr/bin/env python3
  2. # Assumes that there's a directory named ~/src/www-home which is a git repo
  3. # that the contents of output/ can be copied to, committed, & pushed to the
  4. # production server.
  5. # TODO: replace gallery.tinyletterapp.com images with locally hosted content.
  6. # TODO: in template.html, add apple touch icon, maybe other favicon sizes.
  7. # TODO: local mirrors of all papers in publications.html
  8. import argparse
  9. import glob
  10. import markdown
  11. import os
  12. import re
  13. import shutil
  14. input_directory = 'content'
  15. static_directory = 'static'
  16. output_directory = 'output'
  17. deploy_directory = '~/src/www-home'
  18. md_extensions = ['fenced_code', 'codehilite', 'nl2br', 'toc', 'smarty', 'tables', 'linkify']
  19. def print_file(in_file, out_file):
  20. print('%-62s -> %s' % (in_file, out_file))
  21. def copy_static_files():
  22. for (dirpath, _, filenames) in os.walk(static_directory):
  23. for filename in filenames:
  24. source = os.path.join(dirpath, filename)
  25. out_path = dirpath.replace(static_directory, '', 1)
  26. out_path = out_path.lstrip('/')
  27. dest_dir = os.path.join(output_directory, out_path)
  28. os.makedirs(dest_dir, exist_ok=True)
  29. dest = os.path.join(dest_dir, filename)
  30. print_file(source, dest)
  31. shutil.copy2(source, dest)
  32. def process_markdown_files():
  33. template = open('template.html').read()
  34. for (dirpath, _, filenames) in os.walk(input_directory):
  35. for filename in filenames:
  36. markdown_filename = os.path.join(dirpath, filename)
  37. if not markdown_filename.endswith('.md'):
  38. continue
  39. markdown_file = open(markdown_filename)
  40. text = markdown_file.read()
  41. markdown_file.close()
  42. if not text.startswith('# '):
  43. text = '# ' + text
  44. match = re.match(r'^(.*?)\n', text)
  45. if match:
  46. title = match.group(1).lstrip('# ')
  47. else:
  48. title = text
  49. title += ' | Colin McMillen'
  50. if markdown_filename == os.path.join(input_directory, 'index.md'):
  51. title = 'Colin McMillen'
  52. out_filename = os.path.basename(markdown_filename).replace('.md', '.html')
  53. out_dirpath = os.path.join(output_directory, dirpath)
  54. out_dirpath = out_dirpath.replace('/content', '', 1)
  55. out_fullpath = os.path.join(out_dirpath, out_filename)
  56. page_url = out_fullpath.replace('output/', '', 1)
  57. if page_url.endswith('index.html'): # strip off index.html
  58. page_url = page_url[:-len('index.html')]
  59. html = markdown.markdown(text, extensions=md_extensions, output_format='html5')
  60. output = template.format(title=title, content=html, page_url=page_url)
  61. os.makedirs(out_dirpath, exist_ok=True)
  62. print_file(markdown_filename, out_fullpath)
  63. out_file = open(out_fullpath, 'w')
  64. out_file.write(output)
  65. out_file.close()
  66. def make_sitemap():
  67. sitemap_command = ' '.join("""
  68. find output -regextype posix-extended -regex '.*.(html|pdf)$' |
  69. grep -v ^output/google |
  70. grep -v ^output/drafts |
  71. perl -pe 's|output|https://www.mcmillen.dev|'
  72. > output/sitemap.txt""".split('\n'))
  73. os.system(sitemap_command)
  74. def make_rss(): # TODO: implement.
  75. pass
  76. def deploy_site():
  77. os.system('cp -r output/* %s' % deploy_directory)
  78. os.chdir(os.path.expanduser(deploy_directory))
  79. os.system('git add .')
  80. os.system('git commit -m "automated update from build.py"')
  81. os.system('git push')
  82. def main():
  83. parser = argparse.ArgumentParser()
  84. parser.add_argument(
  85. '--clean', action='store_true',
  86. help='wipe the output directory before running')
  87. parser.add_argument(
  88. '--fast', action='store_true',
  89. help='only rebuild content files')
  90. parser.add_argument(
  91. '--deploy', action='store_true',
  92. help='deploy the site by pushing to the www-home git repo')
  93. args = parser.parse_args()
  94. if args.clean:
  95. shutil.rmtree(output_directory)
  96. os.makedirs(output_directory, exist_ok=True)
  97. if not args.fast:
  98. copy_static_files()
  99. process_markdown_files()
  100. make_sitemap()
  101. make_rss()
  102. if args.deploy:
  103. deploy_site()
  104. if __name__ == '__main__':
  105. main()