forked from ihdavids/orgextended
-
Notifications
You must be signed in to change notification settings - Fork 0
/
orgxmlthemeparser.py
76 lines (67 loc) · 2.28 KB
/
orgxmlthemeparser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import os
from xml.etree import ElementTree
from collections import OrderedDict
import sublime
STYLE_TEMPLATE = """
<dict>
<key>name</key>
<string>{name}</string>
<key>scope</key>
<string>{scope}</string>
<key>settings</key>
<dict>
{properties}
</dict>
</dict>
"""
PROPERTY_TEMPLATE = """
<key>{key}</key>
<string>{value}</string>
"""
class XMLThemeParser:
def __init__(self, themeText):
self.cs = {}
self.plist = ElementTree.XML(themeText)
styles = self.plist.find("./dict/array")
assert styles
self.styles = styles
globalsVals = self.styles[0]
gv = {}
k = None
for p in globalsVals:
if p.tag == "dict":
for v in p:
if v.tag == "key":
k = v.text
if v.tag == "string":
gv[k] = v.text
self.cs['globals'] = gv
rules = []
for d in self.styles[1:]:
r = {}
for p in d:
if p.tag == "key":
k = p.text
if p.tag == "string":
if k == "name" or k == 'scope':
r[k] = p.text
if p.tag == "dict" and k == "settings":
dk = None
for c in p:
if c.tag == "key":
k = c.text
if c.tag == "string":
if k == 'fontStyle':
k = "font_style"
r[k] = c.text
rules.append(r)
self.cs['rules'] = rules
# def _add_scoped_style(self, name, scope, **kwargs):
# properties = "".join(PROPERTY_TEMPLATE.format(key=k, value=v) for k, v in kwargs.items())
# new_style = STYLE_TEMPLATE.format(name=name, scope=scope, properties=properties)
# self.styles.append(ElementTree.XML(new_style))
# def write_new_theme(self, name):
# full_path = os.path.join(sublime.packages_path(), self.get_theme_path(name))
# with file.safe_open(full_path, "wb", buffering=0) as out_f:
# out_f.write(STYLES_HEADER.encode("utf-8"))
# out_f.write(ElementTree.tostring(self.plist, encoding="utf-8"))