class Jekyll::KargWare::Integrity::Parser

jekyll-kw-sri parser class

Attributes

configuration[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/jekyll-kw-sri/parser.rb, line 12
def initialize(options = {})
  @configuration = Configuration.new(options)
end

Public Instance Methods

add_source_mapping_url(filename, data) click to toggle source
# File lib/jekyll-kw-sri/parser.rb, line 36
def add_source_mapping_url(filename, data)
  if @configuration.write_source_mapping_url
    base = File.basename(filename)
    base = base.sub! 'scss', 'css'

    data + "\n/*# sourceMappingURL=#{base}.map */"
  else
    data
  end
end
calc_integrity(filename, data) click to toggle source
# File lib/jekyll-kw-sri/parser.rb, line 16
def calc_integrity(filename, data)
  hash_type = @configuration.hash_type

  data_modified = add_source_mapping_url(filename, data)

  # Debuging, save rendered css file as tmp file
  File.open("#{filename}.tmp", 'w') { |file| file.write(data_modified) } if @configuration.create_tmpfile

  case hash_type
  when 'sha256'
    "sha256-#{Digest::SHA256.base64digest data_modified}"
  when 'sha384'
    "sha384-#{Digest::SHA384.base64digest data_modified}"
  when 'sha512'
    "sha512-#{Digest::SHA512.base64digest data_modified}"
  else
    raise Jekyll::KargWare::Integrity::InvalidHashTypeException, "The type of the hash '#{hash_type}' is invalid!'"
  end
end