fabfile.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/bin/env python
  2. import os
  3. import glob
  4. import subprocess
  5. from fabric.api import local
  6. # The keys are the file names of the Pandoc source files.
  7. # e.g. '01-frontpage.md'
  8. # The values are the slugs of the WordPress pages
  9. # e.g. http://www.swaroopch.com/notes/Python_en-Table_of_Contents
  10. MARKDOWN_FILES = {
  11. '01-frontpage.md' : 'Python',
  12. '02-table-of-contents.md' : 'Python_en-Table_of_Contents',
  13. }
  14. def _markdown_to_html(source_text):
  15. from_format = 'markdown'
  16. to_format = 'html'
  17. args = ['pandoc',
  18. '-f', from_format,
  19. '-t', to_format,
  20. '-S']
  21. p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
  22. output = p.communicate(source_text)[0]
  23. # TODO Replace image URLs with uploaded AWS S3 public URLs
  24. return output
  25. def html():
  26. for m in MARKDOWN_FILES.keys():
  27. converted_text = _markdown_to_html(open(m).read())
  28. output_file_name = MARKDOWN_FILES[m] + ".html"
  29. with open(output_file_name, 'w') as output:
  30. output.write(converted_text)
  31. local("open {}".format(output_file_name))
  32. def cleanup():
  33. for m in MARKDOWN_FILES.keys():
  34. output_file_name = MARKDOWN_FILES[m] + ".html"
  35. if os.path.exists(output_file_name):
  36. os.remove(output_file_name)
  37. def epub():
  38. from_format = 'markdown'
  39. to_format = 'epub'
  40. output_file_name = 'byte_of_python.epub'
  41. args = ['pandoc',
  42. '-f', from_format,
  43. '-t', to_format,
  44. '-o', output_file_name,
  45. '-S'] + MARKDOWN_FILES.keys()
  46. local(' '.join(args))