class Danger::DangerHtmllint

[Danger](danger.systems/ruby/) plugin depends on [htmllint-cli](github.com/htmllint/htmllint-cli/).

@example Run htmllint and send violations as inline comment.

# Lint added and modified files only
htmllint.lint

@see konifar/danger-htmllint @tags lint, htmllint

Attributes

fail_on_error[RW]

Set danger fail when errors are detected @return [Bool]

rc_path[RW]

.htmllintrc path @return [String]

Public Instance Methods

lint() click to toggle source

Execute htmllint and send comment @return [void]

# File lib/htmllint/plugin.rb, line 26
def lint
  return if target_files.empty?

  result = run_htmllint(htmllint_bin_path, target_files)
  send_comment(parse(result))
end

Private Instance Methods

htmllint_bin_path() click to toggle source
# File lib/htmllint/plugin.rb, line 35
def htmllint_bin_path
  local = "./node_modules/.bin/htmllint"
  raise "htmlslint not found in ./node_modules/.bin/htmllint" unless File.exist?(local)

  local
end
htmllint_command(bin, target_files) click to toggle source
# File lib/htmllint/plugin.rb, line 42
def htmllint_command(bin, target_files)
  command = "#{bin} #{target_files.join(' ')}"
  command << " --rc #{rc_path}" if rc_path
  p command
  command
end
parse(result) click to toggle source
# File lib/htmllint/plugin.rb, line 62
def parse(result)
  p result
  list = []
  result.split("\n").each do |item|
    next if item == ""

    path_and_err = item.split(".html:")
    next if path_and_err.length < 2

    file_path = "#{path_and_err.first}.html"

    line_col_err_msg = path_and_err.last.split(", ")
    line = line_col_err_msg[0].sub("line ", "").to_i
    col = line_col_err_msg[1].sub("col ", "")
    err_msg = line_col_err_msg[2]

    list << {
      file_path: file_path,
      line: line,
      severity: severity_level(fail_on_error),
      message: "#{err_msg} (col:#{col})"
    }
  end

  list
end
run_htmllint(bin, target_files) click to toggle source
# File lib/htmllint/plugin.rb, line 49
def run_htmllint(bin, target_files)
  `#{htmllint_command(bin, target_files)}`
end
send_comment(errors) click to toggle source
# File lib/htmllint/plugin.rb, line 93
def send_comment(errors)
  errors.each do |error|
    send(error[:severity], error[:message], file: error[:file_path], line: error[:line])
  end
end
severity_level(fail_on_error) click to toggle source
# File lib/htmllint/plugin.rb, line 89
def severity_level(fail_on_error)
  fail_on_error ? "fail" : "warn"
end
target_files() click to toggle source
# File lib/htmllint/plugin.rb, line 53
def target_files
  files = ((git.modified_files - git.deleted_files) + git.added_files)
  result = []
  files.each do |file|
    result << file if file.include?(".html")
  end
  result
end