Skip to content

Instantly share code, notes, and snippets.

@uncreative
Created January 6, 2012 03:23
Show Gist options
  • Save uncreative/1568790 to your computer and use it in GitHub Desktop.
Save uncreative/1568790 to your computer and use it in GitHub Desktop.
shows all the colors and styles specified in sublime theme
#!/usr/bin/env python
# encoding: utf-8
"""
%prog [options] path/to/sublime-text-themefile.tmTheme
shows all the colors and styles specified in sublime theme
Created by uncreative on 2012-01-05.
"""
__author__ = "uncreative"
__version__ = "0.1"
__copyright__ = "Copyright (c) 2012 uncreative"
__license__ = "This script is in the public domain, free from copyrights or restrictions"
import plistlib
from subprocess import check_call
import sys
from optparse import OptionParser
def makeColored(d):
name = d['name']
fonts = d['settings']
fg = fonts.get('foreground', defaultForeground)
bg = fonts.get('background', defaultBackground)
style = fonts.get('fontStyle', False)
italic, bold = False, False
if style == 'italic':
italic = True
if style == 'bold':
bold = True
return name, fg, bg, italic, bold
def coloredToHtml(name, fg, bg, italic, bold):
start = '<span style="background:%s"><span style="color:%s">' % (bg, fg)
end = "</span>"
if bold:
start = start + "<b>"
end = "</b>" + end
if italic:
start = start + "<i>"
end = "</i>" + end
return start + name + ", " + fg + end
def readOptions(argv):
"""
parse command line options
"""
parser = OptionParser(usage=globals()['__doc__'], version=__version__,)
options, args = parser.parse_args(argv)
if (len(args) != 1):
print args
parser.error("You must supply a theme file for which to print the colors")
return options, args
def main(argv=None):
global defaultForeground, defaultBackground
# argv = '~/Library/Application Support/Sublime Text 2/Packages/Color Scheme - Default/Solarized (Light) Modded.tmTheme'
argv = sys.argv[1:]
options, args = readOptions(argv)
themefile = args[0]
p = plistlib.readPlist(themefile)
defaultForeground = p['settings'][0]['settings']['foreground']
defaultBackground = p['settings'][0]['settings']['background']
coloreds = [makeColored(colored) for colored in p['settings']
if 'name' in colored and 'settings' in colored \
and ('foreground' in colored['settings'] \
or 'background' in colored['settings'])]
outfname = 'colored.html'
with open(outfname, 'w') as out:
out.write("<html><body>")
for colored in coloreds:
sys.stdout.flush()
out.write(coloredToHtml(*colored) + "<br>")
out.write("</body></html>")
check_call('open %s' % outfname, shell=True)
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment