split out core extensions from features

This commit is contained in:
Thomas Reynolds 2011-07-06 09:50:34 -07:00
parent a6ad06f3e6
commit 7a35c1f48e
5 changed files with 27 additions and 23 deletions

View file

@ -0,0 +1,92 @@
require "yaml"
require "httparty"
require "thor"
module Middleman::CoreExtensions::Data
class << self
def registered(app)
app.extend ClassMethods
app.helpers Helpers
end
alias :included :registered
end
module Helpers
def data
@@data ||= Middleman::CoreExtensions::Data::DataObject.new(self)
end
end
class DataObject
def initialize(app)
@app = app
end
def method_missing(path)
response = nil
@@local_sources ||= {}
@@remote_sources ||= {}
if @@local_sources.has_key?(path.to_s)
response = @@local_sources[path.to_s]
elsif @@remote_sources.has_key?(path.to_s)
response = HTTParty.get(@@remote_sources[path.to_s]).parsed_response
else
file_path = File.join(@app.class.root, "data", "#{path}.yml")
if File.exists? file_path
response = YAML.load_file(file_path)
end
end
if response
recursively_enhance(response)
end
end
def self.add_source(name, json_url)
@@remote_sources ||= {}
@@remote_sources[name.to_s] = json_url
end
def self.data_content(name, content)
@@local_sources ||= {}
@@local_sources[name.to_s] = content
end
private
def recursively_enhance(data)
if data.is_a? Hash
data = Thor::CoreExt::HashWithIndifferentAccess.new(data)
data.each do |key, val|
data[key] = recursively_enhance(val)
end
data
elsif data.is_a? Array
data.each_with_index do |val, i|
data[i] = recursively_enhance(val)
end
data
else
data
end
end
end
module ClassMethods
# Makes HTTP json data available in the data object
#
# data_source :my_json, "http://my/file.json"
#
# Available in templates as:
#
# data.my_json
def data_source(name, url)
Middleman::CoreExtensions::Data::DataObject.add_source(name, url)
end
def data_content(name, content)
Middleman::CoreExtensions::Data::DataObject.data_content(name, content)
end
end
end

View file

@ -0,0 +1,81 @@
require "padrino-helpers"
module Middleman::CoreExtensions::DefaultHelpers
class << self
def registered(app)
# Use Padrino Helpers
app.register Padrino::Helpers
# Middleman Helpers
app.helpers Helpers
end
alias :included :registered
end
module Helpers
def auto_stylesheet_link_tag(separator="/")
auto_tag(:css, separator) do |path|
stylesheet_link_tag path
end
end
def auto_javascript_include_tag(separator="/")
auto_tag(:js, separator) do |path|
javascript_include_tag path
end
end
def auto_tag(asset_ext, separator="/", asset_dir=nil)
if asset_dir.nil?
asset_dir = case asset_ext
when :js then self.class.js_dir
when :css then self.class.css_dir
end
end
path = request.path_info.dup
# If the basename of the request as no extension, assume we are serving a
# directory and join index_file to the path.
path = File.join(path, self.class.index_file) if File.extname(path).empty?
path = path.gsub(%r{^/}, '')
path = path.gsub(File.extname(path), ".#{asset_ext}")
path = path.gsub("/", separator)
view = File.join(self.class.views, asset_dir, path)
yield path if File.exists?(view) or Dir["#{view}.*"].any?
end
def page_classes
path = request.path_info.dup
path << settings.index_file if path.match(%r{/$})
path = path.gsub(%r{^/}, '')
classes = []
parts = path.split('.')[0].split('/')
parts.each_with_index { |path, i| classes << parts.first(i+1).join('_') }
classes.join(' ')
end
def asset_url(path, prefix="")
Middleman::Assets.get_url(path, prefix, request)
end
# Padrino's asset handling needs to pass through ours
def asset_path(kind, source)
return source if source =~ /^http/
asset_folder = case kind
when :css then settings.css_dir
when :js then settings.js_dir
when :images then settings.images_dir
else kind.to_s
end
source = source.to_s.gsub(/\s/, '')
ignore_extension = (kind == :images) # don't append extension
source << ".#{kind}" unless ignore_extension or source =~ /\.#{kind}/
result_path = source if source =~ %r{^/} # absolute path
result_path ||= asset_url(source, asset_folder)
timestamp = asset_timestamp(result_path)
"#{result_path}#{timestamp}"
end
end
end