-
-
Save coldnebo/1138554 to your computer and use it in GitHub Desktop.
[ | |
{ "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): | |
command = 'tidy -xml -i -utf8 -w -q' | |
# 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')) | |
if err != "": | |
self.view.set_status('tidyxml', "tidyxml: "+err) | |
sublime.set_timeout(self.clear,10000) | |
else: | |
self.view.replace(edit, self.view.sel()[0], result.decode('utf-8')) | |
sublime.set_timeout(self.clear,0) | |
def clear(self): | |
self.view.erase_status('tidyxml') |
jtmille3's update gist sorted out carriage return problems for me on windows.. but thanks for all of this.. also i got confused and added HTMLTidy through Sublime Text 2 package control - no no no.. yes the windows binary were the right thing to download from http://tidy.sourceforge.net/#binaries and then where i put the exe file i added that location to the environment PATH variable by right clicking computer -> properties -> advanced system settings -> environment variables -> system variables -> PATH -> edit -> and adding the path to my exe in there.. on windows 7 x64.. thanks again for this it's great to have nice xml
jtmille3's update gist sorted out carriage return problems for me on windows.. but thanks for all of this.. also i got confused and added HTMLTidy through Sublime Text 2 package control - no no no.. yes the windows binary were the right thing to download from http://tidy.sourceforge.net/#binaries and then where i put the exe file i added that location to the environment PATH variable by right clicking computer -> properties -> advanced system settings -> environment variables -> system variables -> PATH -> edit -> and adding the path to my exe in there.. on windows 7 x64.. thanks again for this it's great to have nice xml
jtmille3's update gist sorted out carriage return problems for me on windows.. but thanks for all of this.. also i got confused and added HTMLTidy through Sublime Text 2 package control - no no no.. yes the windows binary were the right thing to download and then where i put the exe file i added that location to the environment PATH variable by right clicking computer -> properties -> advanced system settings -> environment variables -> system variables -> PATH -> edit -> and adding the path to my exe in there.. on windows 7 x64.. thanks again for this it's great to have nice xml
thanks for this.. jtmille3's version sorted the carriage return problems for on windows 7 x64.. nice to have pretty xml again.. thanks!
Really appreciate the time you put into this, man. Thanks.
Thank You! Works great on Windows with http://tidy.sourceforge.net/#binaries as specified.
Woot! Works great so far...thanks!
Tidy actually edits CDATA value and then wrongly handles what is after it.
e.g., The following "one-lined" XML. 'y = 3;' has got 2 tabulations before and one LF after. 'x * y = ?' has got 2 tabulations.
<?xml version="1.0" encoding="utf-8"?>
<report><bob>aze<![CDATA[ x = 2;
y = 3;
x * y = ?]]><alice>qsd</alice></bob></report>
After running tidy_xml.py, the output is the following
<?xml version="1.0" encoding="utf-8"?>
<report>
<bob>aze
<![CDATA[ x = 2;
y = 3;
x * y = ?]]>
<alice>qsd</alice></bob>
</report>
The CDATA value is not considered as a whole. The tabulations before 'y = 3;' and 'x * y = ?' are replaced with spaces from indentation.
xmllint handles that a little better, the CDATA value remains untouched, but still fail after that.
<?xml version="1.0" encoding="utf-8"?>
<report>
<bob>aze<![CDATA[ x = 2;
y = 3;
x * y = ?]]><alice>qsd</alice></bob>
</report>
thanks! help me a lot.
This is brilliant - thanks!
🍺
Thanks a lot... working perfectly fine.
I'm getting 'broken pipe' error. Any ideas?
Traceback (most recent call last):
File ".\sublime_plugin.py", line 362, in run_
File ".\tidy_xml.py", line 10, in run
File ".\subprocess.py", line 701, in communicate
File ".\subprocess.py", line 911, in _communicate
IOError: [Errno 32] Broken pipe
pretty_json is working, but tiny_xml isn't and there is no response when i use the key "ctrl+shift+x". i don't know why.
i know, there isn't libtidy. The tool is very good and thank you very much!
Changed Tidy xml to work with Sublime Text 3 Beta 3047
Added some error output to the console
nice work
thx
nice, work perfect to me.
Thx!
Pretty JSON works great for me and installs direct and clean through Package Control.
Very impressive. Thank you!
It is very good , Thank you !
See my updated gist. https://gist.github.com/3390999