class Komplement::Base

Constants

EXIT_FAIL
EXIT_SUCCESS

Attributes

dirs[RW]
files_of_interest[R]
filetypes[RW]
ignored_elements[RW]

Public Class Methods

new() click to toggle source
# File lib/komplement/base.rb, line 7
def initialize
  @ignored_elements = @filetypes = @dirs = []
end

Public Instance Methods

find_offenses() click to toggle source
# File lib/komplement/base.rb, line 42
def find_offenses
  [@ignored_elements, @filetypes, @dirs].map { |e| e.uniq! }

  paths = make_paths
  @files_of_interest = make_files(paths)

  unknown = Hash.new { |h, k| h[k] = [] }

  @files_of_interest.each do |file|
    contents = File.read(file)
    Nokogiri::HTML(contents).traverse do |node|
      unless @ignored_elements.include? nm = node.name
        unknown[file].push nm
      end
    end
  end
  unknown
end
in_dirs(dirs_a) click to toggle source

all the paths we should look into

# File lib/komplement/base.rb, line 24
def in_dirs(dirs_a)
  @dirs += dirs_a
  self
end
in_filetypes(filetypes_a) click to toggle source

filetypes to look into for html elements

# File lib/komplement/base.rb, line 18
def in_filetypes(filetypes_a)
  @filetypes = filetypes_a.map { |e| "*.#{e}" }
  self
end
run() click to toggle source
# File lib/komplement/base.rb, line 29
def run
  process_output(find_offenses)
end
run_and_raise() click to toggle source
# File lib/komplement/base.rb, line 33
def run_and_raise
  ret = run
  if ret != 0
    raise Komplement::Error, "there were problems with your html"
  else
    ret
  end
end
with_ignored(ignored_a) click to toggle source

which html elements to ignore

# File lib/komplement/base.rb, line 12
def with_ignored(ignored_a)
  @ignored_elements += ignored_a
  self
end

Private Instance Methods

make_files(paths_a) click to toggle source
# File lib/komplement/base.rb, line 80
def make_files(paths_a)
  files = []
  paths_a.each do |path|
    files += Dir.glob(path)
  end
  files
end
make_paths() click to toggle source
# File lib/komplement/base.rb, line 69
def make_paths
  @dirs.uniq!
  paths = []
  @dirs.each do |dir|
    @filetypes.each do |filetype|
      paths.push File.join(dir, '**', filetype)
    end
  end
  paths
end
process_output(unknown_h) click to toggle source

@param unknown_h are the unknown elements that may have been detected

# File lib/komplement/base.rb, line 89
def process_output(unknown_h)
  if unknown_h.empty?
    $stdout.puts 'No problematic html found'.green.bold
    return EXIT_SUCCESS
  end

  $stderr.puts 'ERROR: Detected possible unknown components:'.red.bold

  unknown_h.each do |file_name, elements|
    $stderr.puts file_name.green.bold
    elements.each do |offending_element_name|
      $stderr.puts "  #{offending_element_name}"
    end
  end

  EXIT_FAIL
end