Module: Octopress::Ink::PluginAssetPipeline

Extended by:
PluginAssetPipeline
Included in:
PluginAssetPipeline
Defined in:
lib/octopress-ink/plugin_asset_pipeline.rb

Instance Method Summary collapse

Instance Method Details

#async_stylesheet_tagObject



76
77
78
79
# File 'lib/octopress-ink/plugin_asset_pipeline.rb', line 76

def async_stylesheet_tag
  js = uglify(File.read(Ink.gem_dir('assets','js','loadCSS.js')))
  %Q{<script>#{js}\n#{load_css}\n</script>\n<noscript>#{combined_stylesheet_tag}</noscript>}
end

#combine_stylesheetsObject

Combine stylesheets from each plugin

Returns a hash of stylesheets grouped by media types

output: { 'screen' => 'body { background... }' }


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/octopress-ink/plugin_asset_pipeline.rb', line 121

def combine_stylesheets
  @combined_stylesheets ||= begin
    combined = {}


    stylesheets.clone.each do |media,files|
      files.each do |file|
        header = "/* #{file.plugin.type.capitalize}: #{file.plugin.name} */"
        combined[media] ||= ''
        combined[media] << "#{header}\n" unless combined[media] =~ /#{file.plugin.name}/
        combined[media] << file.content
      end
    end

    combined
  end
end

#combined_javascript_pathObject



185
186
187
# File 'lib/octopress-ink/plugin_asset_pipeline.rb', line 185

def combined_javascript_path
  File.join(@js_dir, "all-#{javascript_fingerprint}.js")
end

#combined_javascript_urlObject



111
112
113
# File 'lib/octopress-ink/plugin_asset_pipeline.rb', line 111

def combined_javascript_url
  Filters.expand_url(combined_javascript_path)
end

#combined_stylesheet_path(media) ⇒ Object



147
148
149
# File 'lib/octopress-ink/plugin_asset_pipeline.rb', line 147

def combined_stylesheet_path(media)
  File.join(@css_dir, "#{media}-#{stylesheet_fingerprint(media)}.css")
end

#combined_stylesheet_tagObject



68
69
70
71
72
73
74
# File 'lib/octopress-ink/plugin_asset_pipeline.rb', line 68

def combined_stylesheet_tag
  tags = ''
  combined_stylesheet_urls.each do |media, url|
    tags.concat "<link href='#{url}' media='#{media}' rel='stylesheet' type='text/css'>"
  end
  tags
end

#combined_stylesheet_url(media) ⇒ Object



97
98
99
# File 'lib/octopress-ink/plugin_asset_pipeline.rb', line 97

def combined_stylesheet_url(media)
  Filters.expand_url(combined_stylesheet_path(media))
end

#combined_stylesheet_urlsObject



89
90
91
92
93
94
95
# File 'lib/octopress-ink/plugin_asset_pipeline.rb', line 89

def combined_stylesheet_urls
  urls = {}
  combine_stylesheets.keys.each do |media|
    urls[media] = combined_stylesheet_url(media)
  end
  urls
end

#compile_css(content) ⇒ Object

Compile CSS to take advantage of Sass’s compression settings



22
23
24
25
26
27
28
# File 'lib/octopress-ink/plugin_asset_pipeline.rb', line 22

def compile_css(content)
  configs = sass_converter.sass_configs
  configs[:syntax] = :scss
  configs[:style] ||= :compressed if Ink.configuration['asset_pipeline']['compress_css']

  Sass.compile(content, configs)
end

#compile_sass(sass) ⇒ Object



30
31
32
# File 'lib/octopress-ink/plugin_asset_pipeline.rb', line 30

def compile_sass(sass)
  Sass.compile(sass.render, sass_configs(sass))
end

#compress_js?(file) ⇒ Boolean

Returns:

  • (Boolean)


220
221
222
# File 'lib/octopress-ink/plugin_asset_pipeline.rb', line 220

def compress_js?(file)
  Ink.configuration['asset_pipeline']['compress_js'] && !file.path.end_with?('.min.js')
end

#fingerprint(paths) ⇒ Object



228
229
230
231
232
# File 'lib/octopress-ink/plugin_asset_pipeline.rb', line 228

def fingerprint(paths)
  return '' if ENV['JEKYLL_ENV'] == 'test'
  paths = [paths] unless paths.is_a? Array
  Digest::MD5.hexdigest(paths.clone.map! { |path| "#{File.mtime(path).to_i}" }.join)
end

#javascript_fingerprintObject



180
181
182
183
# File 'lib/octopress-ink/plugin_asset_pipeline.rb', line 180

def javascript_fingerprint
  @javascript_fingerprint ||=
    fingerprint(javascripts.clone.map(&:path))
end

#javascript_tagObject



101
102
103
104
105
106
107
108
109
# File 'lib/octopress-ink/plugin_asset_pipeline.rb', line 101

def javascript_tag
  unless @combined_js == ''
    if Ink.configuration['asset_pipeline']['async_js']
      "<script async src='#{combined_javascript_url}'></script>"
    else
      "<script src='#{combined_javascript_url}'></script>"
    end
  end
end

#javascriptsObject



175
176
177
178
# File 'lib/octopress-ink/plugin_asset_pipeline.rb', line 175

def javascripts
  @javascripts ||=
    Plugins.plugins.clone.map(&:javascripts).flatten
end

#load_cssObject



81
82
83
84
85
86
87
# File 'lib/octopress-ink/plugin_asset_pipeline.rb', line 81

def load_css
  script = ''
  combined_stylesheet_urls.each do |media, url|
    script << %Q{loadCSS("#{url}", null, "#{media}")\n}
  end
  script
end

#resetObject



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/octopress-ink/plugin_asset_pipeline.rb', line 6

def reset
  @sass_converter = nil
  @combined_stylesheets = nil
  @stylesheets = nil
  @javascripts = nil
  @uglify_settings = nil
  @combined_js = ''
  @stylesheet_fingerprint = {}
  @javascript_fingerprint = nil
  @uglify_settings ||= Jekyll::Utils.symbolize_hash_keys(Ink.configuration['asset_pipeline']['uglifier'])
  @css_dir = Ink.configuration['asset_pipeline']['stylesheets_destination']
  @js_dir  = Ink.configuration['asset_pipeline']['javascripts_destination']
end

#sass_configs(sass) ⇒ Object

Gets default Sass configuration hash from Jekyll



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/octopress-ink/plugin_asset_pipeline.rb', line 36

def sass_configs(sass)
  configs = sass_converter.sass_configs

  configs[:syntax] = sass.ext.sub(/^\./,'').to_sym

  if sass.respond_to? :load_paths
    configs[:load_paths] = sass.load_paths
  end

  configs
end

#sass_converterObject

Access Jekyll’s built in Sass converter



50
51
52
53
54
55
56
# File 'lib/octopress-ink/plugin_asset_pipeline.rb', line 50

def sass_converter
  @sass_converter ||= begin
    Octopress.site.converters.find do |c|
      c.kind_of?(Jekyll::Converters::Sass) 
    end
  end
end

#stylesheet_fingerprint(media) ⇒ Object



151
152
153
154
# File 'lib/octopress-ink/plugin_asset_pipeline.rb', line 151

def stylesheet_fingerprint(media)
  @stylesheet_fingerprint[media] ||=
    fingerprint(stylesheets[media].clone.map(&:path))
end

#stylesheet_tagObject

Return a link tag, for all plugins’ stylesheets



60
61
62
63
64
65
66
# File 'lib/octopress-ink/plugin_asset_pipeline.rb', line 60

def stylesheet_tag
  if Ink.configuration['asset_pipeline']['async_css']
    async_stylesheet_tag
  else
    combined_stylesheet_tag
  end
end

#stylesheetsObject

Get all plugins stylesheets

Returns a hash of assets grouped by media

output: { 'screen' => [Octopress::Ink::Assets::Stylesheet, ..]


162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/octopress-ink/plugin_asset_pipeline.rb', line 162

def stylesheets
 @stylesheets ||= begin
    files = {}
    Plugins.plugins.clone.each do |plugin| 
      plugin.stylesheets.each do |file|
        files[file.media] ||= []
        files[file.media] << file
      end
    end
    files
  end
end

#uglify(js) ⇒ Object



216
217
218
# File 'lib/octopress-ink/plugin_asset_pipeline.rb', line 216

def uglify(js)
  Uglifier.new(@uglify_settings).compile(js)
end

#write_combined_javascriptObject



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/octopress-ink/plugin_asset_pipeline.rb', line 189

def write_combined_javascript
  if Ink.configuration['asset_pipeline']['combine_js']
    javascripts.each do |file|

      js = if compress_js?(file)
        if cached = Cache.read_cache(file, @uglify_settings)
          cached
        else
          js = uglify(file.content)
          Cache.write_to_cache(file, js, @uglify_settings)
        end
      else
        file.content
      end

      unless @combined_js =~ /#{file.plugin.name}/
        @combined_js << "/* #{file.plugin.type.capitalize}: #{file.plugin.name} */\n" 
      end
      @combined_js << js
    end

    unless @combined_js == ''
      write_files(@combined_js, combined_javascript_path)
    end
  end
end

#write_combined_stylesheetObject



139
140
141
142
143
144
145
# File 'lib/octopress-ink/plugin_asset_pipeline.rb', line 139

def write_combined_stylesheet
  css = combine_stylesheets
  css.keys.each do |media|
    contents = compile_css(css[media])
    write_files(contents, combined_stylesheet_path(media)) 
  end
end

#write_files(source, dest) ⇒ Object



224
225
226
# File 'lib/octopress-ink/plugin_asset_pipeline.rb', line 224

def write_files(source, dest)
  Plugins.static_files << StaticFileContent.new(source, dest)
end