lib/roda/plugins/assets.rb in roda-cj-1.0.4 vs lib/roda/plugins/assets.rb in roda-cj-1.0.5
- old
+ new
@@ -1,7 +1,8 @@
require "tilt"
-require 'open-uri'
+require 'net/http'
+require 'uri'
class Roda
module RodaPlugins
module Assets
def self.load_dependencies(app, opts={})
@@ -51,11 +52,11 @@
opts[:assets]
end
# Generates a unique id, this is used to keep concat/compiled files
# from caching in the browser when they are generated
- def assets_unique_id type
+ def assets_unique_id(type)
if unique_id = instance_variable_get("@#{type}")
unique_id
else
path = "#{assets_opts[:compiled_path]}/#{assets_opts[:"#{type}_folder"]}"
file = "#{path}/#{assets_opts[:compiled_name]}.#{type}"
@@ -81,23 +82,25 @@
end
end
private
- def compile_process_files files, type, folder
+ def compile_process_files(files, type, folder)
require 'yuicompressor'
- r = new
+ # start app instance
+ app = new
+ # content to render to file
content = ''
files.each do |file|
if type != folder && !file[/^\.\//] && !file[/^http/]
file = "#{folder}/#{file}"
end
- content += r.read_asset_file file, type
+ content += app.read_asset_file file, type
end
path = assets_opts[:compiled_path] \
+ "/#{assets_opts[:"#{type}_folder"]}/" \
+ assets_opts[:compiled_name] \
@@ -148,10 +151,11 @@
tags.join "\n"
end
end
def render_asset(file, type)
+ # convert back url safe to period
file.gsub!(/(\$2E|%242E)/, '.')
if !assets_opts[:compiled] && !assets_opts[:concat]
read_asset_file file, type
elsif assets_opts[:compiled]
@@ -175,45 +179,43 @@
content
end
end
- def read_asset_file file, type
+ def read_asset_file(file, type)
folder = assets_opts[:"#{type}_folder"]
# If there is no file it must be a remote file request.
# Lets set the file to the url
if file == ''
route = assets_opts[:route]
file = env['SCRIPT_NAME'].gsub(/^\/#{route}\/#{folder}\//, '')
end
+ # If it's not a url or parent direct append the full path
if !file[/^\.\//] && !file[/^http/]
- path = assets_opts[:path] + '/' + folder + "/#{file}"
- else
- path = file
+ file = assets_opts[:path] + '/' + folder + "/#{file}"
end
+ # set the current engine
engine = assets_opts[:"#{type}_engine"]
- # render via tilt
- if File.exists? "#{path}.#{engine}"
- render path: "#{path}.#{engine}"
- # read file directly
- elsif File.exists? "#{path}.#{type}"
- File.read "#{path}.#{type}"
- # grab remote file content
+ if File.exists? "#{file}.#{engine}"
+ # render via tilt
+ render path: "#{file}.#{engine}"
+ elsif File.exists? "#{file}.#{type}"
+ # read file directly
+ File.read "#{file}.#{type}"
elsif file[/^http/]
- open(file).read
- # as a last attempt lets see if the file can be rendered by tilt
- # otherwise load the file directly
- elsif File.exists? path
- begin
- render path: path
- rescue
- File.read path
- end
+ # grab remote file content
+ Net::HTTP.get(URI.parse(file))
+ elsif File.exists?(file) && !file[/\.#{type}$/]
+ # Render via tilt if the type isn't css or js
+ render path: file
+ else
+ # if it is css/js read the file directly
+ File.read file
end
end
# Shortcut for class opts
def assets_opts
@@ -227,16 +229,16 @@
private
# CSS tag template
# <link rel="stylesheet" href="theme.css">
- def css_assets_tag attrs
+ def css_assets_tag(attrs)
"<link rel=\"stylesheet\" #{attrs} />"
end
# JS tag template
# <script src="scriptfile.js"></script>
- def js_assets_tag attrs
+ def js_assets_tag(attrs)
"<script type=\"text/javascript\" #{attrs}></script>"
end
end
module RequestClassMethods