cleanup calls to self. throughout

This commit is contained in:
Thomas Reynolds 2011-11-20 21:21:19 -08:00
parent 1fbeedb573
commit 6de8d5dde5
22 changed files with 201 additions and 230 deletions

View file

@ -8,48 +8,50 @@ require 'rubygems'
module Middleman
module ProjectLocator
def self.locate_middleman_root!(args)
cwd = Dir.pwd
class << self
def locate_middleman_root!(args)
cwd = Dir.pwd
if !in_middleman_project? && !in_middleman_project_subdirectory?
$stderr.puts "== Error: Could not find a Middleman project config, perhaps you are in the wrong folder?"
return
end
if !in_middleman_project? && !in_middleman_project_subdirectory?
$stderr.puts "== Error: Could not find a Middleman project config, perhaps you are in the wrong folder?"
return
end
if in_middleman_project?
did_locate_middleman_project(cwd, args)
return
end
if in_middleman_project?
did_locate_middleman_project(cwd, args)
return
end
Dir.chdir("..") do
# Recurse in a chdir block: if the search fails we want to be sure
# the application is generated in the original working directory.
locate_middleman_root!(args) unless cwd == Dir.pwd
Dir.chdir("..") do
# Recurse in a chdir block: if the search fails we want to be sure
# the application is generated in the original working directory.
locate_middleman_root!(args) unless cwd == Dir.pwd
end
rescue SystemCallError
# could not chdir, no problem just return
end
rescue SystemCallError
# could not chdir, no problem just return
end
def self.in_middleman_project?
File.exists?('config.rb')
end
def in_middleman_project?
File.exists?('config.rb')
end
def self.in_middleman_project_subdirectory?(path = Pathname.new(Dir.pwd))
File.exists?(File.join(path, 'config.rb')) || !path.root? && in_middleman_project_subdirectory?(path.parent)
end
def in_middleman_project_subdirectory?(path = Pathname.new(Dir.pwd))
File.exists?(File.join(path, 'config.rb')) || !path.root? && in_middleman_project_subdirectory?(path.parent)
end
def self.did_locate_middleman_project(path, args)
# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('Gemfile', path)
def did_locate_middleman_project(path, args)
# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('Gemfile', path)
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
start_cli!(args)
end
start_cli!(args)
end
def self.start_cli!(args)
require 'middleman'
Middleman::CLI.start(args)
def start_cli!(args)
require 'middleman'
Middleman::CLI.start(args)
end
end
end
end
@ -59,7 +61,8 @@ args = ARGV.dup
ARG_ALIASES = {
"s" => "server",
"b" => "build",
"i" => "init"
"i" => "init",
"w" => "watch"
}
if ARG_ALIASES.has_key?(args[0])

View file

@ -154,53 +154,53 @@ module Middleman
end
EXTENSION_FILE = File.join("lib", "middleman_init.rb")
def self.load_extensions_in_path
extensions = rubygems_latest_specs.select do |spec|
spec_has_file?(spec, EXTENSION_FILE)
end
class << self
def load_extensions_in_path
extensions = rubygems_latest_specs.select do |spec|
spec_has_file?(spec, EXTENSION_FILE)
end
extensions.each do |spec|
require spec.name
# $stderr.puts "require: #{spec.name}"
extensions.each do |spec|
require spec.name
# $stderr.puts "require: #{spec.name}"
end
end
end
def self.rubygems_latest_specs
# If newer Rubygems
if ::Gem::Specification.respond_to? :latest_specs
::Gem::Specification.latest_specs
else
::Gem.source_index.latest_specs
def rubygems_latest_specs
# If newer Rubygems
if ::Gem::Specification.respond_to? :latest_specs
::Gem::Specification.latest_specs
else
::Gem.source_index.latest_specs
end
end
end
def self.spec_has_file?(spec, path)
full_path = File.join(spec.full_gem_path, path)
File.exists?(full_path)
end
def spec_has_file?(spec, path)
full_path = File.join(spec.full_gem_path, path)
File.exists?(full_path)
end
def self.server(&block)
sandbox = Class.new(Middleman::Base)
# sandbox.class_eval(&block) if block_given?
sandbox
end
def server(&block)
Class.new(Middleman::Base)
end
def self.start_server(options={})
opts = {
:Port => options[:port] || 4567,
:AccessLog => []
}
def start_server(options={})
opts = {
:Port => options[:port] || 4567,
:AccessLog => []
}
app_class = options[:app] ||= ::Middleman.server.new
opts[:app] = app_class
opts[:server] = 'thin'
app_class = options[:app] ||= ::Middleman.server.new
opts[:app] = app_class
opts[:server] = 'thin'
# require "thin"
# ::Thin::Logging.silent = true if options[:debug] != "true"
# require "thin"
# ::Thin::Logging.silent = true if options[:debug] != "true"
server = ::Rack::Server.new(opts)
server.start
server
server = ::Rack::Server.new(opts)
server.start
server
end
end
end

