Skip to content

Commit

Permalink
Merge pull request #59 from overlord/master
Browse files Browse the repository at this point in the history
#47 - support [png, svg, txt, utxt, latex] output image formats
  • Loading branch information
jvantuyl committed Feb 23, 2018
2 parents b3563b1 + ea9b4b5 commit 1081f39
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 22 deletions.
7 changes: 4 additions & 3 deletions Diagram.sublime-settings
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
// restart sublime to apply the changes
"viewer" : "Preview", // Preview, QuickLook, EyeOfGnome, WindowsDefaultViewer
// restart sublime to apply the changes
"viewer": "Preview", // Preview, QuickLook, EyeOfGnome, WindowsDefaultViewer
"check_on_startup": true,
"new_file":false, // generate new file each time
"new_file": false, // generate new file each time
"output_format": "png", // valid values: png, svg, txt, utxt, latex
"charset": null // Can be "UTF-8" - to support non-latin text in diagram
}
7 changes: 4 additions & 3 deletions diagram/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ def setup():
try:
print("Loading processor class: %r" % processor)
proc = processor()
proc.CHARSET = sublime_settings.get('charset')
proc.CHECK_ON_STARTUP = sublime_settings.get('check_on_startup')
proc.NEW_FILE = sublime_settings.get('new_file')
proc.CHARSET = sublime_settings.get('charset', None)
proc.CHECK_ON_STARTUP = sublime_settings.get('check_on_startup', True)
proc.NEW_FILE = sublime_settings.get('new_file', True)
proc.OUTPUT_FORMAT = sublime_settings.get('output_format', 'png')
proc.load()
ACTIVE_PROCESSORS.append(proc)
print("Loaded processor: %r" % proc)
Expand Down
56 changes: 40 additions & 16 deletions diagram/plantuml.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,40 @@
IS_MSWINDOWS = (platform() == 'windows')
CREATE_NO_WINDOW = 0x08000000 # See MSDN, http://goo.gl/l4OKNe
EXTRA_CALL_ARGS = {'creationflags': CREATE_NO_WINDOW, 'shell': True} if IS_MSWINDOWS else {}
OUTPUT_FORMAT_DICT = {
'png': ('-tpng', '.png'), # generate images using PNG format
'svg': ('-tsvg', '.svg'), # generate images using SVG format
'txt': ('-ttxt', '.txt'), # generate images with ASCII art
'utxt': ('-tutxt', '.utxt'), # generate images with ASCII art using Unicode characters
'latex': ('-tlatex', '.tex') # generate images using LaTeX/Tikz format
# ------------------------------
# These formats are also supported by plantUml, but they need some prerequisite to be installed...
# PlantUml return_code = 1 on invocation.
# 'pdf': ('-tpdf', '.pdf'), # generate images using PDF format
# 'vdx': ('-tvdx', '.vdx'), # generate images using VDX format
# 'eps': ('-teps', '.eps'), # generate images using EPS format
}

class PlantUMLDiagram(BaseDiagram):
def __init__(self, processor, sourceFile, text):
super(PlantUMLDiagram, self).__init__(processor, sourceFile, text)

output_format = self.proc.OUTPUT_FORMAT
self.output_format_arg, self.output_file_extension = OUTPUT_FORMAT_DICT.get(output_format, (None, None))
if not self.output_format_arg:
raise Exception("Unsupported value '%s' for setting 'output_format'" % (output_format))

self.workDir = None
if sourceFile is None:
self.file = NamedTemporaryFile(prefix='untitled', suffix='.png', delete=False)

self.file = NamedTemporaryFile(prefix='untitled', suffix=self.output_file_extension, delete=False)
else:
sourceDir = dirname(sourceFile)
if exists(sourceDir):
self.workDir = sourceDir
if self.proc.NEW_FILE:
self.file = NamedTemporaryFile(prefix=sourceFile, suffix='.png', delete=False)
self.file = NamedTemporaryFile(prefix=sourceFile, suffix=self.output_file_extension, delete=False)
else:
sourceFile = splitext(sourceFile)[0] + '.png'
sourceFile = splitext(sourceFile)[0] + self.output_file_extension
self.file = open(sourceFile, 'w')

def generate(self):
Expand All @@ -52,37 +69,44 @@ def generate(self):
chdir(cwd)

def _generate(self):

# http://en.plantuml.com/command-line
command = [
'java',
'-DPLANTUML_LIMIT_SIZE=50000',
'-DPLANTUML_LIMIT_SIZE=50000', # max output image height
'-jar',
self.proc.plantuml_jar_path,
'-pipe',
'-tpng',
'-charset',
'UTF-8'
self.output_format_arg
]

charset = self.proc.CHARSET
if charset:
print('using charset: ' + charset)
command.append("-charset")
command.append(charset)
else:
print('using default charset: UTF-8')
command.append("-charset")
command.append('UTF-8')

print("Command: %s" % (command))

puml = execute(
command,
stdin=PIPE, stdout=self.file, stderr=DEVNULL,
stdin=PIPE,
stdout=self.file,
stderr=DEVNULL,
**EXTRA_CALL_ARGS
)
puml.communicate(input=self.text.encode('UTF-8'))
if puml.returncode != 0:
print("Error Processing Diagram:")
print("Error Processing Diagram, returncode=%s:" % (puml.returncode))
print(self.text)
return
else:
return self.file


class PlantUMLProcessor(BaseProcessor):
DIAGRAM_CLASS = PlantUMLDiagram
PLANTUML_VERSION = 8054
Expand All @@ -103,7 +127,7 @@ def check_dependencies(self):
)

if has_java is not 0:
raise Exception("can't find Java")
raise Exception("Can't find Java")

def check_plantuml_functionality(self):
puml = execute(
Expand All @@ -113,9 +137,9 @@ def check_plantuml_functionality(self):
self.plantuml_jar_path,
'-testdot'
],
stdin=DEVNULL,
stdout=PIPE,
stderr=STDOUT,
stdin=DEVNULL,
**EXTRA_CALL_ARGS
)

Expand All @@ -139,7 +163,7 @@ def find_plantuml_jar(self):
)
)
if not exists(self.plantuml_jar_path):
raise Exception("can't find " + self.plantuml_jar_file)
raise Exception("Can't find " + self.plantuml_jar_file)
print("Detected %r" % (self.plantuml_jar_path,))

def check_plantuml_version(self):
Expand All @@ -150,9 +174,9 @@ def check_plantuml_version(self):
self.plantuml_jar_path,
'-version'
],
stdin=DEVNULL,
stdout=PIPE,
stderr=STDOUT,
stdin=DEVNULL,
**EXTRA_CALL_ARGS
)

Expand All @@ -165,7 +189,7 @@ def check_plantuml_version(self):
if not puml.returncode == 0:
raise Exception("PlantUML returned an error code")
if self.PLANTUML_VERSION_STRING not in str(version_output):
raise Exception("error verifying PlantUML version")
raise Exception("Error verifying PlantUML version")

def extract_blocks(self, view):
# If any Region is selected - trying to convert it, otherwise converting all @start-@end blocks in view
Expand Down

0 comments on commit 1081f39

Please sign in to comment.