This repository has been archived by the owner on Aug 17, 2023. It is now read-only.
forked from Umekawa/clsss_nest_remover
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
189 lines (180 loc) · 4.43 KB
/
main.rb
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
# frozen_string_literal: true
# @param parent_names [Array<Array<String>>]
# @return [String]
def class_names(parent_names)
names = []
parent_names.each do |elements|
if names.length.eql?(0)
names = elements
else
f = []
names.each do |name|
elements.each do |element|
f.append(name + element)
end
end
names = f
end
end
names.join(",\n")
end
# @param s [String]
# @return [String]
def remove_extend_class(s)
open_num = 0
parent_names = []
styles = []
style = []
line = ''
i = 0
while i < s.length
case s[i]
when '/'
if s[i + 1].eql?('/')
until s[i].eql?("\n") || s[i].eql?(nil)
# line += s[i] コメントがおかしい場所に出るので出さないようにしてます
i += 1
end
elsif s[i + 1].eql?('*')
until s[i].eql?('*') && s[i + 1].eql?('/')
# line += s[i] コメントがおかしい場所に出るので出さないようにしてます
i += 1
end
else
line += s[i]
i += 1
end
when '{'
line += '{'
i += 1
open_num += 1
when '&'
if s[i + 1].eql?(':')
line += s[i]
i += 1
else
parent_name = ''
names = []
until [' ', '{', '{', nil].include?(s[i])
if s[i].eql?(',')
names.append(parent_name.strip.delete('&'))
parent_name = ''
i += 1
else
parent_name += s[i]
i += 1
end
end
names.append(parent_name.strip.delete('&'))
parent_names.append(names)
style.append('')
open_num -= 1
end
when '}'
line += '}'
i += 1
open_num -= 1
if open_num.eql?(0)
class_name = class_names(parent_names)
parent_names.pop
styles.append(class_name + style.pop + line)
line = ''
open_num += 1 unless parent_names.empty?
end
when ' '
line += ' '
i += 1
when "\n"
if !parent_names.empty? && line.strip != ''
style[-1] += line + "\n"
else
style[0] = style.length.eql?(0) ? "#{line}\n" : "#{style[0]}#{line}\n"
end
line = ''
i += 1
when '@'
while !s[i].eql?(';') && !s[i].nil? && !s[i].eql?('{')
line += s[i]
i += 1
end
if s[i].eql?('{')
until s[i].eql?('}')
line += s[i]
i += 1
end
line += s[i]
i += 1
elsif !s[i].nil?
line += s[i]
i += 1
end
else
if parent_names.length.eql?(0) && !(s[i].eql?(' ') || s[i].eql?('{') || s[i].eql?(nil))
parent_name = ''
names = []
until s[i].eql?(' ') || s[i].eql?('{') || s[i].eql?('/') || s[i].eql?(nil)
if s[i].eql?(',')
names.append(parent_name.strip)
parent_name = ''
i += 1
else
parent_name += s[i]
i += 1
style.append('')
end
end
names.append(parent_name.strip)
parent_names.append(names)
else
line += s[i]
i += 1
end
end
end
styles.append("#{style.join('')}#{line}")
s = styles.join("\n\n")
i = 0
spaces = 0
next_spaces = 0
lines = s.split("\n")
s = lines.map.with_index do |line, i|
next_spaces += line.count('{')
spaces -= line.count('}')
next_spaces -= line.count('}')
line = !line.empty? && spaces > 0 ? ' ' * spaces * 2 + line : line
spaces = next_spaces
if line.count('}') > 0 && !lines[i + 1].nil? && lines[i + 1].count('}').eql?(0) && !lines[i + 1].length.eql?(0)
line += "\n"
end
line
end.join("\n") + "\n"
end
def remove_nest(file_path)
s = ''
File.open(file_path, 'r') do |f|
f.each_line do |line|
line = line.strip.gsub('{', "{\n").gsub('}', "}\n")
s += line[-1].eql?("\n") ? line : "#{line}\n"
end
end
s
end
def main
target_dir = ARGV[0]
if target_dir.eql?(nil)
puts 'Please input attempt folder'
exit
end
puts "target_dir: #{target_dir}"
Dir.glob('**/_*.scss', File::FNM_DOTMATCH, base: target_dir).each do |file|
file_path = File.join(target_dir, file)
next unless file_path
puts file_path
s = remove_nest(file_path)
styles = remove_extend_class(s)
File.open(file_path, 'w') do |f|
f.write(styles)
end
end
end
main if File.basename(__FILE__).eql?(File.basename($PROGRAM_NAME))