Skip to content

Instantly share code, notes, and snippets.

@whats-up
Created September 8, 2012 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whats-up/3679948 to your computer and use it in GitHub Desktop.
Save whats-up/3679948 to your computer and use it in GitHub Desktop.
cotEditorでJSON整形するためのコード。
#!/usr/bin/python
# coding: UTF-8
# %%%{CotEditorXInput=Selection}%%%
# %%%{CotEditorXOutput=ReplaceSelection}%%%
#! /usr/bin/env python
# coding: utf-8
import subprocess
import sys
def getClipboardData():
p = subprocess.Popen(['pbpaste'], stdout=subprocess.PIPE)
retcode = p.wait()
data = p.stdout.read()
return data
def setClipboardData(data):
p = subprocess.Popen(['pbcopy'], stdin=subprocess.PIPE)
p.stdin.write(data)
p.stdin.close()
retcode = p.wait()
def jsonformat(strings):
tablevel=0
fstr=""
for s in strings:
if s==",":
fstr+=s+"\n"
tab=tablevel*"\t"
fstr+=tab
elif s=="{" or s=="[":
tablevel=tablevel+1
tab=tablevel*"\t"
fstr+="\n"+tab+s+"\n"+tab
elif s=="}" or s=="]":
tab=tablevel*"\t"
fstr+="\n"+tab+s+"\n"
tablevel=tablevel-1
fstr+=tab
elif s=="" or s=="\t"or s=="\n":
pass
else:
fstr+=s
return fstr
def getclip():
text=""
for line in sys.stdin:
text+=line
formatedtext=jsonformat(text)
print formatedtext
def main():
getclip()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment