forked from chriskempson/base16-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase16
More file actions
executable file
·176 lines (138 loc) · 3.59 KB
/
base16
File metadata and controls
executable file
·176 lines (138 loc) · 3.59 KB
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
#!/usr/bin/env ruby
# Base16 Builder (https://github.com/chriskempson/base16-builder)
require "securerandom"
require "yaml"
require "erb"
class Theme
attr_accessor :template
def initialize
@scheme_dir = "schemes"
@template_dir = "templates"
@output_dir = "output"
end
def build(scheme_file)
if scheme_file then
build_single_scheme(scheme_file)
else
build_all_schemes
end
end
def build_all_schemes
scheme_files = read_scheme_dir
scheme_files.each do |scheme_file|
build_single_scheme(scheme_file)
end
end
def build_single_scheme(scheme_file)
puts scheme_file
scheme_data = read_scheme_file(scheme_file)
parse_template_files(scheme_data)
end
def read_scheme_file(scheme_file)
scheme_file = @scheme_dir + "/#{scheme_file}"
begin
YAML.load_file(scheme_file)
rescue StandardError
abort(read_error_message(scheme_file))
end
end
def read_scheme_dir
Dir.chdir(@scheme_dir) do
begin
Dir.glob("*.yml")
rescue StandardError
abort(read_error_message(scheme_dir))
end
end
end
def read_template_dir
Dir.chdir(@template_dir) do
begin
Dir.glob("**/*.erb")
rescue StandardError
abort(read_error_message(@template_dir))
end
end
end
def read_template_file(template_file)
template_file = @template_dir + "/#{template_file}"
begin
File.open(template_file).read
rescue StandardError
abort(read_error_message(template_file))
end
end
def parse_template_files(scheme_data)
# Define ERB vars
scheme = scheme_data["scheme"]
author = scheme_data["author"]
slug = slug(scheme_data["scheme"])
uuid = SecureRandom.uuid
base = {}
[
"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "0A", "0B",
"0C", "0D", "0E", "0F"
].each do |key|
hex = scheme_data["base" + key];
base[key] = {
"hex" => hex,
"dhex" => to_dhex(hex),
"rgb" => to_rgb(hex),
"srgb" => to_srgb(hex)
}
end
read_template_dir.each do |template_file|
puts " - " + template_file
template_contents = read_template_file(template_file)
parsed = ERB.new(template_contents)
dir_name = File.dirname(template_file);
file_name = File.basename(template_file, ".erb");
scheme_name = slug(scheme)
make_dir("#{@output_dir}/#{dir_name}")
#Emacs themes require -theme to appended to end of filename
if dir_name=="emacs"
scheme_name << "-theme"
end
output_file = File.open(
"#{@output_dir}/#{dir_name}/base16-#{scheme_name}.#{file_name}",
"w"
)
output_file.write parsed.result(binding)
end
end
def make_dir(name)
if FileTest::directory?(name)
return
end
Dir::mkdir(name)
end
def slug(string)
string.downcase.strip.gsub(' ', '.').gsub(/[^\w-]/, '')
end
def to_dhex(hex)
hex.split(//).map {|char| char + char}.join
end
def to_rgb(hex)
hex.scan(/../).map {|color| color.to_i(16)}
end
def to_srgb(hex)
hex.scan(/../).map {|color| color.to_i(16).to_f / 255 }
end
def read_error_message(file)
"Error reading #{file}"
end
end
help_message = <<-EOF
Base16 Builder v0.1
https://github.com/chriskempson/base16-builder
usage: base16 build all schemes
or: base16 scheme.yml build specified scheme
EOF
case ARGV[0]
when "--help"
puts help_message
when "-h"
puts help_message
else
Theme.new.build(ARGV[0])
end