web_randomizer 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 56a64c25d01f7b0fc4ae959f48ebc77eda2729cc862972df826dfe0992d25255
4
- data.tar.gz: da22d32977061ddaa280a713e169cd61b736cd0099106f9b64af4ce5de1fe605
3
+ metadata.gz: 51754e676ba7c463a714258160488a9d50b63e1aee19c1c388efdc9ef7950219
4
+ data.tar.gz: 8a9dbc5026a58356c7e2fe3338bd72548acf7b82259a6bab8d114eb0e6cb57b6
5
5
  SHA512:
6
- metadata.gz: e836dd9a165b10fb12cdd58a105d3c8fbcc9285f4096d49447a361b24a8d8bcc1e8fb524de43e7f3e055ea10daa14949eae32d61baca2c233ea4bf14b59101c7
7
- data.tar.gz: 077ef13667b9dc2d5e53015f731e3f96517b15a9dfcff025eeac629003e72bebc3f796bd2275b24ee250e6fa2510f3fba9659ca39bd8a08c55a1c51c3dde333f
6
+ metadata.gz: 936fc0f6d1c5fda79425d5e856ce895c94fd6d5feb38263777ded73b176f8caf4a464bba61966c92e8fb985d0d22cb7a0125a73668f78ebca1abebfeaac9c262
7
+ data.tar.gz: cdb0ad0a7d310fad09dae425ed5c039252392c2a0bb57d1886863a135ac6859a12a57a110ebb308eac159674af6dcfffe13398d286a6a0ef6d51785eab7b1ed8
@@ -1,153 +1,253 @@
1
- # frozen_string_literal: true
2
-
3
- require 'yaml'
4
-
5
- module WebRandomizer
6
- class << self
7
- def execute
8
- initialize!
9
-
10
- randomize_div!
11
-
12
- @сss_files_array.each do |el|
13
- puts "\n\nUpdating #{el[:filename]}\n"
14
-
15
- File.open(el[:filename], 'w') do |file|
16
- file.write(color_shift(el[:contents]))
17
- end
18
- end
19
- end
20
-
21
- private
22
-
23
- def initialize!
24
- if File.file?('randomize.yml')
25
- config = YAML.load_file('randomize.yml')
26
-
27
- @html_dir_list = config['html_dir']
28
- @css_dir_list = config['css_dir']
29
-
30
- else
31
- @html_dir_list = %w[_layouts _includes]
32
- @css_dir_list = %w[assets/css _sass]
33
- end
34
-
35
- @сss_files_array = []
36
-
37
- @css_dir_list.each do |dir_item|
38
- Dir.foreach(dir_item) do |css_filename|
39
- next if css_filename == '.' || css_filename == '..'
40
-
41
- @сss_files_array << ({ filename: "#{dir_item}/#{css_filename}",
42
- contents: File.open("#{dir_item}/#{css_filename}").read })
43
- end
44
- end
45
- end
46
-
47
- def randomize_div!
48
- @html_dir_list.each do |dir_item|
49
- Dir.foreach(dir_item) do |filename|
50
- next if filename == '.' || filename == '..'
51
-
52
- puts "Processing #{dir_item}/#{filename}"
53
-
54
- output = File.open("#{dir_item}/#{filename}") { |f| f.read }
55
-
56
- # puts 'DEBUG output:' + output.inspect + "\n\n"
57
-
58
- output.gsub!(/<div>/, "<div class=\"#{rand_string}\">")
59
-
60
- output.scan(/<div.*?class=(.*?)>/).uniq.each do |div|
61
- div.first.slice(/\"(.*)\"/, 1).strip.split(/\s+/).each do |el|
62
- puts "\n\nHTML Searching non-div classes: " + el + "\n\n"
63
-
64
- if found_in_non_div(el)
65
- puts "\n\nFound in non-div tags. Skipping: " + el + "\n\n"
66
- next
67
- end
68
-
69
- new_value = rand_string.strip
70
- @html_dir_list.each do |inner_dir_item|
71
- Dir.foreach(inner_dir_item) do |inner_filename|
72
- next if inner_filename == '.' || inner_filename == '..'
73
-
74
- puts "\n\nHTML Processing inner file: #{inner_dir_item}/" + inner_filename + "\n\n"
75
-
76
- contents = nil
77
-
78
- File.open("#{inner_dir_item}/#{inner_filename}", 'r') { |f| contents = f.read }
79
-
80
- File.open("#{inner_dir_item}/#{inner_filename}", 'w+') do |fw|
81
- fw.write(contents.gsub(el, new_value))
82
- end
83
-
84
- # puts 'DEBUG contents:' + contents
85
- # puts 'DEBUG contents.gsub(el, new_value):' + contents.gsub(el, new_value)
86
- # raise ">>>>>>>>>>> DEBUG "
87
- end
88
- end
89
-
90
- css_array_update!(el, new_value)
91
- end
92
- end
93
- end
94
- end
95
- end
96
-
97
- def found_in_non_div(el)
98
- @html_dir_list.each do |dir_item|
99
- Dir.foreach(dir_item) do |filename|
100
- next if filename == '.' || filename == '..'
101
-
102
- puts "Processing #{dir_item}/#{filename}"
103
-
104
- output = File.open("#{dir_item}/#{filename}") { |f| f.read }
105
-
106
- # puts 'DEBUG output:' + output.inspect + "\n\n"
107
-
108
- # puts 'DEBUG output scan: ' + output.scan(/<(?!div).*?class.*?=.*?#{el}.*?>/).inspect
109
- # puts 'DEBUG output scan: ' + output.scan(/<(?!div).*?class.*?=.*?#{el}.*?>/).empty?.inspect
110
-
111
- return true unless output.scan(/<(?!div).*?class.*?=.*?#{el}.*?>/).empty?
112
-
113
- # raise
114
- end
115
- end
116
-
117
- false
118
- end
119
-
120
- def css_array_update!(old_value, new_value)
121
- puts "\n\nCSS Processing div class from #{old_value} to #{new_value}\n\n"
122
-
123
- @сss_files_array.each do |el|
124
- puts "CSS Processing file: #{el[:filename]}\n"
125
- el[:contents].gsub!(/.\b#{old_value}\b/, '.' + new_value) # .inspect
126
- end
127
- end
128
-
129
- def rand_string
130
- o = [('a'..'z'), ('A'..'Z')].map(&:to_a).flatten
131
- (0...16).map { o[rand(o.length)] }.join
132
- end
133
-
134
- def color_shift(contents)
135
- contents.gsub!(/#[0-9a-fA-F]+/) do |pattern|
136
- # puts "Color processing old_value: #{pattern}\n"
137
-
138
- delta = pattern[1..2].downcase == 'ff' ? -1 : 1
139
-
140
- pattern[1..2] = to_hex(pattern[1..2].hex + delta)
141
-
142
- # puts "Color processing new value: #{pattern}\n"
143
-
144
- pattern
145
- end
146
- contents
147
- end
148
-
149
- def to_hex(int)
150
- int < 16 ? '0' + int.to_s(16) : int.to_s(16)
151
- end
152
- end
153
- end
1
+ # frozen_string_literal: true
2
+
3
+ require 'yaml'
4
+
5
+ module WebRandomizer
6
+ class << self
7
+ def execute
8
+ initialize!
9
+
10
+ randomize_div!
11
+
12
+ @сss_files_array.each do |el|
13
+ puts "\n\nUpdating #{el[:filename]}\n"
14
+
15
+ File.open(el[:filename], 'w') do |file|
16
+ file.write(color_shift(el[:contents]))
17
+ end
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def initialize!
24
+ if File.file?('randomize.yml')
25
+ config = YAML.load_file('randomize.yml')
26
+
27
+ @html_dir_list = config['html_dir']
28
+ @css_dir_list = config['css_dir']
29
+
30
+ else
31
+ @html_dir_list = %w[_layouts _includes]
32
+ @css_dir_list = %w[assets/css _sass]
33
+ end
34
+
35
+ @сss_files_array = []
36
+
37
+ @css_dir_list.each do |dir_item|
38
+ Dir.foreach(dir_item) do |css_filename|
39
+ next if ['.', '..'].include?(css_filename)
40
+
41
+ @сss_files_array << ({ filename: "#{dir_item}/#{css_filename}",
42
+ contents: File.open("#{dir_item}/#{css_filename}").read })
43
+ end
44
+ end
45
+ end
46
+
47
+ def randomize_div!
48
+ @html_dir_list.each do |dir_item|
49
+ Dir.foreach(dir_item) do |filename|
50
+ next if ['.', '..'].include?(filename)
51
+
52
+ puts "Processing #{dir_item}/#{filename}"
53
+
54
+ output = File.open("#{dir_item}/#{filename}", &:read)
55
+
56
+ # puts 'DEBUG output:' + output.inspect + "\n\n"
57
+
58
+ output.gsub!(/<div>/, "<div class=\"#{rand_string}\">")
59
+
60
+ output.scan(/<div.*?class.*?=(.*?)>/).uniq.each do |div|
61
+ # puts "\n\nDEBUG output.scan:" + output.scan(/<div.*?class.*?=(.*?)>/).inspect + "\n\n"
62
+
63
+ # raise
64
+
65
+ # puts 'DEBUG div.first: ' + div.first
66
+ # puts 'DEBUG div.first.slice(/\"(.*)\"/, 1): ' + div.first.tr('"', '').inspect
67
+
68
+ # if compound?(div.first)
69
+ # puts "\n\nSkipping compound div: " + div.first
70
+ # next
71
+ # end
72
+
73
+ div.first.tr('"', '').strip.split(/\s+/).each do |el|
74
+ # if found_in_compound(div.first)
75
+ # puts "\n\nFound in compound class. Skipping: " + div.first + "\n\n"
76
+ # next
77
+ # end
78
+
79
+ if found_in_non_div(el)
80
+ puts "\n\nFound in non-div tags. Skipping: " + el + "\n\n"
81
+
82
+ # raise
83
+
84
+ next
85
+ end
86
+
87
+ new_value = rand_string.strip
88
+
89
+ html_files_update(el, new_value)
90
+
91
+ css_array_update!(el, new_value)
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
97
+
98
+ def html_files_update(old_value, new_value)
99
+ @html_dir_list.each do |inner_dir_item|
100
+ Dir.foreach(inner_dir_item) do |inner_filename|
101
+ next if ['.', '..'].include?(inner_filename)
102
+
103
+ puts "\n\nHTML Processing inner file: #{inner_dir_item}/" + inner_filename + "\n\n"
104
+
105
+ contents = nil
106
+
107
+ File.open("#{inner_dir_item}/#{inner_filename}", 'r') { |f| contents = f.read }
108
+
109
+ puts 'DEBUG old_value:' + old_value
110
+ puts 'DEBUG new_value:' + new_value
111
+
112
+ # raise
113
+
114
+ File.open("#{inner_dir_item}/#{inner_filename}", 'w+') do |fw|
115
+ # TODO: must find old_value with div tag
116
+
117
+ puts "\n\nDEBUG new contents.scan:" + contents.scan(/<div.*?[^-]\b#{old_value}\b[^-].*?>/).inspect + "\n\n"
118
+
119
+ scan_result = contents.scan(/<div.*?[^-]\b#{old_value}\b[^-].*?>/).each do |el|
120
+ replaced_el = el.gsub(/\b#{old_value}\b/, new_value)
121
+
122
+ puts "\n\nDEBUG replaced_el:" + replaced_el.inspect + "\n\n"
123
+
124
+ contents.gsub!(el, replaced_el)
125
+
126
+ puts "\n\nDEBUG contents:" + contents.inspect + "\n\n"
127
+ end
128
+
129
+ # puts "\n\nDEBUG wrong contents.scan:" + contents.scan(/<.?*\b#{old_value}\b.?*>/).inspect + "\n\n"
130
+ # raise
131
+
132
+ fw.write(contents)
133
+
134
+ # puts "\n\nDEBUG contents.gsub OLD:\n\n" + contents.gsub(old_value, new_value).inspect
135
+ # puts "\n\nDEBUG contents.gsub NEW:\n\n" + contents.gsub(/[\"\s]?#{old_value}[\"\s]?/, new_value).inspect
136
+ # puts "\n\nDEBUG contents.scan :\n\n" + contents.scan(/[\"\s]?#{old_value}[\"\s]?/).inspect
137
+ # puts "\n\nDEBUG contents.scan :\n\n" + contents.scan(/\b#{old_value}\b/).inspect
138
+ # puts "\n\nDEBUG contents.scan :\n\n" + contents.scan(old_value).inspect
139
+ end
140
+
141
+ # puts 'DEBUG contents:' + contents
142
+ # puts 'DEBUG contents.gsub(el, new_value):' + contents.gsub(el, new_value)
143
+ # raise ">>>>>>>>>>> DEBUG "
144
+ end
145
+ end
146
+ end
147
+
148
+ def found_in_non_div(el)
149
+ @html_dir_list.each do |dir_item|
150
+ Dir.foreach(dir_item) do |filename|
151
+ next if ['.', '..'].include?(filename)
152
+
153
+ # puts "Processing #{dir_item}/#{filename}"
154
+
155
+ output = File.open("#{dir_item}/#{filename}", &:read)
156
+
157
+ # puts 'DEBUG output:' + output.inspect + "\n\n"
158
+
159
+ # puts 'DEBUG output scan: ' + output.scan(/<(?!div).*?class.*?=.*?#{el}.*?>/).inspect
160
+ # puts 'DEBUG output scan: ' + output.scan(/<(?!div).*?class.*?=.*?#{el}.*?>/).empty?.inspect
161
+
162
+ # return true unless output.scan(/<(?!div).*?class.*?=.*?[^-]\b#{el}\b[^-].*?>/).empty?
163
+ unless output.scan(/<(?!div).*?class.*?=.*?[^-]\b#{el}\b[^-].*?>/).empty?
164
+
165
+ puts 'DEBUG el:' + el
166
+ puts "\n\nDEBUG output scan: " + output.scan(/<(?!div).*?class.*?=.*?[^-]\b#{el}\b[^-].*?>/).inspect + "\n\n"
167
+
168
+ return true
169
+
170
+ # raise
171
+
172
+ end
173
+ # puts 'DEBUG el:' + el
174
+ # puts "\n\nDEBUG output scan: " + output.scan(/<(?!div).*?class.*?=.*?[^-]\b#{el}\b[^-].*?>/).inspect+"\n\n"
175
+
176
+ # raise
177
+ end
178
+ end
179
+
180
+ false
181
+ end
182
+
183
+ def css_array_update!(old_value, new_value)
184
+ puts "\n\nCSS Processing div class from #{old_value} to #{new_value}\n\n"
185
+
186
+ @сss_files_array.each do |el|
187
+ puts "CSS Processing file: #{el[:filename]}\n"
188
+
189
+ el[:contents].gsub!(/\.\b#{old_value}\b/, '.' + new_value) # .inspect
190
+
191
+ puts 'DEBUG old_value:' + old_value
192
+ puts 'DEBUG new_value:' + new_value
193
+ puts "DEBUG OLD el[:contents].gsub: \n\n" + el[:contents].gsub(/.\b#{old_value}\b/, '.' + new_value) + "\n\n"
194
+ puts "DEBUG NEW el[:contents].gsub: \n\n" + el[:contents].gsub(/\.\b#{old_value}\b[^-]/, '.' + new_value) + "\n\n"
195
+
196
+ # raise
197
+
198
+ # el[:contents].gsub!(/\.\b#{old_value}\b[^-]/, '.' + new_value+' ') # .inspect
199
+ end
200
+ end
201
+
202
+ def rand_string
203
+ o = [('a'..'z'), ('A'..'Z')].map(&:to_a).flatten
204
+ (0...16).map { o[rand(o.length)] }.join
205
+ end
206
+
207
+ def color_shift(contents)
208
+ contents.gsub!(/#[0-9a-fA-F]+/) do |pattern|
209
+ # puts "Color processing old_value: #{pattern}\n"
210
+
211
+ delta = pattern[1..2].downcase == 'ff' ? -1 : 1
212
+
213
+ pattern[1..2] = to_hex(pattern[1..2].hex + delta)
214
+
215
+ # puts "Color processing new value: #{pattern}\n"
216
+
217
+ pattern
218
+ end
219
+ contents
220
+ end
221
+
222
+ def to_hex(int)
223
+ int < 16 ? '0' + int.to_s(16) : int.to_s(16)
224
+ end
225
+
226
+ # def compound?(value)
227
+ # value.tr('"', '').strip.split(/\s+/).count>1
228
+ # end
229
+ #
230
+ # def found_in_compound(el)
231
+ # @html_dir_list.each do |dir_item|
232
+ # Dir.foreach(dir_item) do |filename|
233
+ # next if ['.', '..'].include?(filename)
234
+ #
235
+ # puts "Processing #{dir_item}/#{filename}"
236
+ #
237
+ # output = File.open("#{dir_item}/#{filename}", &:read)
238
+ #
239
+ # # puts 'DEBUG output:' + output.inspect + "\n\n"
240
+ #
241
+ # # puts 'DEBUG output scan: ' + output.scan(/<(?!div).*?class.*?=.*?#{el}.*?>/).inspect
242
+ # # puts 'DEBUG output scan: ' + output.scan(/<(?!div).*?class.*?=.*?#{el}.*?>/).empty?.inspect
243
+ #
244
+ # return true unless output.scan(/<(?!div).*?class.*?=.*?#{el}.*?>/).empty?
245
+ #
246
+ # # raise
247
+ # end
248
+ # end
249
+ #
250
+ # false
251
+ # end
252
+ end
253
+ end
@@ -21,31 +21,54 @@ RSpec.describe 'WebRandomizer' do
21
21
  context 'with valid tags' do
22
22
  before do
23
23
  html1_str =
24
- <<-'_layouts'
24
+ <<~'_LAYOUTS'
25
+
26
+ <div class="mh-footer-widget">
27
+
25
28
  <div class="div1">
29
+
30
+ <footer class="footer-widget-title">
31
+
32
+ <div class="footer-widget">
33
+ <div class="dsada footer-widget">
34
+
35
+
36
+ <div class="footer-widget-1">
37
+
38
+ <div class="compound compound2">
39
+
40
+ <div class="footer_class">
41
+
26
42
  <div class="div2">
27
- <footer class="footer_class">
28
- _layouts
43
+ <div id="text-4" class="after_id">
44
+ _LAYOUTS
29
45
 
30
46
  html2_str =
31
- <<-'_includes'
47
+ <<~'_INCLUDES'
48
+ <footer class="footer-widget-title">
49
+
50
+ <div class="compound">
51
+
52
+ <footer class="footer_class">
32
53
  <div class="div2">
33
- <div class="footer_class">
34
- _includes
54
+ _INCLUDES
35
55
 
36
56
  css_str =
37
- <<-'css'
38
- .div1
39
- .div2
40
- cite { color: #9B9b97; }
41
- css
57
+ <<~'CSS'
58
+ .mh-footer-widget.widget_archive li
59
+ .comment-edit-link .div1 { margin-right: 15px; }
60
+ .div1 .comment-edit-link { margin-right: 15px; }
61
+
62
+ .div2
63
+ cite { color: #9B9b97; }
64
+ CSS
42
65
 
43
66
  sass_str =
44
- <<-'sass'
45
- .div2
46
- .div3
47
- a:hover { color: #ff805E; }
48
- sass
67
+ <<~'SASS'
68
+ .div2
69
+ .div3
70
+ a:hover { color: #ff805E; }
71
+ SASS
49
72
 
50
73
  File.open('_layouts/test.html', 'w') { |file| file.write(html1_str) }
51
74
  File.open('_includes/test.html', 'w') { |file| file.write(html2_str) }
@@ -55,13 +78,18 @@ RSpec.describe 'WebRandomizer' do
55
78
  WebRandomizer.execute
56
79
 
57
80
  puts "\n\nSPEC OUTPUT:\n\n"
58
- puts "\n_layouts:\n" + File.open('_layouts/test.html', 'r', &:read)
59
- puts "\n_includes:\n" + File.open('_includes/test.html', 'r', &:read)
60
- puts "\nassets: \n" + File.open('assets/css/test.css', 'r', &:read)
61
- puts "\n_sass:\n" + File.open('_sass/test.sass', 'r', &:read)
81
+ puts "\n_layouts:\n\n#{html1_str}\n" + File.open('_layouts/test.html', 'r', &:read)
82
+ puts "\n_includes:\n\n#{html2_str}\n" + File.open('_includes/test.html', 'r', &:read)
83
+ puts "\nassets: \n\n#{css_str}\n" + File.open('assets/css/test.css', 'r', &:read)
84
+ puts "\n_sass:\n\n#{sass_str}\n" + File.open('_sass/test.sass', 'r', &:read)
62
85
  end
63
86
 
64
- it 'should randomize div tags' do
87
+ it 'shouldn`t match tags with equal substrings' do
88
+ scan_result = File.open('_layouts/test.html', 'r', &:read).scan(/.*?[^-][\b]footer-widget[\b][^-].*?/)
89
+ expect(scan_result.empty?).to be true
90
+ end
91
+
92
+ it 'should change div tags' do
65
93
  scan_result = File.open('_layouts/test.html', 'r', &:read).scan(/.*?(?=div1|div2).*?/)
66
94
  expect(scan_result.empty?).to be true
67
95
  end
@@ -83,5 +111,14 @@ RSpec.describe 'WebRandomizer' do
83
111
  change_result = File.open('assets/css/test.css', 'r', &:read).scan(/.*?#9B9b97.*?/)
84
112
  expect(change_result.empty?).to be true
85
113
  end
114
+
115
+ =begin
116
+ it 'should keep compound classes' do
117
+ scan_result = File.open('_layouts/test.html', 'r', &:read).scan(/.*?compound.*?/)
118
+ expect(scan_result.empty?).to be false
119
+ scan_result = File.open('_includes/test.html', 'r', &:read).scan(/.*?compound2.*?/)
120
+ expect(scan_result.empty?).to be false
121
+ end
122
+ =end
86
123
  end
87
124
  end
@@ -4,8 +4,8 @@ $LOAD_PATH.push File.expand_path('lib', __dir__)
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = 'web_randomizer'
7
- s.version = '0.2.0'
8
- s.date = '2020-10-11'
7
+ s.version = '0.3.0'
8
+ s.date = '2020-10-13'
9
9
  s.summary = 'WebRandomizer - gem for randomizing div classes and color styles for static sites'
10
10
  s.description = 'Gem for randomizing div classes and color styles for static sites.'
11
11
  s.author = 'Timur Sobolev'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: web_randomizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Timur Sobolev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-11 00:00:00.000000000 Z
11
+ date: 2020-10-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: yaml