forked from victorporof/Sublime-HTMLPrettify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HTMLPrettify.py
176 lines (144 loc) · 6.56 KB
/
HTMLPrettify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import sublime, sublime_plugin
import os, sys, subprocess, codecs, webbrowser
try:
import commands
except ImportError:
pass
PLUGIN_FOLDER = os.path.dirname(os.path.realpath(__file__))
RC_FILE = ".jsbeautifyrc"
SETTINGS_FILE = "HTMLPrettify.sublime-settings"
KEYMAP_FILE = "Default ($PLATFORM).sublime-keymap"
OUTPUT_VALID = b"*** HTMLPrettify output ***"
class HtmlprettifyCommand(sublime_plugin.TextCommand):
def run(self, edit):
previous_selection = [(region.a, region.b) for region in self.view.sel()]
previous_position = self.view.viewport_position()
if PLUGIN_FOLDER.find(u".sublime-package") != -1:
# Can't use this plugin if installed via the Package Manager in Sublime
# Text 3, because it will be zipped into a .sublime-package archive.
# Thus executing scripts *located inside this archive* via node.js
# will, unfortunately, not be possible.
url = "https://github.com/victorporof/Sublime-HTMLPrettify#manually"
msg = """You won't be able to use this plugin in Sublime Text 3 when \
installed via the Package Manager.\n\nPlease remove it and install manually, \
following the instructions at:\n"""
sublime.ok_cancel_dialog(msg + url)
webbrowser.open(url)
return
# Get the current text in the buffer.
bufferText = self.view.substr(sublime.Region(0, self.view.size()))
# ...and save it in a temporary file. This allows for scratch buffers
# and dirty files to be beautified as well.
namedTempFile = ".__temp__"
tempPath = PLUGIN_FOLDER + "/" + namedTempFile
print("Saving buffer to: " + tempPath)
f = codecs.open(tempPath, mode='w', encoding='utf-8')
f.write(bufferText)
f.close()
# Simply using `node` without specifying a path sometimes doesn't work :(
settings = sublime.load_settings(SETTINGS_FILE)
if exists_in_path("nodejs"):
node = "nodejs"
elif exists_in_path("node"):
node = "node"
else:
node = settings.get("node_path")
output = ""
try:
print("Plugin folder is: " + PLUGIN_FOLDER)
scriptPath = PLUGIN_FOLDER + "/scripts/run.js"
filePath = self.view.file_name()
output = get_output([node, scriptPath, tempPath, filePath or "?"])
# Make sure the correct/expected output is retrieved.
if output.find(OUTPUT_VALID) == -1:
print(output)
cmd = node + " " + scriptPath + " " + tempPath + " " + filePath
msg = "Command " + cmd + " created invalid output"
raise Exception(msg)
except:
# Something bad happened.
print("Unexpected error({0}): {1}".format(sys.exc_info()[0], sys.exc_info()[1]))
# Usually, it's just node.js not being found. Try to alleviate the issue.
msg = "Node.js was not found in the default path. Please specify the location."
if sublime.ok_cancel_dialog(msg):
open_htmlprettify_sublime_settings(self.view.window())
else:
msg = "You won't be able to use this plugin without specifying the path to Node.js."
sublime.error_message(msg)
return
# Remove the output identification marker (first line).
output = output[len(OUTPUT_VALID) + 1:]
os.remove(tempPath)
# We're done with beautifying, change the text shown in the current buffer.
self.view.erase_regions("jshint_errors")
if len(output) > 0:
region = sublime.Region(0, self.view.size())
text = output.decode("utf-8")
if self.view.settings().get("ensure_newline_at_eof_on_save") and not text.endswith("\n"):
text += "\n"
self.view.replace(edit, region, text)
self.view.set_viewport_position((0, 0,), False)
self.view.set_viewport_position(previous_position, False)
self.view.sel().clear()
for a, b in previous_selection:
self.view.sel().add(sublime.Region(a, b))
class PreSaveFormatListner(sublime_plugin.EventListener):
def on_pre_save(self, view):
settings = sublime.load_settings(SETTINGS_FILE)
view_settings = view.settings()
should_format = view_settings.get('prettify_format_on_save', settings.get('format_on_save'))
if should_format == True:
view.run_command("htmlprettify")
class HtmlprettifySetPrettifyPrefsCommand(sublime_plugin.TextCommand):
def run(self, edit):
open_jsbeautify_rc(self.view.window())
class HtmlprettifySetPluginOptionsCommand(sublime_plugin.TextCommand):
def run(self, edit):
open_htmlprettify_sublime_settings(self.view.window())
class HtmlprettifySetKeyboardShortcutsCommand(sublime_plugin.TextCommand):
def run(self, edit):
open_jshint_sublime_keymap(self.view.window(), {
"windows": "Windows", "linux": "Linux", "osx": "OSX"
}.get(sublime.platform()))
class HtmlprettifySetNodePathCommand(sublime_plugin.TextCommand):
def run(self, edit):
open_htmlprettify_sublime_settings(self.view.window())
def open_jsbeautify_rc(window):
window.open_file(PLUGIN_FOLDER + "/" + RC_FILE)
def open_htmlprettify_sublime_settings(window):
window.open_file(PLUGIN_FOLDER + "/" + SETTINGS_FILE)
def open_jshint_sublime_keymap(window, platform):
window.open_file(PLUGIN_FOLDER + "/" + KEYMAP_FILE.replace("$PLATFORM", platform))
def exists_in_path(cmd):
# Can't search the path if a directory is specified.
assert not os.path.dirname(cmd)
path = os.environ.get("PATH", "").split(os.pathsep)
extensions = os.environ.get("PATHEXT", "").split(os.pathsep)
# For each directory in PATH, check if it contains the specified binary.
for directory in path:
base = os.path.join(directory, cmd)
options = [base] + [(base + ext) for ext in extensions]
for filename in options:
if os.path.isfile(filename):
return True
return False
def get_output(cmd):
if int(sublime.version()) < 3000:
if sublime.platform() != "windows":
# Handle Linux and OS X in Python 2.
run = '"' + '" "'.join(cmd) + '"'
return commands.getoutput(run)
else:
# Handle Windows in Python 2.
# Hack to prevent console window from showing. Stolen from
# http://stackoverflow.com/questions/1813872/running-a-process-in-pythonw-with-popen-without-a-console
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
return subprocess.Popen(cmd, stdout=subprocess.PIPE, startupinfo=startupinfo).communicate()[0]
else:
# Handle all OS in Python 3.
run = '"' + '" "'.join(cmd) + '"'
return subprocess.check_output(run, stderr=subprocess.STDOUT, shell=True)