Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions GraphvizPreview.py
Original file line number Diff line number Diff line change
@@ -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()))
4 changes: 3 additions & 1 deletion helpers.py
Original file line number Diff line number Diff line change
@@ -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)

Expand Down