diff --git a/GraphvizPreview.py b/GraphvizPreview.py index 77edcf6..7878506 100644 --- a/GraphvizPreview.py +++ b/GraphvizPreview.py @@ -1,34 +1,44 @@ import sublime, sublime_plugin from subprocess import call +import os +import platform try: # python 3 from .helpers import surroundingGraphviz, graphvizPDF, ENVIRON except ValueError: # python 2 from helpers import surroundingGraphviz, graphvizPDF, ENVIRON +DOT_SYNTAX = 'Packages/Graphviz/DOT.tmLanguage' class GraphvizPreviewCommand(sublime_plugin.TextCommand): def run(self, edit): - sel = self.view.sel()[0] - - if not sel.empty(): - code = self.view.substr(sel).strip() - else: - code = surroundingGraphviz( - self.view.substr(sublime.Region(0, self.view.size())), - sel.begin() - ) + code = self.code() if not code: sublime.error_message('Graphviz: Please place cursor in graphviz code before running') return - pdf_filename = graphvizPDF(code) try: - call(['open', pdf_filename], env=ENVIRON) + if platform.system() == 'Windows': + os.startfile(pdf_filename) + else: + call(['open', pdf_filename], env=ENVIRON) except Exception as e: - sublime.error_message('Graphviz: Could not open PDF, only works for Mac... fork this repo for your OS!') + sublime.error_message('Graphviz: Could not open PDF, ' + str(e)) raise e + def code(self): + syntax = self.view.settings().get('syntax') + sel = self.view.sel()[0] + + if syntax == DOT_SYNTAX: + return self.content() + elif not sel.empty(): + code = self.view.substr(sel).strip() + else: + code = surroundingGraphviz(self.content(), sel.begin()) + + def content(self): + return self.view.substr(sublime.Region(0, self.view.size())) \ No newline at end of file diff --git a/helpers.py b/helpers.py index 6338010..a231cba 100644 --- a/helpers.py +++ b/helpers.py @@ -1,10 +1,12 @@ from subprocess import call import os +import platform import re import tempfile ENVIRON = os.environ -ENVIRON['PATH'] += ':/usr/local/bin' +if platform.system() != 'Windows': + ENVIRON['PATH'] += ':/usr/local/bin' DIGRAPH_START = re.compile('.*(digraph([ \t\n\r]+[a-zA-Z\200-\377_][a-zA-Z\200-\3770-9_]*[ \t\n\r]*{|[ \t\n\r]*{).*)', re.DOTALL | re.IGNORECASE)