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.

95 lines
2.7 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. import glob
  14. import markdown
  15. import os
  16. import re
  17. import shutil
  18. input_directory = 'content'
  19. static_directory = 'static'
  20. output_directory = 'output'
  21. md_extensions = ['fenced_code', 'codehilite', 'nl2br', 'toc', 'smarty', 'tables', 'linkify']
  22. def print_file(in_file, out_file):
  23. print('%-62s -> %s' % (in_file, out_file))
  24. template = open('template.html').read()
  25. os.makedirs(output_directory, exist_ok=True)
  26. for (dirpath, _, filenames) in os.walk(static_directory):
  27. for filename in filenames:
  28. source = os.path.join(dirpath, filename)
  29. out_path = dirpath.replace(static_directory, '', 1)
  30. out_path = out_path.lstrip('/')
  31. dest_dir = os.path.join(output_directory, out_path)
  32. os.makedirs(dest_dir, exist_ok=True)
  33. dest = os.path.join(dest_dir, filename)
  34. print_file(source, dest)
  35. shutil.copy2(source, dest)
  36. for (dirpath, _, filenames) in os.walk(input_directory):
  37. for filename in filenames:
  38. markdown_filename = os.path.join(dirpath, filename)
  39. if not markdown_filename.endswith('.md'):
  40. continue
  41. markdown_file = open(markdown_filename)
  42. text = markdown_file.read()
  43. markdown_file.close()
  44. if not text.startswith('# '):
  45. text = '# ' + text
  46. match = re.match(r'^(.*?)\n', text)
  47. if match:
  48. title = match.group(1).lstrip('# ')
  49. else:
  50. title = text
  51. title += ' | Colin McMillen'
  52. if markdown_filename == os.path.join(input_directory, 'index.md'):
  53. title = 'Colin McMillen'
  54. out_filename = os.path.basename(markdown_filename).replace('.md', '.html')
  55. out_dirpath = os.path.join(output_directory, dirpath)
  56. out_dirpath = out_dirpath.replace('/content', '', 1)
  57. out_fullpath = os.path.join(out_dirpath, out_filename)
  58. page_url = out_fullpath.replace('output/', '', 1)
  59. if page_url.endswith('index.html'): # strip off index.html
  60. page_url = page_url[:-len('index.html')]
  61. html = markdown.markdown(text, extensions=md_extensions, output_format='html5')
  62. output = template.replace('__TITLE_GOES_HERE__', title)
  63. output = output.replace('__CONTENT_GOES_HERE__', html)
  64. output = output.replace('__PAGE_URL_GOES_HERE__', page_url)
  65. os.makedirs(out_dirpath, exist_ok=True)
  66. print_file(markdown_filename, out_fullpath)
  67. out_file = open(out_fullpath, 'w')
  68. out_file.write(output)
  69. out_file.close()
  70. # TODO: make a sitemap / RSS?
  71. #index_filename = os.path.join(output_directory, 'index.html')
  72. #print_file('', index_filename)
  73. #index = open(index_filename, 'w')
  74. #for f in out_filenames:
  75. # index.write('<a href="%s">%s</a><br>' % (f, f))
  76. #index.close()