web_randomizer 0.1.0 → 0.2.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: 87005117f31f22bf286e0e4a7cbbe73722dba4f281532cb95e64f3cad57d2e60
4
- data.tar.gz: 85190000e6ba3d5297cb5dd918cf5184f45348dc32d34a77239efe7041057596
3
+ metadata.gz: 56a64c25d01f7b0fc4ae959f48ebc77eda2729cc862972df826dfe0992d25255
4
+ data.tar.gz: da22d32977061ddaa280a713e169cd61b736cd0099106f9b64af4ce5de1fe605
5
5
  SHA512:
6
- metadata.gz: f8b279533300cc4def8c412ef21952115258396c9a533b61c3b2da133b2a32d16c9ef10268025292d6c4b84a8564218d063e352374fd8f3c08c7790c4b35d0ba
7
- data.tar.gz: 2f7b3e44dd1151b446045d27bfc7985bc68f119fd8e2d16417e5694fa13bc259656da896ce8390b44fae44b8791ce73fb23c0609037dc55740623b1da75e92ed
6
+ metadata.gz: e836dd9a165b10fb12cdd58a105d3c8fbcc9285f4096d49447a361b24a8d8bcc1e8fb524de43e7f3e055ea10daa14949eae32d61baca2c233ea4bf14b59101c7
7
+ data.tar.gz: 077ef13667b9dc2d5e53015f731e3f96517b15a9dfcff025eeac629003e72bebc3f796bd2275b24ee250e6fa2510f3fba9659ca39bd8a08c55a1c51c3dde333f
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ .idea/
2
+ *.gem
data/README.md CHANGED
@@ -35,4 +35,3 @@ Usage example:
35
35
 
36
36
  ## License
37
37
  MIT License.
