PageRenderTime 67ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/Packages/Octopress/octopress.py

https://bitbucket.org/gyk001/st2cfg
Python | 189 lines | 180 code | 9 blank | 0 comment | 2 complexity | 26006c57127ebe046303afa55560b47a MD5 | raw file
  1. import functools
  2. import os
  3. import re
  4. import sublime
  5. import sublime_plugin
  6. import subprocess
  7. import thread
  8. import glob
  9. class OctopressCommand(sublime_plugin.WindowCommand):
  10. def exec_command(self, command):
  11. print "octopress exec start."
  12. self.output = ""
  13. self.load_config()
  14. os.chdir(self.octopress_path)
  15. exec_command = "%s %s" % (self.rake_command, command)
  16. self.proc = subprocess.Popen(exec_command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
  17. thread.start_new_thread(self.read_stdout, ())
  18. def load_config(self):
  19. octo_set = sublime.load_settings("octopress.sublime-settings")
  20. self.octopress_path = octo_set.get("octopress_path")
  21. if self.octopress_path[-1] != os.sep:
  22. self.octopress_path += os.sep
  23. pre_rake = octo_set.get("octopress_cmd_before_rake")
  24. if (pre_rake != ''):
  25. pre_rake += '; '
  26. self.rake_command = pre_rake + "rake"
  27. use_bundle = octo_set.get("use_bundle")
  28. if use_bundle:
  29. self.rake_command = pre_rake + "bundle exec rake"
  30. print self.rake_command
  31. def show_status_message(self, msg):
  32. sublime.status_message(msg)
  33. def finish(self):
  34. finded = re.search(self.check_str, self.output)
  35. if finded:
  36. sublime.status_message("octopress exec successfully finished.")
  37. if self.file:
  38. print "Open File : " + self.file
  39. self.window.open_file(self.octopress_path + self.file)
  40. else:
  41. sublime.status_message("")
  42. sublime.error_message("Octopress exec failed. Please check octopress env, and try again.\n\nYou can check exec log in sublime console.")
  43. def read_stdout(self):
  44. while True:
  45. sublime.set_timeout(functools.partial(self.show_status_message, "octopress running..."), 0)
  46. data = os.read(self.proc.stdout.fileno(), 2 ** 15)
  47. if data != "":
  48. print data
  49. if self.do_open_file and self.check_str and data.startswith(self.check_str):
  50. self.file = data.split(os.linesep)[0].replace(self.check_str, "")
  51. self.output += data
  52. else:
  53. self.proc.stdout.close()
  54. print "octopress exec end."
  55. sublime.set_timeout(functools.partial(self.finish), 0)
  56. break
  57. class OctopressNewPostCommand(OctopressCommand):
  58. def run(self):
  59. self.window.show_input_panel("Enter Name Of New Post", "", self.on_done, None, None)
  60. def on_done(self, text):
  61. command = " \"new_post[%s]\"" % text
  62. self.check_str = "Creating new post: "
  63. self.do_open_file = True
  64. self.exec_command(command)
  65. class OctopressNewPageCommand(OctopressCommand):
  66. def run(self):
  67. self.window.show_input_panel("Enter Name Of New Page", "", self.on_done, None, None)
  68. def on_done(self, text):
  69. command = " \"new_page[%s]\"" % text
  70. self.check_str = "Creating new page: "
  71. self.do_open_file = True
  72. self.exec_command(command)
  73. class OctopressGenerateCommand(OctopressCommand):
  74. def run(self):
  75. self.file = ""
  76. self.check_str = "Successfully generated site:"
  77. self.do_open_file = False
  78. self.exec_command("generate")
  79. class OctopressDeployCommand(OctopressCommand):
  80. def run(self):
  81. self.file = ""
  82. self.check_str = "(## Github Pages deploy complete|OK)"
  83. self.do_open_file = False
  84. self.exec_command("deploy")
  85. class OctopressGenerateAndDeployCommand(OctopressCommand):
  86. def run(self):
  87. self.file = ""
  88. self.check_str = "(## Github Pages deploy complete|OK)"
  89. self.do_open_file = False
  90. self.exec_command("gen_deploy")
  91. class OctopressAutoGenerate(sublime_plugin.EventListener):
  92. def on_post_save(self, view):
  93. global_settings = sublime.load_settings(__name__ + '.sublime-settings')
  94. octo_set = sublime.load_settings("octopress.sublime-settings")
  95. self.octopress_onsave_action = octo_set.get("octopress_onsave_action")
  96. self.octopress_path = octo_set.get("octopress_path")
  97. valid_actions = ["", "generate", "deploy", "generate_and_deploy"]
  98. if (self.octopress_onsave_action not in valid_actions):
  99. # No valid on_save action present, abort
  100. sublime.error_message("Given action: '" + self.octopress_onsave_action + "' is not valid")
  101. return
  102. if (self.octopress_onsave_action == ""):
  103. # They don't want an onsave, so abort
  104. return
  105. if not re.search(self.octopress_path + ".*", view.file_name()):
  106. # current file being saved is not in the octopress path
  107. return
  108. view.window().run_command('octopress_' + self.octopress_onsave_action)
  109. class OctopressOpenExistingPostCommand(sublime_plugin.TextCommand):
  110. def run(self, edit):
  111. octo_set = sublime.load_settings("octopress.sublime-settings")
  112. self.octopress_path = octo_set.get("octopress_path")
  113. self.posts_dir = octo_set.get("octopress_posts_dir")
  114. files = [f for f in os.listdir(self.octopress_path + "/source/" + self.posts_dir)]
  115. files.reverse()
  116. self.quick_panel_items = files
  117. self.view.window().show_quick_panel(self.quick_panel_items, self.on_done, sublime.MONOSPACE_FONT)
  118. def on_done(self, index):
  119. if (index == -1):
  120. return
  121. sublime.active_window().run_command("open_file", {"file":self.octopress_path + "/source/" + self.posts_dir + "/" + self.quick_panel_items[index]})
  122. return
  123. class OctopressOpenExistingPageCommand(OctopressOpenExistingPostCommand):
  124. def run(self, edit):
  125. octo_set = sublime.load_settings("octopress.sublime-settings")
  126. self.octopress_path = octo_set.get("octopress_path")
  127. self.page_extension = octo_set.get("octopress_page_extension")
  128. files = glob.glob(self.octopress_path + "/source/*/index." + self.page_extension);
  129. files.reverse()
  130. self.quick_panel_items = []
  131. for file in files:
  132. self.quick_panel_items.append(file.replace(self.octopress_path + "/source/", ""))
  133. self.view.window().show_quick_panel(self.quick_panel_items, self.on_done, sublime.MONOSPACE_FONT)
  134. def on_done(self, index):
  135. if (index == -1):
  136. return
  137. sublime.active_window().run_command("open_file", {"file":self.octopress_path + "/source/" + self.quick_panel_items[index]})
  138. return