This repository has been archived by the owner on Jan 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 84
/
converter
executable file
·166 lines (137 loc) · 3.33 KB
/
converter
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
#!/usr/bin/env ruby
src_file_path, dst_file_path = *ARGV
dst_file_path ||= src_file_path
lines = File.readlines(src_file_path)
indent_size_calc = lambda do |line|
size = 0
line.each_char do |char|
char =~ /\s/ ? size += 1 : break
end
size
end
swap_variable_prefix = lambda do |line, _|
line.gsub(/\@[[:alnum:]]+/) do |var|
if %w(@media @import).include?(var)
var
else
var.sub('@', '$')
end
end
end
turn_percentages_into_percentiles = lambda do |line, _|
line.gsub(/([\d\.]+)%/) do
($1.to_f / 100.0).to_s.sub(/^0/, '')
end
end
convert_mixin_declarations = lambda do |line, _|
line.sub(/^\.([^\(\s]+)\(/) do
"@mixin #{$1}("
end
end
convert_namespace_include_syntax = lambda do |line, _|
line.sub(/#(\S+)\s+>\s+\.([[:alnum:]]+)/) do
"@include #{$1}-#{$2}"
end
end
convert_include_syntax = lambda do |line, _|
line.sub(/^(\s+)\.([^\(\s]+)\(/) do
"#{$1}@include #{$2}("
end
end
escape_box_shadow_arguments = lambda do |line, _|
box_shadow = /@include\s+box-shadow/
without_comma = /@include\s+box-shadow\([^,]+$/
without_dollar = /@include\s+box-shadow\([^\$]+$/
with_dollar_and_comma =
(line !~ without_comma) && (line !~ without_dollar)
if line =~ box_shadow && with_dollar_and_comma
line.sub(/\((.+)\);/) { "(\#{#{$1}});" }
else
line
end
end
swap_spin_adjust_hue = lambda do |line, _|
line.gsub(/(\W)spin\(/) do
"#{$1}adjust-hue("
end
end
resolve_ampersand_dash = lambda do |line, index|
line.sub(/^(\s*)&-(\S+)/) do
indent, suffix, prefix = $1, $2, ''
while index > 0
index -= 1
parent = lines[index]
next if parent.strip == ''
parent_indent_size = indent_size_calc.call(parent)
if parent_indent_size < indent.size
prefix = parent[/^\s*(\S+)/, 1].to_s.strip
break
end
end
if prefix == ''
raise "Could not find parent for &- substitution on line #{index}\n" +
line
else
"#{indent}#{prefix}-#{suffix}"
end
end
end
clear_out_escapes = lambda do |line, _|
line.gsub(/~"([^"]+)"/) { $1 }
end
rewrite_image_paths = lambda do |line, _|
if line =~ /^\s*\$/ && line.include?(':') && line.include?('.png"')
line.sub(/"([^"]+)"/) do
"image-path(\"#{File.basename($1)}\")"
end
else
line
end
end
remove_icon_font_path = lambda do |line, _|
if line =~ /^\s*\$icon-font-path/
""
else
line
end
end
mark_variables_default = lambda do |line, _|
if line =~ /^\s*\$/ && line.include?(':')
line.sub(';', ' !default;')
else
line
end
end
line_processors = [
swap_variable_prefix,
# This converter doesn't seem to be actually needed
# turn_percentages_into_percentiles,
convert_mixin_declarations,
convert_namespace_include_syntax,
convert_include_syntax,
escape_box_shadow_arguments,
swap_spin_adjust_hue,
resolve_ampersand_dash,
clear_out_escapes
]
if File.basename(src_file_path) =~ /variables/
line_processors += [
rewrite_image_paths,
remove_icon_font_path,
mark_variables_default
]
end
comment_line = /^\s*\/\//
processed_lines = lines.map.with_index do |line, index|
if line =~ comment_line
line
else
line_processors.each do |processor|
line = processor.call(line, index)
end
line
end
end
File.open(dst_file_path, 'w') do |file|
file.write(processed_lines.join)
end