This repository has been archived by the owner on Aug 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathez.py
219 lines (170 loc) · 6.06 KB
/
ez.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env python3
# Author: Murtaza Udaipurwala <https://github.com/Murtaza-Udaipurwala>
import yaml
import os
import argparse
import sys
def parse_arguments():
parser = argparse.ArgumentParser()
parser.add_argument('filename', type=argparse.FileType('r'),
help='pass a filename')
args = parser.parse_args()
return args
def write(fname, content):
with open(os.path.join(base_path, fname), 'w') as fhand:
fhand.write(content)
def colors(name, author, keys):
loaded = f"package.loaded['{name}'] = nil\n"
for key in keys:
loaded += f"package.loaded['{name}.{key}'] = nil\n"
code = f'''" Author: {author}\n
lua << EOF
{loaded}
require("{name}")
EOF
'''
with open(os.path.join(os.path.join(name, 'colors', f'{name}.vim')), 'w') as fhand:
fhand.write(code)
def config(name):
code = '''local config
vim = vim or { g = {}, o = {} }
local function opt(key, default)
if vim.g[key] == nil then
return default
end
if vim.g[key] == 0 then
return false
end
return vim.g[key]
end
config = {
transparent_background = opt("transparent_background", false),
italic_comments = opt("italic_keywords", true) and "italic" or "NONE",
italic_keywords = opt("italic_keywords", true) and "italic" or "NONE",
italic_functions = opt("italic_function", false) and "italic" or "NONE",
italic_variables = opt("italic_variables", true) and "italic" or "NONE",
}
return config'''
write('config.lua', code)
def util():
code = '''local M = {}
local function highlight(group, properties)
local bg = properties.bg == nil and "" or "guibg=" .. properties.bg
local fg = properties.fg == nil and "" or "guifg=" .. properties.fg
local style = properties.style == nil and "" or "gui=" .. properties.style
local cmd = table.concat({
"highlight", group, bg, fg, style
}, " ")
vim.api.nvim_command(cmd)
end
function M.initialise(skeleton)
for group, properties in pairs(skeleton) do
highlight(group, properties)
end
end
return M'''
write('util.lua', code)
def init(name, background, keys):
requirements = ""
for key in keys:
requirements += f'local {key} = require("{name}.{key}")\n'
code = f'''vim.api.nvim_command("hi clear")
if vim.fn.exists("syntax_on") then
vim.api.nvim_command("syntax reset")
end
vim.o.background = "{background}"
vim.o.termguicolors = true
vim.g.colors_name = "{name}"
local util = require("{name}.util")
Config = require("{name}.config")
C = require("{name}.palette")
{requirements}
local skeletons = {{
{", ".join(keys)}
}}
for _, skeleton in ipairs(skeletons) do
util.initialise(skeleton)
end'''
write('init.lua', code)
def gen_palette(palette):
code = 'local colors = {\n'
for key, value in palette.items():
code += f'\t{key} = "{value}",\n'
code += "}\n\nreturn colors"
write('palette.lua', code)
def gen_skeleton(syntax, name, colorscheme_name):
code = f'local {name} = {{\n'
for key, value in syntax.items():
group = key
props = value.split(' ')
prop_keys = ('fg', 'bg', 'style')
if props[0] != '':
skeleton = "\t" + group + " = {"
for i in range(len(props)):
if props[i] not in ['-', '.']:
if i == 2:
style = ','.join([styles[char] for char in props[i]])
skeleton += f'{prop_keys[i]} = "{style}", '
elif i == 1:
if group in ["Normal", "NormalNC", "MsgArea", "TelescopeBorder"]:
skeleton += f'{prop_keys[i]} = Config.transparent_background and "NONE" or C.{props[i]}, '
elif group == "Keyword":
skeleton += f'{prop_keys[i]} = Config.italic_keywords, '
elif group == "Comment":
skeleton += f'{prop_keys[i]} = Config.italic_comments, '
elif group == "Identifier":
skeleton += f'{prop_keys[i]} = Config.italic_variables, '
elif group == "Function":
skeleton += f'{prop_keys[i]} = Config.italic_functions, '
elif props[i][0] == '#':
skeleton += f'{prop_keys[i]} = "{props[i]}", '
else:
skeleton += f"{prop_keys[i]} = C.{props[i]}, "
else:
if props[i][0] == '#':
skeleton += f'{prop_keys[i]} = "{props[i]}", '
else:
skeleton += f"{prop_keys[i]} = C.{props[i]}, "
code += '\t' + skeleton + '},\n'
code += "}\n\nreturn " + name
write(f'{name}.lua', code)
if __name__ == "__main__":
args = parse_arguments()
with args.filename as fhand:
if fhand.name.endswith('.yml') or fhand.name.endswith('.yaml'):
obj = yaml.safe_load(fhand)
else:
print("File must have an extension .yml or .yaml")
sys.exit()
keys = [key for key in obj]
if 'palette' not in keys:
print("palette key not found in yaml file")
sys.exit()
elif 'information' not in keys:
print("information key not found in yaml file")
sys.exit()
styles = {
'i': 'italic',
'b': 'bold',
'u': 'underline',
'r': 'reverse'
}
colorscheme = obj['information']['name']
author = obj['information']['author']
background = obj['information']['background']
base_path = os.path.join(colorscheme, 'lua', colorscheme)
try:
os.makedirs(base_path)
path = os.path.join(colorscheme, "colors")
os.makedirs(path)
except:
pass
keys.remove('palette')
keys.remove('information')
gen_palette(obj['palette'])
init(colorscheme, background, keys)
util()
colors(colorscheme, author, keys)
config(colorscheme)
for key in keys:
gen_skeleton(obj[key], key, colorscheme)