Class: Jekyll::KargWare::SriScssHashTag

Inherits:
Tags::IncludeRelativeTag
  • Object
show all
Defined in:
lib/jekyll-kw-sri.rb

Overview

jekyll-kw-sri custom tag

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, input, tokens) ⇒ SriScssHashTag

Returns a new instance of SriScssHashTag.



12
13
14
15
16
17
# File 'lib/jekyll-kw-sri.rb', line 12

def initialize(tag_name, input, tokens)
  super

  raise 'Please enter a file path' if input.length <= 0
  # File.exists? is file?
end

Instance Method Details

#cache_compiled_scss(path, _context, compute) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/jekyll-kw-sri.rb', line 49

def cache_compiled_scss(path, _context, compute)
  @cached_scss ||= {}
  if @cached_scss.key?(path)
    @cached_scss[path]
  else
    @cached_scss[path] = compute.call
  end
end

#render(context) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/jekyll-kw-sri.rb', line 19

def render(context)
  cache_compiled_scss(@file, context, lambda {
    if context.nil? || context.registers[:site].nil?
      puts 'WARNING: There was no context, generate default site and context'
      site = Jekyll::Site.new(Jekyll::Configuration::DEFAULTS)
      context = Liquid::Context.new({}, {}, { site: site })
    else
      site = context.registers[:site]
      # Read the global configuration
      @sri_config = context.registers[:site].config['kw-sri'] || {}
    end

    # Render the context with the base-class
    converter = site.find_converter_instance(Jekyll::Converters::Scss)
    result = super(context) # super_render(context)
    scss = result.gsub(/^---.*---/m, '')
    data = converter.convert(scss)

    # Get path out of the file object
    file = render_variable(context) || @file
    validate_file_name(file)
    path = locate_include_file(context, file, site.safe)

    # Use default config for kw-sri if it is nil
    @sri_config ||= Jekyll::KargWare::Integrity::Configuration::DEFAULT_CONFIG

    Integrity::Parser.new(@sri_config).calc_integrity(path, data)
  })
end

#tag_includes_dirs(context) ⇒ Object

Register the sccs file as include folder



59
60
61
# File 'lib/jekyll-kw-sri.rb', line 59

def tag_includes_dirs(context)
  [context.registers[:site].source].freeze
end