Home Download Buy Blog Forum Support

Color Schemes as JSON

Color Schemes as JSON

Postby socrattes on Fri Mar 30, 2012 9:26 pm

Just a simple question.

Is it possible to format Sublime ColorSchemes as JSON?
If so, could you tell me how is the structure?

Thanks!
socrattes
 
Posts: 13
Joined: Fri Mar 30, 2012 9:25 pm

Re: Color Schemes as JSON

Postby facelessuser on Fri Mar 30, 2012 10:31 pm

You could easily create a good plugin to do this. Here is a thrown together sloppy plugin you could build off of.

Code: Select all
import sublime
import sublime_plugin
import json
import StringIO
from plistlib import readPlist, writePlistToString


class PlistToJsonCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        f = StringIO.StringIO(
            self.view.substr(
                sublime.Region(0, self.view.size())
            ).encode('utf8')
        )
        self.view.replace(
            edit,
            sublime.Region(0, self.view.size()),
            json.dumps(
                readPlist(f), indent=4, separators=(',', ': ')
            )
        )


class JsonToPlistCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        j = json.loads(
            self.view.substr(
                sublime.Region(0, self.view.size())
            )
        )
        self.view.replace(
            edit,
            sublime.Region(0, self.view.size()),
            writePlistToString(j)
        )


Load the color scheme and then execute the PlistToJson command, work with the data as a json. When you get done, run the JsonToPlist command and then save. I think AAAPackageDev has something much better than this, but I have never used it; I usually work with them as plists. Anyways, the code I posted is not meant to be a completed thing, though it works.

I would try out AAAPackageDev, or if that doesn't do what you want, you can build on this concept. You could have it do something crazy like detect if a file is a plist on file open and then convert it to json, and when you save it, convert it back to plist. I don't know, I am just throwing out stuff, but what you are asking is easily doable.

Edit: Updated code to work completely from the content in the view; no need to open the file. Assumes plist is always UTF8.
facelessuser
 
Posts: 1576
Joined: Tue Apr 05, 2011 7:38 pm

Re: Color Schemes as JSON

Postby socrattes on Fri Mar 30, 2012 11:45 pm

Wow facelesuser your awesome <3

I'm just a designer - coder converted to Sublime but I'll see what I can do.
Thanks a lot!
socrattes
 
Posts: 13
Joined: Fri Mar 30, 2012 9:25 pm

Re: Color Schemes as JSON

Postby facelessuser on Fri Mar 30, 2012 11:55 pm

One more thing. Added change file syntax accordingly on conversion. If you are using a non-standard syntax, you could make it look where you want via a sublime-settings file.

Code: Select all
import sublime
import sublime_plugin
import json
import StringIO
from plistlib import readPlist, writePlistToString


JSON_SYNTAX = "Packages/Javascript/JSON.tmLanguage"
PLIST_SYNTAX = "Packages/XML/XML.tmLanguage"


class PlistToJsonCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        f = StringIO.StringIO(
            self.view.substr(
                sublime.Region(0, self.view.size())
            ).encode('utf8')
        )
        self.view.replace(
            edit,
            sublime.Region(0, self.view.size()),
            json.dumps(
                readPlist(f), indent=4, separators=(',', ': ')
            )
        )
        self.view.set_syntax_file(JSON_SYNTAX)


class JsonToPlistCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        j = json.loads(
            self.view.substr(
                sublime.Region(0, self.view.size())
            )
        )
        self.view.replace(
            edit,
            sublime.Region(0, self.view.size()),
            writePlistToString(j)
        )
        self.view.set_syntax_file(PLIST_SYNTAX)


Okay, that is it.
facelessuser
 
Posts: 1576
Joined: Tue Apr 05, 2011 7:38 pm


Return to Technical Support

Who is online

Users browsing this forum: Exabot [Bot], Renegade221 and 27 guests