View file

@ -1,6 +1,5 @@
require "rack"
require "tilt"
require "i18n"
require "middleman/vendor/hooks-0.2.0/lib/hooks"
require "active_support"
@ -62,8 +61,6 @@ class Middleman::Base
@defaults ||= {}
@defaults[key] = value
end
def asset_stamp; false; end
end
def set(key, value=nil, &block)
@ -89,7 +86,7 @@ class Middleman::Base
set :images_dir, "images" # Where to look for images
set :build_dir, "build" # Which folder are builds output to
set :http_prefix, nil # During build, add a prefix for absolute paths
set :http_prefix, "/" # During build, add a prefix for absolute paths
set :views, "source"
@ -140,9 +137,7 @@ class Middleman::Base
def initialize(&block)
@current_path = nil
self.class.superclass.defaults.each do |k, v|
set(k, v)
end
self.class.superclass.defaults.each { |k,v| set k,v }
instance_exec(&block) if block_given?

View file

@ -31,12 +31,14 @@ module Middleman
include Thor::Actions
include Middleman::ThorActions
def self.shared_rack
@shared_rack ||= begin
mock = ::Rack::MockSession.new(SHARED_SERVER.to_rack_app)
sess = ::Rack::Test::Session.new(mock)
response = sess.get("__middleman__")
sess
class << self
def shared_rack
@shared_rack ||= begin
mock = ::Rack::MockSession.new(SHARED_SERVER.to_rack_app)
sess = ::Rack::Test::Session.new(mock)
response = sess.get("__middleman__")
sess
end
end
end

View file

@ -1,8 +1,8 @@
module Middleman::CoreExtensions::Assets
class << self
def registered(app)
# Disable Padrino cache buster until explicitly enabled
# app.set :asset_stamp, false
# Disable Padrino cache buster
app.set :asset_stamp, false
app.send :include, InstanceMethod
end
@ -11,7 +11,7 @@ module Middleman::CoreExtensions::Assets
module InstanceMethod
def asset_url(path, prefix="")
path.include?("://") ? path : File.join(self.http_prefix || "/", prefix, path)
path.include?("://") ? path : File.join(http_prefix, prefix, path)
end
end
end

View file

@ -1,79 +1,59 @@
require "compass"
module Middleman::CoreExtensions::Compass
class << self
def registered(app)
require "compass"
# Where to look for fonts
app.set :fonts_dir, "fonts"
app.define_hook :compass_config
app.define_hook :after_compass_config
app.after_configuration do
# Support a stand-alone compass config file
# Many options are overwritten by Middleman, but the config is a good
# place to add:
# * output_style
# * disable_warnings
# * sass_options
# * line_comments
# * sprite_engine
# * chunky_png_options
compass_config_file = File.join(self.root, "compass.config")
if File.exists?(compass_config_file)
::Compass.add_project_configuration(compass_config_file)
end
::Compass.configuration do |config|
config.project_path = self.root
config.environment = :development
config.cache_path = File.join(self.root, ".sass-cache")
config.project_path = root
config.environment = :development
config.cache_path = File.join(root, ".sass-cache")
config.sass_dir = File.join(views, css_dir)
config.css_dir = File.join(views, css_dir)
config.javascripts_dir = File.join(views, js_dir)
config.fonts_dir = File.join(views, fonts_dir)
config.images_dir = File.join(views, images_dir)
views_root = File.basename(self.views)
config.sass_dir = File.join(views_root, self.css_dir)
config.css_dir = File.join(views_root, self.css_dir)
config.javascripts_dir = File.join(views_root, self.js_dir)
config.fonts_dir = File.join(views_root, self.fonts_dir)
config.images_dir = File.join(views_root, self.images_dir)
config.http_images_path = if self.respond_to? :http_images_path
self.http_images_path
config.http_images_path = if respond_to? :http_images_path
http_images_path
else
File.join(self.http_prefix || "/", self.images_dir)
File.join(http_prefix, images_dir)
end
config.http_stylesheets_path = if self.respond_to? :http_css_path
self.http_css_path
config.http_stylesheets_path = if respond_to? :http_css_path
http_css_path
else
File.join(self.http_prefix || "/", self.css_dir)
File.join(http_prefix, css_dir)
end
config.http_javascripts_path = if self.respond_to? :http_js_path
self.http_js_path
config.http_javascripts_path = if respond_to? :http_js_path
http_js_path
else
File.join(self.http_prefix || "/", self.js_dir)
File.join(http_prefix, js_dir)
end
config.http_fonts_path = if self.respond_to? :http_fonts_path
self.http_fonts_path
config.http_fonts_path = if respond_to? :http_fonts_path
http_fonts_path
else
File.join(self.http_prefix || "/", self.fonts_dir)
File.join(http_prefix, fonts_dir)
end
config.asset_cache_buster :none
config.output_style = :nested
# config.add_import_path(config.sass_dir)
end
# Required for relative paths
configure :build do
::Compass.configuration do |config|
config.environment = :production
build_root = File.basename(self.build_dir)
config.css_dir = File.join(build_root, self.css_dir)
config.images_dir = File.join(build_root, self.images_dir)
config.fonts_dir = File.join(build_root, self.fonts_dir)
config.css_dir = File.join(build_dir, css_dir)
config.images_dir = File.join(build_dir, images_dir)
config.fonts_dir = File.join(build_dir, fonts_dir)
end
end

View file

@ -40,8 +40,20 @@ module Middleman::CoreExtensions::Data
end
class DataStore
def self.matcher
%r{[\w-]+\.(yml|yaml|json)$}
class << self
def matcher
%r{[\w-]+\.(yml|yaml|json)$}
end
def data_content(name, content)
@@local_sources ||= {}
@@local_sources[name.to_s] = content
end
def data_callback(name, proc)
@@callback_sources ||= {}
@@callback_sources[name.to_s] = proc
end
end
def initialize(app)
@ -121,16 +133,6 @@ module Middleman::CoreExtensions::Data
data
end
def self.data_content(name, content)
@@local_sources ||= {}
@@local_sources[name.to_s] = content
end
def self.data_callback(name, proc)
@@callback_sources ||= {}
@@callback_sources[name.to_s] = proc
end
private
def recursively_enhance(data)

View file

@ -22,6 +22,10 @@ module Middleman::CoreExtensions::DefaultHelpers
app.helpers ::Padrino::Helpers::TranslationHelpers
app.helpers Helpers
app.initialized do
::I18n.load_path = Dir["#{File.join(root, 'locales', '*.yml')}"]
end
end
alias :included :registered
end
@ -42,15 +46,14 @@ module Middleman::CoreExtensions::DefaultHelpers
def auto_tag(asset_ext, separator="/", asset_dir=nil)
if asset_dir.nil?
asset_dir = case asset_ext
when :js then self.js_dir
when :css then self.css_dir
when :js then js_dir
when :css then css_dir
end
end
# If the basename of the request as no extension, assume we are serving a
# directory and join index_file to the path.
path = full_path(current_path.dup)
path = path.sub(%r{^/}, '')
path = path.gsub(File.extname(path), ".#{asset_ext}")
path = path.gsub("/", separator)
@ -60,7 +63,7 @@ module Middleman::CoreExtensions::DefaultHelpers
def page_classes
path = current_path.dup
path << self.index_file if path.match(%r{/$})
path << index_file if path.match(%r{/$})
path = path.gsub(%r{^/}, '')
classes = []
@ -74,9 +77,9 @@ module Middleman::CoreExtensions::DefaultHelpers
def asset_path(kind, source)
return source if source =~ /^http/
asset_folder = case kind
when :css then self.css_dir
when :js then self.js_dir
when :images then self.images_dir
when :css then css_dir
when :js then js_dir
when :images then images_dir
else kind.to_s
end
source = source.to_s.gsub(/\s/, '')

View file

@ -96,7 +96,7 @@ module Middleman::CoreExtensions::Features
run_hook :before_configuration
# Check for and evaluate local configuration
local_config = File.join(self.root, "config.rb")
local_config = File.join(root, "config.rb")
if File.exists? local_config
puts "== Reading: Local config" if logging?
instance_eval File.read(local_config)

View file

@ -49,8 +49,10 @@ module Middleman::CoreExtensions::FrontMatter
end
class FrontMatter
def self.matcher
%r{source/.*\.html}
class << self
def matcher
%r{source/.*\.html}
end
end
def initialize(app)

View file

@ -38,10 +38,11 @@ module Middleman::CoreExtensions::Sprockets
end
# add paths to js_env (vendor/assets/javascripts)
map "/#{self.js_dir}" do
map "/#{js_dir}" do
run js_env
end
sass.each { |k, v| ::Sprockets::Sass.options[k] = v }
css_env = Middleman::CoreExtensions::Sprockets::StylesheetEnvironment.new(self)
vendor_dir = File.join("vendor", "assets", "stylesheets")
@ -58,7 +59,7 @@ module Middleman::CoreExtensions::Sprockets
css_env.append_path File.join(spec.full_gem_path, app_dir)
end
map("/#{self.css_dir}") do
map("/#{css_dir}") do
run css_env
end
end

View file

@ -4,8 +4,8 @@ module Middleman::Features::AssetHost
app.set :asset_host, nil
app.compass_config do |config|
if self.asset_host.is_a?(Proc)
config.asset_host(&self.asset_host)
if asset_host.is_a?(Proc)
config.asset_host(&asset_host)
end
end
@ -20,10 +20,10 @@ module Middleman::Features::AssetHost
valid_extensions = %w(.png .gif .jpg .jpeg .js .css)
asset_prefix = if self.asset_host.is_a?(Proc)
self.asset_host.call(original_output)
elsif self.asset_host.is_a?(String)
self.asset_host
asset_prefix = if asset_host.is_a?(Proc)
asset_host.call(original_output)
elsif asset_host.is_a?(String)
asset_host
end
File.join(asset_prefix, original_output)

View file

@ -12,12 +12,12 @@ module Middleman::Features::AutomaticImageSizes
def image_tag(path, params={})
if !params.has_key?(:width) && !params.has_key?(:height) && !path.include?("://")
params[:alt] ||= ""
http_prefix = self.http_images_path rescue self.images_dir
http_prefix = http_images_path rescue images_dir
begin
real_path = File.join(self.views, self.images_dir, path)
full_path = File.expand_path(real_path, self.root)
http_prefix = self.http_images_path rescue self.images_dir
real_path = File.join(views, images_dir, path)
full_path = File.expand_path(real_path, root)
http_prefix = http_images_path rescue images_dir
if File.exists? full_path
dimensions = ::FastImage.size(full_path, :raise_on_failure => true)
params[:width] = dimensions[0]

View file

@ -6,7 +6,7 @@ module Middleman::Features::CacheBuster
app.compass_config do |config|
config.asset_cache_buster do |path, real_path|
real_path = real_path.path if real_path.is_a? File
real_path = real_path.gsub(File.join(self.root, self.build_dir), self.views)
real_path = real_path.gsub(File.join(root, build_dir), views)
if File.readable?(real_path)
File.mtime(real_path).strftime("%s")
else
@ -26,15 +26,15 @@ module Middleman::Features::CacheBuster
http_path
else
begin
prefix = self.images_dir if prefix == self.http_images_path
prefix = images_dir if prefix == http_images_path
rescue
end
real_path_static = File.join(prefix, path)
if self.build?
real_path_dynamic = File.join(self.build_dir, prefix, path)
real_path_dynamic = File.expand_path(real_path_dynamic, self.root)
if build?
real_path_dynamic = File.join(build_dir, prefix, path)
real_path_dynamic = File.expand_path(real_path_dynamic, root)
http_path << "?" + File.mtime(real_path_dynamic).strftime("%s") if File.readable?(real_path_dynamic)
elsif sitemap.exists?(real_path_static)
page = sitemap.page(real_path_static)

View file

@ -4,10 +4,10 @@ module Middleman::Features::DirectoryIndexes
app.send :include, InstanceMethods
app.before do
prefix = @original_path.sub(/\/$/, "")
indexed_path = prefix + "/" + self.index_file
indexed_path = prefix + "/" + index_file
extensioned_path = prefix + File.extname(self.index_file)
is_ignored = self.ignored_directory_indexes.include?(extensioned_path)
extensioned_path = prefix + File.extname(index_file)
is_ignored = ignored_directory_indexes.include?(extensioned_path)
if !sitemap.exists?(indexed_path) && !is_ignored
parts = @original_path.split("/")
@ -22,12 +22,12 @@ module Middleman::Features::DirectoryIndexes
end
app.build_reroute do |destination, request_path|
index_ext = File.extname(self.index_file)
new_index_path = "/#{self.index_file}"
index_ext = File.extname(index_file)
new_index_path = "/#{index_file}"
indexed_path = request_path.sub(/\/$/, "") + index_ext
if self.ignored_directory_indexes.include?(request_path)
if ignored_directory_indexes.include?(request_path)
false
elsif request_path =~ /#{new_index_path}$/
false

View file

@ -13,7 +13,7 @@ module Middleman::Features::RelativeAssets
module InstanceMethods
def asset_url(path, prefix="")
begin
prefix = self.images_dir if prefix == self.http_images_path
prefix = images_dir if prefix == http_images_path
rescue
end
@ -24,7 +24,7 @@ module Middleman::Features::RelativeAssets
else
path = File.join(prefix, path) if prefix.length > 0
request_path = @request_path.dup
request_path << self.index_file if path.match(%r{/$})
request_path << index_file if path.match(%r{/$})
request_path.gsub!(%r{^/}, '')
parts = request_path.split('/')

View file

@ -8,24 +8,26 @@ end
module Middleman
module Guard
def self.add_guard(&block)
# Deprecation Warning
puts "== Middleman::Guard.add_guard has been removed. Update your extensions to versions which support this change."
end
def self.start(options={})
options_hash = ""
options.each do |k,v|
options_hash << ", :#{k} => '#{v}'"
class << self
def add_guard(&block)
# Deprecation Warning
puts "== Middleman::Guard.add_guard has been removed. Update your extensions to versions which support this change."
end
guardfile_contents = %Q{
guard 'middleman'#{options_hash} do
watch(%r{(.*)})
def start(options={})
options_hash = ""
options.each do |k,v|
options_hash << ", :#{k} => '#{v}'"
end
}
guardfile_contents = %Q{
guard 'middleman'#{options_hash} do
watch(%r{(.*)})
end
}
::Guard.start({ :guardfile_contents => guardfile_contents })
::Guard.start({ :guardfile_contents => guardfile_contents })
end
end
end
end

View file

@ -1,16 +1,14 @@
require "tilt"
module Middleman::Renderers::ERb
class << self
def registered(app)
app.send :include, InstanceMethods
app.set :erb_engine, :erb
app.set :erb_engine_prefix, ::Tilt
app.after_configuration do
if erb_engine.is_a? Symbol
erb_engine = erb_tilt_template_from_symbol(erb_engine)
engine = engine.to_s
engine = engine == "erb" ? "ERB" : engine.camelize
erb_engine = erb_engine_prefix.const_get("#{engine}Template")
end
::Tilt.prefer(erb_engine)
@ -18,12 +16,4 @@ module Middleman::Renderers::ERb
end
alias :included :registered
end
module InstanceMethods
def erb_tilt_template_from_symbol(engine)
engine = engine.to_s
engine = engine == "erb" ? "ERB" : engine.camelize
erb_engine_prefix.const_get("#{engine}Template")
end
end
end

View file

@ -2,17 +2,16 @@ module Middleman::Renderers::Liquid
class << self
def registered(app)
# Liquid is not included in the default gems,
# but we'll support it if necessary.
# but we'll support it if available.
begin
require "liquid"
app.after_configuration do
Liquid::Template.file_system = Liquid::LocalFileSystem.new(self.source_dir)
Liquid::Template.file_system = Liquid::LocalFileSystem.new(source_dir)
provides_metadata %r{\.liquid$} do |path|
{ :locals => { :data => data.to_h } }
end
end
rescue LoadError
end

View file

@ -1,13 +1,17 @@
module Middleman::Renderers::Markdown
class << self
def registered(app)
app.send :include, InstanceMethods
app.set :markdown_engine, nil
begin
require "maruku"
app.set :markdown_engine, :maruku
rescue LoadError
app.set :markdown_engine, nil
begin
require "rdiscount"
app.set :markdown_engine, :rdiscount
rescue LoadError
end
end
app.set :markdown_engine_prefix, ::Tilt
@ -15,7 +19,9 @@ module Middleman::Renderers::Markdown
app.after_configuration do
unless markdown_engine.nil?
if markdown_engine.is_a? Symbol
markdown_engine = markdown_tilt_template_from_symbol(markdown_engine)
engine = engine.to_s
engine = engine == "rdiscount" ? "RDiscount" : engine.camelize
markdown_engine = markdown_engine_prefix.const_get("#{engine}Template")
end
::Tilt.prefer(markdown_engine)
@ -24,12 +30,4 @@ module Middleman::Renderers::Markdown
end
alias :included :registered
end
module InstanceMethods
def markdown_tilt_template_from_symbol(engine)
engine = engine.to_s
engine = engine == "rdiscount" ? "RDiscount" : engine.camelize
markdown_engine_prefix.const_get("#{engine}Template")
end
end
end

View file

@ -10,10 +10,8 @@ module Middleman::Renderers::Sass
end
alias :included :registered
end
class SassPlusCSSFilenameTemplate < ::Sprockets::Sass::SassTemplate
self.default_mime_type = "text/css"
# Add exception messaging
def evaluate(context, locals, &block)
begin
@ -35,9 +33,7 @@ module Middleman::Renderers::Sass
parts.pop
css_filename = File.join(location_of_sass_file, @context.css_dir, parts.join("."))
super.merge(
:css_filename => css_filename
)
super.merge(:css_filename => css_filename)
end
end
::Sprockets.register_engine ".sass", SassPlusCSSFilenameTemplate
@ -45,8 +41,6 @@ module Middleman::Renderers::Sass
::Tilt.prefer(SassPlusCSSFilenameTemplate)
class ScssPlusCSSFilenameTemplate < SassPlusCSSFilenameTemplate
self.default_mime_type = "text/css"
# Define the expected syntax for the template
def syntax
:scss

View file

@ -1,3 +1,3 @@
module Middleman
VERSION = "3.0.0.alpha.2"
VERSION = "3.0.0.alpha.3"
end