module WebRandomizer

Public Class Methods

execute() click to toggle source
# File lib/web_randomizer.rb, line 7
def execute
  initialize!

  randomize_div!

  @сss_files_array.each do |el|
    puts "\n\nUpdating #{el[:filename]}\n"

    File.open(el[:filename], 'w') do |file|
      file.write(color_shift(el[:contents]))
    end
  end
end

Private Class Methods

color_shift(contents) click to toggle source
# File lib/web_randomizer.rb, line 207
def color_shift(contents)
  contents.gsub!(/#[0-9a-fA-F]+/) do |pattern|
    #    puts "Color processing old_value: #{pattern}\n"

    delta = pattern[1..2].downcase == 'ff' ? -1 : 1

    pattern[1..2] = to_hex(pattern[1..2].hex + delta)

    #    puts "Color processing new value: #{pattern}\n"

    pattern
  end
  contents
end
css_array_update!(old_value, new_value) click to toggle source
# File lib/web_randomizer.rb, line 183
def css_array_update!(old_value, new_value)
  puts "\n\nCSS Processing div class from #{old_value} to #{new_value}\n\n"

  @сss_files_array.each do |el|
    puts "CSS Processing file: #{el[:filename]}\n"

    el[:contents].gsub!(/\.\b#{old_value}\b/, '.' + new_value) # .inspect

    puts 'DEBUG old_value:' + old_value
    puts 'DEBUG new_value:' + new_value
    puts "DEBUG OLD el[:contents].gsub: \n\n" + el[:contents].gsub(/.\b#{old_value}\b/, '.' + new_value) + "\n\n"
    puts "DEBUG NEW el[:contents].gsub: \n\n" + el[:contents].gsub(/\.\b#{old_value}\b[^-]/, '.' + new_value) + "\n\n"

    #    raise

    #   el[:contents].gsub!(/\.\b#{old_value}\b[^-]/, '.' + new_value+' ') # .inspect
  end
end
found_in_non_div(el) click to toggle source
# File lib/web_randomizer.rb, line 148
def found_in_non_div(el)
  @html_dir_list.each do |dir_item|
    Dir.foreach(dir_item) do |filename|
      next if ['.', '..'].include?(filename)

      #  puts "Processing #{dir_item}/#{filename}"

      output = File.open("#{dir_item}/#{filename}", &:read)

      #    puts 'DEBUG output:' + output.inspect + "\n\n"

      #          puts 'DEBUG output scan: ' + output.scan(/<(?!div).*?class.*?=.*?#{el}.*?>/).inspect
      #          puts 'DEBUG output scan: ' + output.scan(/<(?!div).*?class.*?=.*?#{el}.*?>/).empty?.inspect

      #          return true unless output.scan(/<(?!div).*?class.*?=.*?[^-]\b#{el}\b[^-].*?>/).empty?
      unless output.scan(/<(?!div).*?class.*?=.*?[^-]\b#{el}\b[^-].*?>/).empty?

        puts 'DEBUG el:' + el
        puts "\n\nDEBUG output scan: " + output.scan(/<(?!div).*?class.*?=.*?[^-]\b#{el}\b[^-].*?>/).inspect + "\n\n"

        return true

        #  raise

      end
      #      puts 'DEBUG el:' + el
      #      puts "\n\nDEBUG output scan: " + output.scan(/<(?!div).*?class.*?=.*?[^-]\b#{el}\b[^-].*?>/).inspect+"\n\n"

      #      raise
    end
  end

  false
end
html_files_update(old_value, new_value) click to toggle source
# File lib/web_randomizer.rb, line 98
def html_files_update(old_value, new_value)
  @html_dir_list.each do |inner_dir_item|
    Dir.foreach(inner_dir_item) do |inner_filename|
      next if ['.', '..'].include?(inner_filename)

      puts "\n\nHTML Processing inner file: #{inner_dir_item}/" + inner_filename + "\n\n"

      contents = nil

      File.open("#{inner_dir_item}/#{inner_filename}", 'r') { |f| contents = f.read }

      puts 'DEBUG old_value:' + old_value
      puts 'DEBUG new_value:' + new_value

      #    raise

      File.open("#{inner_dir_item}/#{inner_filename}", 'w+') do |fw|
        # TODO: must find old_value with div tag

        puts "\n\nDEBUG new contents.scan:" + contents.scan(/<div.*?[^-]\b#{old_value}\b[^-].*?>/).inspect + "\n\n"

        scan_result = contents.scan(/<div.*?[^-]\b#{old_value}\b[^-].*?>/).each do |el|
          replaced_el = el.gsub(/\b#{old_value}\b/, new_value)

          puts "\n\nDEBUG replaced_el:" + replaced_el.inspect + "\n\n"

          contents.gsub!(el, replaced_el)

          puts "\n\nDEBUG contents:" + contents.inspect + "\n\n"
        end

        #       puts "\n\nDEBUG wrong contents.scan:" + contents.scan(/<.?*\b#{old_value}\b.?*>/).inspect + "\n\n"
        #        raise

        fw.write(contents)

        #               puts "\n\nDEBUG contents.gsub OLD:\n\n" + contents.gsub(old_value, new_value).inspect
        #               puts "\n\nDEBUG contents.gsub NEW:\n\n" + contents.gsub(/[\"\s]?#{old_value}[\"\s]?/, new_value).inspect
        #               puts "\n\nDEBUG contents.scan :\n\n" + contents.scan(/[\"\s]?#{old_value}[\"\s]?/).inspect
        #               puts "\n\nDEBUG contents.scan :\n\n" + contents.scan(/\b#{old_value}\b/).inspect
        #               puts "\n\nDEBUG contents.scan :\n\n" + contents.scan(old_value).inspect
      end

      #    puts 'DEBUG contents:' + contents
      #    puts 'DEBUG contents.gsub(el, new_value):' + contents.gsub(el, new_value)
      #    raise ">>>>>>>>>>> DEBUG "
    end
  end
end
initialize!() click to toggle source
# File lib/web_randomizer.rb, line 23
def initialize!
  if File.file?('randomize.yml')
    config = YAML.load_file('randomize.yml')

    @html_dir_list = config['html_dir']
    @css_dir_list = config['css_dir']

  else
    @html_dir_list = %w[_layouts _includes]
    @css_dir_list = %w[assets/css _sass]
  end

  @сss_files_array = []

  @css_dir_list.each do |dir_item|
    Dir.foreach(dir_item) do |css_filename|
      next if ['.', '..'].include?(css_filename)

      @сss_files_array << ({ filename: "#{dir_item}/#{css_filename}",
                             contents: File.open("#{dir_item}/#{css_filename}").read })
    end
  end
end
rand_string() click to toggle source
# File lib/web_randomizer.rb, line 202
def rand_string
  o = [('a'..'z'), ('A'..'Z')].map(&:to_a).flatten
  (0...16).map { o[rand(o.length)] }.join
end
randomize_div!() click to toggle source
# File lib/web_randomizer.rb, line 47
def randomize_div!
  @html_dir_list.each do |dir_item|
    Dir.foreach(dir_item) do |filename|
      next if ['.', '..'].include?(filename)

      puts "Processing #{dir_item}/#{filename}"

      output = File.open("#{dir_item}/#{filename}", &:read)

      #    puts 'DEBUG output:' + output.inspect + "\n\n"

      output.gsub!(/<div>/, "<div class=\"#{rand_string}\">")

      output.scan(/<div.*?class.*?=(.*?)>/).uniq.each do |div|
        #    puts "\n\nDEBUG output.scan:" + output.scan(/<div.*?class.*?=(.*?)>/).inspect + "\n\n"

        #       raise

        #        puts 'DEBUG div.first: ' + div.first
        #        puts 'DEBUG div.first.slice(/\"(.*)\"/, 1):   ' + div.first.tr('"', '').inspect

        #             if compound?(div.first)
        #               puts "\n\nSkipping compound div: " + div.first
        #               next
        #             end

        div.first.tr('"', '').strip.split(/\s+/).each do |el|
          #             if found_in_compound(div.first)
          #               puts "\n\nFound in compound class. Skipping: " + div.first + "\n\n"
          #               next
          #             end

          if found_in_non_div(el)
            puts "\n\nFound in non-div tags. Skipping: " + el + "\n\n"

            #      raise

            next
          end

          new_value = rand_string.strip

          html_files_update(el, new_value)

          css_array_update!(el, new_value)
        end
      end
    end
  end
end
to_hex(int) click to toggle source
# File lib/web_randomizer.rb, line 222
def to_hex(int)
  int < 16 ? '0' + int.to_s(16) : int.to_s(16)
end