Last active
December 26, 2015 18:19
-
-
Save protke/7193842 to your computer and use it in GitHub Desktop.
Sublime Text 3 Xml Prettifier Plugin, modified for W7. Created by André Bergonse http://www.bergspot.com/blog/2012/05/formatting-xml-in-sublime-text-2-xmllint/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sublime, sublime_plugin, subprocess | |
class TidyXmlLintCommand(sublime_plugin.TextCommand): | |
def run(self, edit): | |
command = "xmllint -format -encode utf-8 -" | |
# help from http://www.sublimetext.com/forum/viewtopic.php?f=2&p=12451 | |
# discussion on http://www.bergspot.com/blog/2012/05/formatting-xml-in-sublime-text-2-xmllint/ | |
if self.view.sel()[0].empty(): | |
xmlRegion = sublime.Region(0, self.view.size()) | |
else: | |
xmlRegion = self.view.sel()[0] | |
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).encode('utf-8')) | |
if err != b"": | |
self.view.set_status('xmllint', "xmllint: " + err.decode("utf-8")) | |
sublime.set_timeout(self.clear,10000) | |
else: | |
self.view.replace(edit, self.view.sel()[0], result.decode('utf-8').replace("\r","")) | |
sublime.set_timeout(self.clear,0) | |
def clear(self): | |
self.view.erase_status('xmllint') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment