Skip to content

Instantly share code, notes, and snippets.

Created June 4, 2013 15:51
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 anonymous/5707028 to your computer and use it in GitHub Desktop.
Save anonymous/5707028 to your computer and use it in GitHub Desktop.
Quick mockup python script which converts styles from studiostyl.es to xml-files that QtCreator can use for syntax highlighting. Put a bunch of .vssetting files in the input folder (./VisualStudioStyles), run the script and copy the resulting XML files from the output folder (QtCreatorStyle) to the style folder of your Qt Creator installation (e…
import glob
import os.path
import xml.etree.ElementTree as ET
from xml.etree import ElementTree
from xml.dom import minidom
VS_INPUT_FOLDER = 'VisualStudioStyles'
QT_OUTPUT_FOLDER = 'QtCreatorStyles'
QT_to_VS = {
'AddedLine' : '' ,
'Comment' : 'Comment' ,
'CurrentLine' : 'Selected Text' ,
'CurrentLineNumber' : '' ,
'DiffFile' : '' ,
'DiffLocation' : '' ,
'DisabledCode' : '' ,
'Doxygen.Comment' : 'XML Doc Comment' ,
'Doxygen.Tag' : 'XML Doc Tag' ,
'Field' : 'Identifier' ,
'Function' : 'Identifier' ,
'Keyword' : 'Keyword' ,
'Label' : 'Identifier' ,
'LineNumber' : 'Line Numbers' ,
'Link' : '' ,
'Local' : 'Identifier' ,
'Number' : 'Number' ,
'Occurrences' : '' ,
'Occurrences.Rename' : '' ,
'Occurrences.Unused' : '' ,
'Operator' : 'Operator' ,
'Parentheses' : 'Operator' ,
'Preprocessor' : 'Preprocessor Keyword',
'RemovedLine' : '' ,
'SearchResult' : '' ,
'SearchScope' : '' ,
'Selection' : 'Selected Text' ,
'Static' : 'Identifier' ,
'String' : 'String' ,
'Text' : 'Plain Text' ,
'Type' : 'User Types(Value types)',
'VirtualMethod' : 'Identifier' ,
'VisualWhitespace' : '' ,
'QmlTypeId' : 'User Types' ,
'Binding' : '' ,
'QmlLocalId' : 'XML Text' ,
'QmlRootObjectProperty' : 'XML Attribute' ,
'QmlScopeObjectProperty' : 'XML Attribute Value',
'QmlStateName' : 'XML Name' ,
'QmlExternalId' : 'XML Name' ,
'QmlExternalObjectProperty' : 'XML Name' ,
'JsScopeVar' : '' ,
'JsImportVar' : '' ,
'JsGlobalVar' : '' ,
}
# From http://stackoverflow.com/questions/14440375/how-to-add-an-element-to-xml-file-by-using-elementtree
def prettify(elem):
"""Return a pretty-printed XML string for the Element.
"""
rough_string = ElementTree.tostring(elem, 'utf-8')
reparsed = minidom.parseString(rough_string)
return reparsed.toprettyxml(indent=" ")
for vsFile in glob.glob (os.path.join (VS_INPUT_FOLDER, "*.vssettings")):
# Parse one style file...
styleInfo = {}
name = os.path.basename(vsFile)
vsRoot = ET.parse (vsFile).getroot()
for E in vsRoot.findall(".//Item"):
styleInfo[ E.attrib['Name'] ] = E.attrib
qtRoot = ET.Element ('style-scheme', { 'version': "1.0", 'name': 'vs-' + name})
for q,v in QT_to_VS.items():
if not v:
v = 'Plain Text'
def findColor (v, color):
if styleInfo[v][color] == '0x02000000':
c = styleInfo['Plain Text'][color];
if c == '0x02000000':
if color == 'Foreground': c = '000000';
elif color == 'Background': c = 'ffffff';
else:
c = styleInfo[v][color]
c = c[4:]
r = c[4:6]
g = c[2:4]
b = c[0:2]
return '#' + r + g + b
attributes = { 'name': q }
if 'Foreground' in styleInfo[v]: attributes['foreground'] = findColor (v, 'Foreground')
if 'Background' in styleInfo[v]: attributes['background'] = findColor (v, 'Background')
if 'BoldFont' in styleInfo[v] and styleInfo[v]['BoldFont' ] == 'Yes': attributes['bold' ] = 'true'
ET.SubElement (qtRoot, 'style', attributes)
qtFile = open(os.path.join (QT_OUTPUT_FOLDER, 'vs-' + name.replace('.vssettings', '.xml')), 'w')
qtFile.write (prettify(qtRoot))
qtFile.close()
@centipede
Copy link

Btw, it's unfinished! Default color values aren't treated properly at all.
But it is close enough that the result makes visual sense.
-Rene

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment