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.

99 lines
2.9 KiB

  1. #!/usr/bin/env python3
  2. # Usage:
  3. #
  4. # cd ~/src/www-builder
  5. # python3 build.py
  6. # cd output
  7. # cp -r * ~/src/www-home
  8. # cd ~/src/www-home
  9. # git status
  10. # git add [some stuff]
  11. # git commit
  12. # git push
  13. # TODO: replace gallery.tinyletterapp.com images with locally hosted content.
  14. # TODO: in template.html, add apple touch icon, maybe other favicon sizes.
  15. # TODO: local mirrors of all papers in publications.html
  16. import glob
  17. import markdown
  18. import os
  19. import re
  20. import shutil
  21. input_directory = 'content'
  22. static_directory = 'static'
  23. output_directory = 'output'
  24. md_extensions = ['fenced_code', 'codehilite', 'nl2br', 'toc', 'smarty', 'tables', 'linkify']
  25. def print_file(in_file, out_file):
  26. print('%-62s -> %s' % (in_file, out_file))
  27. template = open('template.html').read()
  28. os.makedirs(output_directory, exist_ok=True)
  29. for (dirpath, _, filenames) in os.walk(static_directory):
  30. for filename in filenames:
  31. source = os.path.join(dirpath, filename)
  32. out_path = dirpath.replace(static_directory, '', 1)
  33. out_path = out_path.lstrip('/')
  34. dest_dir = os.path.join(output_directory, out_path)
  35. os.makedirs(dest_dir, exist_ok=True)
  36. dest = os.path.join(dest_dir, filename)
  37. print_file(source, dest)
  38. shutil.copy2(source, dest)
  39. for (dirpath, _, filenames) in os.walk(input_directory):
  40. for filename in filenames:
  41. markdown_filename = os.path.join(dirpath, filename)
  42. if not markdown_filename.endswith('.md'):
  43. continue
  44. markdown_file = open(markdown_filename)
  45. text = markdown_file.read()
  46. markdown_file.close()
  47. if not text.startswith('# '):
  48. text = '# ' + text
  49. match = re.match(r'^(.*?)\n', text)
  50. if match:
  51. title = match.group(1).lstrip('# ')
  52. else:
  53. title = text
  54. title += ' | Colin McMillen'
  55. if markdown_filename == os.path.join(input_directory, 'index.md'):
  56. title = 'Colin McMillen'
  57. out_filename = os.path.basename(markdown_filename).replace('.md', '.html')
  58. out_dirpath = os.path.join(output_directory, dirpath)
  59. out_dirpath = out_dirpath.replace('/content', '', 1)
  60. out_fullpath = os.path.join(out_dirpath, out_filename)
  61. page_url = out_fullpath.replace('output/', '', 1)
  62. if page_url.endswith('index.html'): # strip off index.html
  63. page_url = page_url[:-len('index.html')]
  64. html = markdown.markdown(text, extensions=md_extensions, output_format='html5')
  65. output = template.replace('__TITLE_GOES_HERE__', title)
  66. output = output.replace('__CONTENT_GOES_HERE__', html)
  67. output = output.replace('__PAGE_URL_GOES_HERE__', page_url)
  68. os.makedirs(out_dirpath, exist_ok=True)
  69. print_file(markdown_filename, out_fullpath)
  70. out_file = open(out_fullpath, 'w')
  71. out_file.write(output)
  72. out_file.close()
  73. # TODO: make a sitemap / RSS?
  74. #index_filename = os.path.join(output_directory, 'index.html')
  75. #print_file('', index_filename)
  76. #index = open(index_filename, 'w')
  77. #for f in out_filenames:
  78. # index.write('<a href="%s">%s</a><br>' % (f, f))
  79. #index.close()