Class: Embarista::Filters::ManifestUrlFilter

Inherits:
Rake::Pipeline::Filter
  • Object
show all
Defined in:
lib/embarista/filters/manifest_url_filter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ ManifestUrlFilter

Returns a new instance of ManifestUrlFilter.



6
7
8
9
10
# File 'lib/embarista/filters/manifest_url_filter.rb', line 6

def initialize(options= {}, &block)
  @options = options || {}
  raise 'Must pass :path option' unless @options.key?(:path)
  super(&block)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/embarista/filters/manifest_url_filter.rb', line 4

def options
  @options
end

#urls_manifestObject



13
14
15
16
17
18
19
20
21
# File 'lib/embarista/filters/manifest_url_filter.rb', line 13

def urls_manifest
  @urls_manifest ||= begin
    if File.exists?(options[:path])
      JSON.parse(IO.read(options[:path]))
    else
      {}
    end
  end
end

Instance Method Details

#generate_output(inputs, output) ⇒ Object



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
48
# File 'lib/embarista/filters/manifest_url_filter.rb', line 23

def generate_output(inputs, output)
  inputs.each do |input|
    code = input.read
    code.gsub!(%r{\bmanifest_url\(\s*(["'/])([^"']+)\1\)}) do |m|
      quote_char = $1
      path = $2

      # take the anchor/querystring off if there is one
      post_path_start = path.index(/[?#]/)
      if post_path_start
        post_path = path[post_path_start..-1]
        path = path[0..post_path_start-1]
      end

      path = "/#{path}" unless path =~ %r{^/}
      path = "#{options[:prefix]}#{path}"
      resolved_path = urls_manifest[path] || path

      # put the anchor/querystring back on, if there is one
      resolved_path = resolved_path + post_path if post_path

      "(#{options[:prepend]}'#{resolved_path}')"
    end
    output.write(code)
  end
end