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.

73 lines
2.1 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('%-53s -> %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. out_filenames = []
  26. # TODO: also walk the markdown directory
  27. for markdown_filename in glob.glob(os.path.join(input_directory, '*.md')):
  28. markdown_file = open(markdown_filename)
  29. text = markdown_file.read()
  30. markdown_file.close()
  31. if not text.startswith('# '):
  32. text = '# ' + text
  33. match = re.match(r'^(.*?)\n', text)
  34. if match:
  35. title = match.group(1).lstrip('#')
  36. else:
  37. title = text
  38. title += ' | Colin McMillen'
  39. if markdown_filename == os.path.join(input_directory, 'index.md'):
  40. title = 'Colin McMillen'
  41. html = markdown.markdown(text, extensions=md_extensions, output_format='html5')
  42. output = template.replace('__TITLE_GOES_HERE__', title)
  43. output = output.replace('__CONTENT_GOES_HERE__', html)
  44. out_filename = os.path.basename(markdown_filename).replace('.md', '.html')
  45. out_filenames.append(out_filename)
  46. out_fullpath = os.path.join(output_directory, out_filename)
  47. print_file(markdown_filename, out_fullpath)
  48. out_file = open(out_fullpath, 'w')
  49. out_file.write(output)
  50. out_file.close()
  51. # TODO: make a sitemap / RSS?
  52. #index_filename = os.path.join(output_directory, 'index.html')
  53. #print_file('', index_filename)
  54. #index = open(index_filename, 'w')
  55. #for f in out_filenames:
  56. # index.write('<a href="%s">%s</a><br>' % (f, f))
  57. #index.close()