38
- `
@@ -1,123 +1,153 @@
1
- require 'yaml'
2
-
3
- module WebRandomizer
4
- class << self
5
- def execute
6
- initialize!
7
-
8
- randomize_div!
9
-
10
- @сss_files_array.each do |el|
11
- puts "\n\nUpdating #{el[:filename]}\n"
12
-
13
- File.open(el[:filename], 'w') do |file|
14
- file.write(color_shift(el[:contents]))
15
- end
16
- end
17
- end
18
-
19
- private
20
-
21
- def initialize!
22
- if File.file?('randomize.yml')
23
- config = YAML.load_file('randomize.yml')
24
-
25
- @html_dir_list = config['html_dir']
26
- @css_dir_list = config['css_dir']
27
-
28
- else
29
- @html_dir_list = %w[_layouts _includes]
30
- @css_dir_list = %w[assets/css _sass]
31
- end
32
-
33
- @сss_files_array = []
34
-
35
- @css_dir_list.each do |dir_item|
36
- Dir.foreach(dir_item) do |css_filename|
37
- next if css_filename == '.' || css_filename == '..'
38
-
39
- @сss_files_array << ({ filename: "#{dir_item}/#{css_filename}",
40
- contents: File.open("#{dir_item}/#{css_filename}").read })
41
- end
42
- end
43
- end
44
-
45
- def randomize_div!
46
- @html_dir_list.each do |dir_item|
47
- Dir.foreach(dir_item) do |filename|
48
- next if filename == '.' || filename == '..'
49
-
50
- puts "Processing #{dir_item}/#{filename}"
51
-
52
- output = File.open("#{dir_item}/#{filename}") { |f| f.read }
53
-
54
- # puts 'DEBUG output:' + output.inspect + "\n\n"
55
-
56
- output.gsub!(/<div>/, "<div class=\"#{rand_string}\">")
57
-
58
- output.scan(/<div.*?class=(.*?)>/).uniq.each do |div|
59
- div.first.slice(/\"(.*)\"/, 1).strip.split(/\s+/).each do |el|
60
- puts "\n\nHTML Processing div: " + el + "\n\n"
61
-
62
- new_value = rand_string.strip
63
- @html_dir_list.each do |inner_dir_item|
64
- Dir.foreach(inner_dir_item) do |inner_filename|
65
- next if inner_filename == '.' || inner_filename == '..'
66
-
67
- puts "\n\nHTML Processing inner file: #{inner_dir_item}/" + inner_filename + "\n\n"
68
-
69
- contents = nil
70
-
71
- File.open("#{inner_dir_item}/#{inner_filename}", 'r') { |f| contents = f.read }
72
-
73
- File.open("#{inner_dir_item}/#{inner_filename}", 'w+') do |fw|
74
- fw.write(contents.gsub(el, new_value))
75
- end
76
-
77
- # puts 'DEBUG contents:' + contents
78
- # puts 'DEBUG contents.gsub(el, new_value):' + contents.gsub(el, new_value)
79
- # raise ">>>>>>>>>>> DEBUG "
80
- end
81
- end
82
-
83
- css_array_update!(el, new_value)
84
- end
85
- end
86
- end
87
- end
88
- end
89
-
90
- def css_array_update!(old_value, new_value)
91
- puts "\n\nCSS Processing div class from #{old_value} to #{new_value}\n\n"
92
-
93
- @сss_files_array.each do |el|
94
- puts "CSS Processing file: #{el[:filename]}\n"
95
- el[:contents].gsub!(/.\b#{old_value}\b/, '.' + new_value) # .inspect
96
- end
97
- end
98
-
99
- def rand_string
100
- o = [('a'..'z'), ('A'..'Z')].map(&:to_a).flatten
101
- (0...16).map { o[rand(o.length)] }.join
102
- end
103
-
104
- def color_shift(contents)
105
- contents.gsub!(/#[0-9a-fA-F]+/) do |pattern|
106
- # puts "Color processing old_value: #{pattern}\n"
107
-
108
- delta = pattern[1..2].downcase == 'ff' ? -1 : 1
109
-
110
- pattern[1..2] = to_hex(pattern[1..2].hex + delta)
111
-
112
- # puts "Color processing new value: #{pattern}\n"
113
-
114
- pattern
115
- end
116
- contents
117
- end
118
-
119
- def to_hex(int)
120
- int < 16 ? '0' + int.to_s(16) : int.to_s(16)
121
- end
122
- end
123
- 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 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
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'web_randomizer'
4
+ require 'fileutils'
5
+
6
+ RSpec.describe 'WebRandomizer' do
7
+ before do
8
+ FileUtils.mkdir_p '_layouts'
9
+ FileUtils.mkdir_p '_includes'
10
+ FileUtils.mkdir_p 'assets/css'
11
+ FileUtils.mkdir_p '_sass'
12
+ end
13
+
14
+ after do
15
+ FileUtils.rm_rf('_layouts')
16
+ FileUtils.rm_rf('_includes')
17
+ FileUtils.rm_rf('assets')
18
+ FileUtils.rm_rf('_sass')
19
+ end
20
+
21
+ context 'with valid tags' do
22
+ before do
23
+ html1_str =
24
+ <<-'_layouts'
25
+ <div class="div1">
26
+ <div class="div2">
27
+ <footer class="footer_class">
28
+ _layouts
29
+
30
+ html2_str =
31
+ <<-'_includes'
32
+ <div class="div2">
33
+ <div class="footer_class">
34
+ _includes
35
+
36
+ css_str =
37
+ <<-'css'
38
+ .div1
39
+ .div2
40
+ cite { color: #9B9b97; }
41
+ css
42
+
43
+ sass_str =
44
+ <<-'sass'
45
+ .div2
46
+ .div3
47
+ a:hover { color: #ff805E; }
48
+ sass
49
+
50
+ File.open('_layouts/test.html', 'w') { |file| file.write(html1_str) }
51
+ File.open('_includes/test.html', 'w') { |file| file.write(html2_str) }
52
+ File.open('assets/css/test.css', 'w') { |file| file.write(css_str) }
53
+ File.open('_sass/test.sass', 'w') { |file| file.write(sass_str) }
54
+
55
+ WebRandomizer.execute
56
+
57
+ 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)
62
+ end
63
+
64
+ it 'should randomize div tags' do
65
+ scan_result = File.open('_layouts/test.html', 'r', &:read).scan(/.*?(?=div1|div2).*?/)
66
+ expect(scan_result.empty?).to be true
67
+ end
68
+
69
+ it 'should keep non-div tags' do
70
+ scan_result = File.open('_layouts/test.html', 'r', &:read).scan(/.*?footer_class.*?/)
71
+ expect(scan_result.empty?).to be false
72
+ end
73
+
74
+ it 'should process css classes' do
75
+ change_result = File.open('assets/css/test.css', 'r', &:read).scan(/.*?(?=div1|div2).*?/)
76
+ keep_result = File.open('_sass/test.sass', 'r', &:read).scan(/.*?(?=div3|div4).*?/)
77
+
78
+ expect(change_result.empty?).to be true
79
+ expect(keep_result.empty?).to be false
80
+ end
81
+
82
+ it 'should randomize colors' do
83
+ change_result = File.open('assets/css/test.css', 'r', &:read).scan(/.*?#9B9b97.*?/)
84
+ expect(change_result.empty?).to be true
85
+ end
86
+ end
87
+ 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.1.0'
8
- s.date = '2020-10-10'
7
+ s.version = '0.2.0'
8
+ s.date = '2020-10-11'
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.1.0
4
+ version: 0.2.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-10 00:00:00.000000000 Z
11
+ date: 2020-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: yaml
@@ -30,9 +30,11 @@ executables: []
30
30
  extensions: []
31
31
  extra_rdoc_files: []
32
32
  files:
33
+ - ".gitignore"
33
34
  - Gemfile
34
35
  - README.md
35
36
  - lib/web_randomizer.rb
37
+ - spec/web_randomizer_spec.rb
36
38
  - web_randomizer.gemspec
37
39
  homepage: https://github.com/sobolevtimur/web-randomizer
38
40
  licenses: