Created
September 8, 2012 21:36
-
-
Save whats-up/3679948 to your computer and use it in GitHub Desktop.
cotEditorでJSON整形するためのコード。
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
#!/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