I just switched to sublime text 2 yesterday and I’m loving it so far.
I’m swicthing from Notepad++ and there’s just a few things I’m missing that I can’t live without.
One is XML tools (actually it’s a Notepad++ plugin).
I searched around and stumbled upon an implementation at gist.github.com/1138554 , but that didn’t quite work out for me so i made a few tweaks
Modifications
- Supports XML Validation
- You don’t have to select all text prior to running it
- It does a syntax check and pretty at save
- Normalized line endings (My line endings were getting screwed up whenever i used it)
There’s definitely room for clean-up & improvement but I’m posting it here for now to gauge interest. I’m currently satisfied with it’s functionality
[code]import sublime, sublime_plugin, subprocess
class TidyXmlCommand(sublime_plugin.TextCommand):
def run(self, edit):
# update this path to the location of the xmllint.exe file,
# in this case it is d:\Tools\libxml - verify that all dependencies are satisfied
# by running it via command-line
command = ‘D:\Tools\libxml\xmllint.exe --format --recover -’
# help from https://forum.sublimetext.com/t/run-file-contents-through-external-command/2177/4
XmlRegion = sublime.Region(0, self.view.size())
p = subprocess.Popen(command, bufsize=-1, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, shell=True)
result, err = p.communicate(self.view.substr(XmlRegion))
if err != "":
sublime.error_message("XML failed syntax validation!\n\n"+err)
self.view.set_status('tidyxml', "tidyxml: "+err)
sublime.set_timeout(self.clear,10000)
else:
result = self.normalize_line_endings(result)
self.view.replace(edit, XmlRegion, result)
sublime.set_timeout(self.clear,0)
def clear(self):
self.view.erase_status(‘tidyxml’)
def normalize_line_endings(self, string):
string = string.replace(’\r\n’, ‘\n’).replace(’\r’, ‘\n’)
line_endings = self.view.settings().get(‘default_line_ending’)
if line_endings == ‘windows’:
string = string.replace(’\n’, ‘\r\n’)
elif line_endings == ‘mac’:
string = string.replace(’\n’, ‘\r’)
return string
class EnsureTidyXml(sublime_plugin.EventListener):
def on_pre_save(self, view):
if view.settings().get(‘syntax’) == “Packages/XML/XML.tmLanguage”:
view.run_command(‘tidy_xml’)[/code]