Class: Asset

Inherits:
Liquid::Tag
  • Object
show all
Includes:
Liquid::Tag::Disableable
Defined in:
lib/tags/asset.rb

Constant Summary collapse

SYNTAX =
/(#{Liquid::QuotedFragment}+|\w+)/o.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ Asset

Returns a new instance of Asset.

Raises:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/tags/asset.rb', line 19

def initialize(tag_name, markup, tokens)
  super
  raise AssetError, 'Invalid layout syntax' unless markup =~ SYNTAX

  @path = parse_expression(Regexp.last_match(1))
  # This is defaulted to the pages dir, because it represents the structure
  # of our website. Asset directories are copied as siblings at runtime.
  @@root_dir ||= File.join(Dir.pwd, 'pages')

  @attributes = {}

  markup.scan(Liquid::TagAttributes) do |key, value|
    @attributes[key] = parse_expression(value)
  end
end

Class Method Details

.helper_port=(port) ⇒ Object



39
40
41
# File 'lib/tags/asset.rb', line 39

def self.helper_port=(port)
  @@helper_port = port
end

.root_dir=(dir) ⇒ Object



35
36
37
# File 'lib/tags/asset.rb', line 35

def self.root_dir=(dir)
  @@root_dir = dir
end

Instance Method Details

#render_to_output_buffer(context, output) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/tags/asset.rb', line 43

def render_to_output_buffer(context, output)
  path = @path
  path = path.evaluate(context) if path.is_a? Liquid::VariableLookup
  unless @@root_dir
    raise AssetError,
          'root_dir must be set on Archival::Asset'
  end

  unless context.key? 'template_path'
    raise AssetError,
          'template_path must be provided to parse when using assets'
  end
  template_path = File.dirname(context['template_path'])
  abs_asset_path = Pathname.new(File.join(@@root_dir, path))
  asset_path = abs_asset_path.relative_path_from(template_path).cleanpath.to_s
  output << if @attributes['serve'] == true
              "http://localhost:#{@@helper_port}/#{asset_path}"
            else
              asset_path
            end
  output
end