module Roda::RodaPlugins::Assets::InstanceMethods

  1. lib/roda/plugins/assets.rb

Public Instance methods

assets(type, attrs = OPTS)

Return a string containing html tags for the given asset type. This will use a script tag for the :js type and a link tag for the :css type.

To return the tags for a specific asset group, use an array for the type, such as [:css, :frontend].

When the assets are not compiled, this will result in a separate tag for each asset file. When the assets are compiled, this will result in a single tag to the compiled asset file.

[show source]
    # File lib/roda/plugins/assets.rb
706 def assets(type, attrs = OPTS)
707   ltype = type.is_a?(Array) ? type[0] : type
708 
709   o = self.class.assets_opts
710   if o[:compiled] && (algo = o[:sri]) && (hash = _compiled_assets_hash(type))
711     attrs = Hash[attrs]
712     attrs[:integrity] = "#{algo}-#{h([[hash].pack('H*')].pack('m').tr("\n", ''))}"
713   end
714 
715   attrs = attrs.map{|k,v| "#{k}=\"#{h(v)}\""}.join(' ')
716 
717   if ltype == :js
718     tag_start = "<script type=\"text/javascript\" #{attrs} src=\""
719     tag_end = "\"></script>"
720   else
721     tag_start = "<link rel=\"stylesheet\" #{attrs} href=\""
722     tag_end = "\" />"
723   end
724 
725   paths = assets_paths(type)
726   if o[:early_hints]
727     early_hint_as = ltype == :js ? 'script' : 'style'
728     send_early_hints('Link'=>paths.map{|p| "<#{p}>; rel=preload; as=#{early_hint_as}"}.join("\n"))
729   end
730   paths.map{|p| "#{tag_start}#{h(p)}#{tag_end}"}.join("\n")
731 end
assets_paths(type)

Return an array of paths for the given asset type and optionally asset group. See the assets function documentation for details.

[show source]
    # File lib/roda/plugins/assets.rb
653 def assets_paths(type)
654   o = self.class.assets_opts
655   if type.is_a?(Array)
656     ltype, *dirs = type
657   else
658     ltype = type
659   end
660   stype = ltype.to_s
661 
662   url_prefix = request.script_name if self.class.opts[:add_script_name]
663   relative_paths = o[:relative_paths]
664 
665   paths = if o[:compiled]
666     relative_paths = false if o[:compiled_asset_host]
667     if ukey = _compiled_assets_hash(type, true)
668       ["#{o[:compiled_asset_host]}#{url_prefix}/#{o[:"compiled_#{stype}_prefix"]}.#{ukey}.#{stype}"]
669     else
670       []
671     end
672   else
673     asset_dir = o[ltype]
674     if dirs && !dirs.empty?
675       dirs.each{|f| asset_dir = asset_dir[f]}
676       prefix = "#{dirs.join('/')}/" if o[:group_subdirs]
677     end
678     Array(asset_dir).map do |f|
679       if ts = o[:timestamp_paths]
680         mtime = asset_last_modified(File.join(o[:"#{stype}_path"], *[prefix, f].compact))
681         mtime = "#{sprintf("%i%06i", mtime.to_i, mtime.usec)}#{ts}"
682       end
683       "#{url_prefix}/#{o[:"#{stype}_prefix"]}#{mtime}#{prefix}#{f}#{o[:"#{stype}_suffix"]}"
684     end
685   end
686 
687   if relative_paths
688     paths.map! do |path|
689       "#{relative_prefix}#{path}"
690     end
691   end
692 
693   paths
694 end
read_asset_file(file, type)

Return the content of the file if it is already of the correct type. Otherwise, render the file using the render plugin. file should be the relative path to the file from the current directory.

[show source]
    # File lib/roda/plugins/assets.rb
762 def read_asset_file(file, type)
763   o = self.class.assets_opts
764 
765   content = if file.end_with?(".#{type}")
766     ::File.read(file)
767   else
768     render_asset_file(file, :template_opts=>o[:"#{type}_opts"], :dependencies=>o[:expanded_dependencies][file])
769   end
770 
771   o[:postprocessor] ? o[:postprocessor].call(file, type, content) : content
772 end
render_asset(file, type)

Render the asset with the given filename. When assets are compiled, or when the file is already of the given type (no rendering necessary), this returns the contents of the compiled file. When assets are not compiled and the file is not already in the same format, this will render the asset using the render plugin. In both cases, if the file has not been modified since the last request, this will return a 304 response.

[show source]
    # File lib/roda/plugins/assets.rb
740 def render_asset(file, type)
741   o = self.class.assets_opts
742   if o[:compiled]
743     file = "#{o[:"compiled_#{type}_path"]}#{file}"
744 
745     if o[:gzip] && env['HTTP_ACCEPT_ENCODING'] =~ /\bgzip\b/
746       @_response['Content-Encoding'] = 'gzip'
747       file += '.gz'
748     end
749 
750     check_asset_request(file, type, ::File.stat(file).mtime)
751     ::File.read(file)
752   else
753     file = "#{o[:"#{type}_path"]}#{file}"
754     check_asset_request(file, type, asset_last_modified(file))
755     read_asset_file(file, type)
756   end
757 end