Module: Yarrow::HTML::AssetTags

Includes:
Configurable
Included in:
Output::Context
Defined in:
lib/yarrow/html/asset_tags.rb

Instance Method Summary collapse

Methods included from Configurable

#config

Instance Method Details

#base_url_pathObject

Computes the base URL path to assets in the public web directory.



13
14
15
16
17
18
19
20
21
22
# File 'lib/yarrow/html/asset_tags.rb', line 13

def base_url_path
  raise Yarrow::ConfigurationError if config.assets.nil?
  raise Yarrow::ConfigurationError if config.output_dir.nil?

  # TODO: prepend configurable CDN URL for host path
  # TODO: dev/production mode switch

  assets_path = config.assets.output_dir
  assets_path.gsub(config.output_dir, '')
end

TODO: support asset path option?



41
42
43
44
45
46
47
48
49
50
# File 'lib/yarrow/html/asset_tags.rb', line 41

def link_tag(options)
  if options.has_key? :asset and manifest.exists? options[:asset]
    stylesheet_path = manifest.digest_path(options[:asset])
    href_path = "#{base_url_path}/#{stylesheet_path}"      
  else
    href_path = options[:href]
  end

  "<link href=\"#{href_path}\" rel=\"stylesheet\" type=\"text/css\">"
end

#manifestObject

TODO: make sprockets manifest optional/pluggable



7
8
9
# File 'lib/yarrow/html/asset_tags.rb', line 7

def manifest
  Yarrow::Assets::Manifest.new(config)
end

#script_tag(options) ⇒ Object

TODO: support asset path option?



29
30
31
32
33
34
35
36
37
38
# File 'lib/yarrow/html/asset_tags.rb', line 29

def script_tag(options)
  if options.has_key? :asset and manifest.exists? options[:asset]
    script_path = manifest.digest_path(options[:asset])
    src_path = "#{base_url_path}/#{script_path}"
  else
    src_path = options[:src]
  end

  "<script src=\"#{src_path}\"></script>"
end

#script_tagsObject



24
25
26
# File 'lib/yarrow/html/asset_tags.rb', line 24

def script_tags
  manifest.js_logical_paths.map { |asset_path| script_tag(asset: asset_path) }.join("\n")
end