diff --git a/Mermaid-build.py b/Mermaid-build.py new file mode 100644 index 0000000..83e94cd --- /dev/null +++ b/Mermaid-build.py @@ -0,0 +1,90 @@ +import sublime +import sublime_plugin +import os +import sys +import subprocess +import time + + +class MermaidBuildCommand(sublime_plugin.WindowCommand): + """Build command for Mermaid""" + + def handle_popen(self, handler): + """Stream output from mermaid-cli""" + err_collector = [] + while True: + stdout = handler.stdout + if not stdout: + break + output = stdout.readline() + if not output: + err = handler.stderr.readline() + if not err: + break + else: + err_collector.append(err.decode().rstrip()) + else: + print(output.rstrip(), flush=True) + if len(err_collector) > 0: + sublime.error_message("Issue occurred when rendering Mermaid diagram:\n" + "\n".join(err_collector)) + return False + return True + + def run(self): + """Run the build and convert the Mermaid diagram.""" + view = self.window.active_view() + if not view: + return + + settings = sublime.load_settings("mermaid.sublime-settings") + + mmdc_cmd = settings.get("mmdc_location", "mmdc") + build_settings = settings.get("build") + # remove unused settings + pruned_build_settings = {k: v for k, v in build_settings.items() if v} + + # set input file + pruned_build_settings["input"] = view.file_name() + + # make output file + output = os.path.splitext(view.file_name()) + outputFile = output[0] + if "outputFormat" not in pruned_build_settings: + outputFormat = "svg" + else: + outputFormat = pruned_build_settings["outputFormat"] + + outputFile = outputFile + "." + outputFormat + pruned_build_settings["output"] = outputFile + + # handle the weird singleton flag + pdfFit = None + pdfFit = pruned_build_settings.pop("pdfFit") + + # create flags then flatten for Popen arg + flags = [["--" + k, str(v)] for k, v in pruned_build_settings.items()] + flattened_flags = [mmdc_cmd] + [x for i in flags for x in i] + if outputFormat.lower() == "pdf" and pdfFit: + flattened_flags += ["--pdfFit"] + + # run mmdc + p = subprocess.Popen(flattened_flags, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + + # stream stdout and stderr + success = self.handle_popen(p) + + if success: + print("mmdc has finished generating. Rendered Mermaid is at " + outputFile) + sublime.status_message("Build finished") + + will_open = settings.get("open_in_default_app_after_build", False) + if will_open: + if sys.platform == "darwin": + opener = "open" + elif sys.platform == "linux": + opener = "xdg-open" + elif sys.platform == "win32": + opener = "Invoke-Item" + + p = subprocess.Popen([opener, outputFile]) + self.handle_popen(p) diff --git a/Mermaid.sublime-build b/Mermaid.sublime-build index 856e551..2f4a03a 100644 --- a/Mermaid.sublime-build +++ b/Mermaid.sublime-build @@ -1,13 +1,4 @@ { "selector" : "source.mermaid", - "cmd": [ - "mmdc", - "-i", "$file_name", - "-o", "$file_base_name.png", - //"-t", "dark", - "-b", "transparent", - "--width", "2048", - "--height", "2048", - "--scale", "2", - ], + "target": "mermaid_build" } diff --git a/mermaid.sublime-settings b/mermaid.sublime-settings index dde3c0e..97825d9 100644 --- a/mermaid.sublime-settings +++ b/mermaid.sublime-settings @@ -2,10 +2,29 @@ // Default values for mermaid.sublime-settings. // { - // Whether to dim links to reduce noise, - // for certain graphs with more links. - "quiet_graph_links": false, - // The theme to use for view-in-browser. - // Available: dark, default, forest, neutral. - "theme": "neutral" + // Absolute location of the mermaid-cli executable. If let blank it will check the PATH + "mmdc-location": "", + "build":{ + // The location where the rendered Mermaid diagram will be generated + // If left blank, it will be the same directory as the Mermaid file + "output": "", + // The format of the rendered Mermaid diagram + // Overrides the file extension used in `output`. Options are: 'svg', 'png', 'pdf' + "outputFormat": "png", + // The width of the page (mmdc defaults to 800) + "width": 2048, + // The height of the page (mmdc defaults to 600) + "height": 2048, + // The scale factor used when rendering the Mermaid diagram (mmdc defaults to 1) + "scale": 2, + // The background color for PNGs and SVGs. Accepts CSS color names or hex values + "backgroundColor": "transparent", + // Theme of the chart. Options are 'default', 'forest', 'dark', 'neutral' + "theme": "default", + // The CSS file to use when rendering the Mermaid diagram + "cssFile": "", + // Scale the PDF to fit the chart (if using PDF output) + "pdfFit": true + }, + "open_in_default_app_after_build": false }