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.

83 lines
2.5 KiB

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