Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save paoloantinori/4175305 to your computer and use it in GitHub Desktop.
Save paoloantinori/4175305 to your computer and use it in GitHub Desktop.
simple scripts to prettify your xml and json in sublime text 2
[
{ "keys": ["ctrl+shift+x"], "command": "tidy_xml" },
{ "keys": ["ctrl+shift+j"], "command": "prettify_json" }
]
import sublime, sublime_plugin, subprocess
class PrettifyJsonCommand(sublime_plugin.TextCommand):
def run(self, edit):
command = 'python -mjson.tool'
# help from http://www.sublimetext.com/forum/viewtopic.php?f=2&p=12451
p = subprocess.Popen(command, bufsize=-1, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, shell=True)
result, err = p.communicate(self.view.substr(self.view.sel()[0]).encode('utf-8'))
# gave up trying this approach: result always has '\n' strings in it that refuse to render
#result = json.dumps( self.view.substr(self.view.sel()[0]), indent=2)
# http://code.activestate.com/recipes/65211/ seems to say that Python "ruins" non-raw strings by
# actually placing '\','n' in the friggin string unless it's marked 'raw'? Is that true? Shouldn't a string be a string
# and the raw/not raw output be a function of the runtime? Why does "print" have some magic to reescape these strings and
# yet there are no other buffer objects that seem to do it (aka StringIO or BytesIO).
if result != "":
self.view.replace(edit, self.view.sel()[0], result.decode('utf-8'))
sublime.set_timeout(self.clear,0)
else:
self.view.set_status('tidyjson', "tidyjson: "+err)
sublime.set_timeout(self.clear,10000)
def clear(self):
self.view.erase_status('tidyjson')
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 = '/usr/bin/xmllint --format --recover -'
# help from http://www.sublimetext.com/forum/viewtopic.php?f=2&p=12451
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')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment