lib/octopress-ink/plugins.rb in octopress-ink-1.0.0.alpha.33 vs lib/octopress-ink/plugins.rb in octopress-ink-1.0.0.alpha.34

- old
+ new

@@ -1,11 +1,12 @@ +require 'octopress' + module Octopress module Ink module Plugins - @plugins = [] - @local_plugins = [] + @user_plugins = [] @site = nil def self.theme @theme end @@ -29,30 +30,62 @@ found.first end end def self.plugins - [@theme].concat(@plugins).concat(@local_plugins).compact + [@theme].concat(@plugins).concat(@user_plugins).compact end - def self.site=(site) - @site = site + def self.register(site) + unless @site + @site = site + plugins.each do |p| + p.register + end + end end + def self.add_files + add_assets(%w{images pages files fonts docs}) + plugin('asset-pipeline').register_assets + add_stylesheets + add_javascripts + end + + def self.add_assets(assets) + plugins.each do |p| + p.add_asset_files(assets) + end + end + def self.site @site end + def self.register_plugin(plugin, name, type='plugin', local=nil) + new_plugin = plugin.new(name, type) + + case type + when 'theme' + @theme = new_plugin + else + if local + @user_plugins << new_plugin + else + @plugins << new_plugin + end + end + end + def self.config if @config @config else @config = {} @config['plugins'] = {} @config['theme'] = @theme.nil? ? {} : @theme.config - plugins.each do |p| unless p == @theme @config['plugins'][p.name] = p.config end end @@ -64,39 +97,12 @@ def self.include(name, file) p = plugin(name) p.include(file) end - def self.register_plugin(plugin, name, type='plugin') - new_plugin = plugin.new(name, type) - - case type - when 'theme' - @theme = new_plugin - when 'local_plugin' - @local_plugins << new_plugin - else - @plugins << new_plugin - end - end - - def self.register_layouts - plugins.each do |p| - p.layouts.clone.each { |layout| layout.register } - end - end - - def self.layouts - names = [] - plugins.each do |p| - p.layouts.each { |layout| names << layout.name } - end - names - end - def self.custom_dir - @site.config['plugins'] + site.config['plugins'] end def self.fingerprint(paths) paths = [paths] unless paths.is_a? Array Digest::MD5.hexdigest(paths.clone.map! { |path| "#{File.mtime(path).to_i}" }.join) @@ -105,12 +111,17 @@ def self.combined_stylesheet_path(media) File.join('stylesheets', "#{media}-#{@combined_stylesheets[media][:fingerprint]}.css") end def self.combined_javascript_path - print = @javascript_fingerprint || '' - File.join('javascripts', "#{print}.js") + print = '' + + if @js_fingerprint + print = "-" + @js_fingerprint + end + + File.join('javascripts', "all#{print}.js") end def self.write_files(source, dest) @site.static_files << StaticFileContent.new(source, dest) end @@ -121,33 +132,28 @@ def self.compile_sass(contents, options) ::Sass.compile(contents, options) end - def self.sass_config(item, default) - config = @site.config - if config['octopress'] && config['octopress']['sass'] && config['octopress']['sass'][item] - config['octopress']['sass'][item] - else - default - end - end - def self.sass_options - options = { - style: sass_config('output_style', 'compressed').to_sym, - trace: sass_config('trace', false), - line_numbers: sass_config('line_numbers', false) + config = @site.config + + defaults = { + 'style' => :compressed, + 'trace' => false, + 'line_numbers' => false } + + options = defaults.deep_merge(config['sass'] || {}).symbolize_keys + options = options.each{ |k,v| options[k] = v.to_sym if v.is_a? String } + options end def self.write_combined_stylesheet css = combine_stylesheets css.keys.each do |media| - options = sass_options - options[:line_numbers] = false - contents = compile_sass(css[media][:contents], options) + contents = compile_sass(css[media][:contents], sass_options) write_files(contents, combined_stylesheet_path(media)) end end def self.write_combined_javascript @@ -163,11 +169,11 @@ if plugin.type == 'theme' plugin_header = "/* Theme: #{plugin.name} */\n" else plugin_header = "/* Plugin: #{plugin.name} */\n" end - stylesheets = plugin.stylesheets.clone.concat plugin.sass + stylesheets = plugin.stylesheets stylesheets.each do |file| css[file.media] ||= {} css[file.media][:contents] ||= '' css[file.media][:contents] << plugin_header css[file.media][:paths] ||= [] @@ -194,11 +200,11 @@ def self.combine_javascripts unless @combined_javascripts js = '' plugins.each do |plugin| paths = plugin.javascript_paths - @javascript_fingerprint = fingerprint(paths) + @js_fingerprint = fingerprint(paths) paths.each do |file| js.concat Pathname.new(file).read end end @combined_javascripts = js @@ -219,11 +225,11 @@ "<script src='#{Filters.expand_url(combined_javascript_path)}'></script>" end end def self.stylesheet_tags - if concat_css + if Ink.config['concat_css'] combined_stylesheet_tag else css = [] plugins.each do |plugin| css.concat plugin.stylesheet_tags @@ -231,89 +237,44 @@ end css end end - def self.concat_css - config = @site.config - if config['octopress'] && !config['octopress']['concat_css'].nil? - config['octopress']['concat_css'] != false - else - true - end - end - - def self.concat_js - config = @site.config - if config['octopress'] && !config['octopress']['concat_js'].nil? - config['octopress']['concat_js'] != false - else - true - end - end - def self.javascript_tags - if concat_js + if Ink.config['concat_js'] combined_javascript_tag else js = [] plugins.each do |plugin| js.concat plugin.javascript_tags end js end end - def self.copy_javascripts - plugins.each do |plugin| - copy plugin.javascripts - end - end - - def self.copy_stylesheets - stylesheets = plugins.clone.map { - |p| p.stylesheets.clone.concat(p.sass) - }.flatten - copy stylesheets - end - - def self.add_static_files + # Copy/Generate Stylesheets + # + def self.add_stylesheets - plugin('user stylesheets').add_files - - # Copy/Generate Stylesheets - # - if concat_css + if Ink.config['concat_css'] write_combined_stylesheet else - copy_stylesheets + add_assets(%w{css sass}) end + end - # Copy/Generate Javascripts - # - if concat_js + # Copy/Generate Javascripts + # + def self.add_javascripts + + if Ink.config['concat_js'] write_combined_javascript else - copy_javascripts + add_assets(['javascripts']) end - # Copy other assets - # - copy_static_files end - def self.copy_static_files - plugins.each do |plugin| - copy plugin.files - copy plugin.pages - copy plugin.images - copy plugin.fonts - end - end - - def self.copy(files) - files.each { |f| f.copy } - end end end end