jekyll-assets 1.0.0 → 2.0.0.pre.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +10 -10
  3. data/LICENSE +16 -18
  4. data/README.md +94 -554
  5. data/Rakefile +2 -8
  6. data/lib/jekyll/assets.rb +18 -12
  7. data/lib/jekyll/assets/cached.rb +12 -0
  8. data/lib/jekyll/assets/configuration.rb +43 -87
  9. data/lib/jekyll/assets/context.rb +23 -0
  10. data/lib/jekyll/assets/env.rb +181 -0
  11. data/lib/jekyll/assets/extras/es6.rb +5 -0
  12. data/lib/jekyll/assets/extras/font-awesome.rb +1 -0
  13. data/lib/jekyll/assets/extras/helpers.rb +6 -0
  14. data/lib/jekyll/assets/extras/prefix.rb +5 -0
  15. data/lib/jekyll/assets/filters.rb +9 -11
  16. data/lib/jekyll/assets/helpers.rb +37 -0
  17. data/lib/jekyll/assets/hook.rb +45 -0
  18. data/lib/jekyll/assets/hooks/post_read.rb +3 -0
  19. data/lib/jekyll/assets/hooks/post_write.rb +3 -0
  20. data/lib/jekyll/assets/logger.rb +25 -0
  21. data/lib/jekyll/assets/patches/jekyll/cleaner.rb +11 -0
  22. data/lib/jekyll/assets/patches/jekyll/site.rb +5 -0
  23. data/lib/jekyll/assets/patches/sprockets/asset.rb +13 -0
  24. data/lib/jekyll/assets/tag.rb +136 -9
  25. data/lib/jekyll/assets/tag/parser.rb +129 -0
  26. data/lib/jekyll/assets/tag/proxied_asset.rb +84 -0
  27. data/lib/jekyll/assets/tag/proxies.rb +86 -0
  28. data/lib/jekyll/assets/tag/proxies/magick.rb +101 -0
  29. data/lib/jekyll/assets/version.rb +1 -1
  30. metadata +59 -149
  31. data/.gitignore +0 -27
  32. data/.rspec +0 -1
  33. data/.rubocop.yml +0 -43
  34. data/.rubocop_todo.yml +0 -10
  35. data/.travis.yml +0 -16
  36. data/.yardopts +0 -1
  37. data/Appraisals +0 -7
  38. data/Guardfile +0 -7
  39. data/HISTORY.md +0 -297
  40. data/gemfiles/jekyll_2.gemfile +0 -17
  41. data/gemfiles/jekyll_3.gemfile +0 -17
  42. data/jekyll-assets.gemspec +0 -34
  43. data/lib/jekyll-assets.rb +0 -2
  44. data/lib/jekyll/assets/asset_path.rb +0 -39
  45. data/lib/jekyll/assets/environment.rb +0 -62
  46. data/lib/jekyll/assets/patches.rb +0 -1
  47. data/lib/jekyll/assets/patches/asset_patch.rb +0 -102
  48. data/lib/jekyll/assets/patches/bundled_asset_patch.rb +0 -16
  49. data/lib/jekyll/assets/patches/context_patch.rb +0 -31
  50. data/lib/jekyll/assets/patches/index_patch.rb +0 -25
  51. data/lib/jekyll/assets/patches/processed_asset_patch.rb +0 -59
  52. data/lib/jekyll/assets/patches/site_patch.rb +0 -62
  53. data/lib/jekyll/assets/renderer.rb +0 -122
  54. data/spec/fixtures/.gitignore +0 -2
  55. data/spec/fixtures/.jekyll-metadata +0 -0
  56. data/spec/fixtures/_assets/alert.js +0 -1
  57. data/spec/fixtures/_assets/app.css.erb +0 -5
  58. data/spec/fixtures/_assets/app.js +0 -1
  59. data/spec/fixtures/_assets/app.min.css +0 -0
  60. data/spec/fixtures/_assets/app.min.js +0 -0
  61. data/spec/fixtures/_assets/fonts/vapor.eot +0 -0
  62. data/spec/fixtures/_assets/fonts/vapor.svg +0 -0
  63. data/spec/fixtures/_assets/fonts/vapor.ttf +0 -0
  64. data/spec/fixtures/_assets/fonts/vapor.woff +0 -0
  65. data/spec/fixtures/_assets/lib/relative.css.scss +0 -12
  66. data/spec/fixtures/_assets/noise.png +0 -0
  67. data/spec/fixtures/_assets/noize.png +0 -0
  68. data/spec/fixtures/_assets/should_be_blank.css.erb +0 -1
  69. data/spec/fixtures/_assets/should_fail.css.erb +0 -1
  70. data/spec/fixtures/_assets/vapor.css.scss +0 -13
  71. data/spec/fixtures/_assets/vapor.js +0 -2
  72. data/spec/fixtures/_assets/vendor/with_bootstrap.css.sass +0 -1
  73. data/spec/fixtures/_assets/vendor/with_bourbon.css.sass +0 -4
  74. data/spec/fixtures/_assets/vendor/with_compass.css.sass +0 -4
  75. data/spec/fixtures/_assets/vendor/with_neat.css.sass +0 -5
  76. data/spec/fixtures/_assets/wowscript.js +0 -0
  77. data/spec/fixtures/_assets/wowstyle.css +0 -0
  78. data/spec/fixtures/_config.yml +0 -2
  79. data/spec/fixtures/_layouts/default.html +0 -9
  80. data/spec/fixtures/_posts/2012-10-19-hello-world.md +0 -6
  81. data/spec/fixtures/_posts/2015-02-02-duplicates.md +0 -8
  82. data/spec/fixtures/index.html +0 -0
  83. data/spec/lib/jekyll/assets/configuration_spec.rb +0 -172
  84. data/spec/lib/jekyll/assets/environment_spec.rb +0 -18
  85. data/spec/lib/jekyll/assets/filters_spec.rb +0 -112
  86. data/spec/lib/jekyll/assets/patches/site_patch_spec.rb +0 -176
  87. data/spec/lib/jekyll/assets/renderer_spec.rb +0 -286
  88. data/spec/lib/jekyll/assets/tag_spec.rb +0 -139
  89. data/spec/spec_helper.rb +0 -58
  90. data/spec/support/fixtures_helpers.rb +0 -7
data/Rakefile CHANGED
@@ -1,9 +1,3 @@
1
- require "bundler/gem_tasks"
2
-
3
1
  require "rspec/core/rake_task"
4
- RSpec::Core::RakeTask.new
5
-
6
- require "rubocop/rake_task"
7
- RuboCop::RakeTask.new
8
-
9
- task :default => [:spec, :rubocop]
2
+ RSpec::Core::RakeTask.new(:spec)
3
+ task :default => [:spec]
data/lib/jekyll/assets.rb CHANGED
@@ -1,14 +1,20 @@
1
- require "jekyll/assets/patches"
2
- require "jekyll/assets/filters"
3
- require "jekyll/assets/tag"
4
- require "jekyll/assets/version"
1
+ require "sprockets"
2
+ require "sprockets/helpers"
3
+ require "jekyll"
5
4
 
6
- module Jekyll
7
- module Assets
8
- HOOKS = []
5
+ require_relative "assets/hook"
6
+ require_relative "assets/helpers"
7
+ require_relative "assets/extras/es6"
8
+ require_relative "assets/extras/font-awesome"
9
+ require_relative "assets/extras/helpers"
10
+ require_relative "assets/extras/prefix"
9
11
 
10
- def self.configure(&blk)
11
- HOOKS << blk
12
- end
13
- end
14
- end
12
+ require_relative "assets/env"
13
+ require_relative "assets/patches/jekyll/cleaner"
14
+ require_relative "assets/patches/sprockets/asset"
15
+ require_relative "assets/patches/jekyll/site"
16
+
17
+ require_relative "assets/hooks/post_read"
18
+ require_relative "assets/hooks/post_write"
19
+ require_relative "assets/filters"
20
+ require_relative "assets/tag"
@@ -0,0 +1,12 @@
1
+ module Jekyll
2
+ module Assets
3
+ class Cached < Sprockets::CachedEnvironment
4
+ attr_reader :jekyll, :parent
5
+ def initialize(env)
6
+ @parent = env
7
+ @jekyll = env.jekyll
8
+ super env
9
+ end
10
+ end
11
+ end
12
+ end
@@ -1,94 +1,50 @@
1
- # stdlib
2
- require "ostruct"
3
-
4
1
  module Jekyll
5
2
  module Assets
6
- class Configuration
7
- DEFAULTS = {
8
- :dirname => "assets",
9
- :sources => %w(_assets/javascripts
10
- _assets/stylesheets
11
- _assets/images),
12
- :js_compressor => nil,
13
- :css_compressor => nil,
14
- :autosize => false,
15
- :cachebust => :hard,
16
- :cache => false,
17
- :gzip => %w(text/css application/javascript),
18
- :debug => false,
19
- :version => 1
20
- }.freeze
21
-
22
- # rubocop:disable Metrics/AbcSize
23
- def initialize(site, config = {})
24
- @site = site
25
- @data = OpenStruct.new DEFAULTS.merge(config)
26
-
27
- @data.sources = [@data.sources] if @data.sources.is_a? String
28
- @data.dirname = @data.dirname.gsub(%r{^/+|/+$}, "")
29
-
30
- compress = OpenStruct.new @data.compress
31
-
32
- @data.js_compressor ||= compress.js
33
- @data.css_compressor ||= compress.css
34
- @data.cache ||= @data.cache_assets
35
- end
36
- # rubocop:enable Metrics/AbcSize
37
-
38
- def baseurl
39
- unless @data.baseurl
40
- baseurl = "/" << [@site.config["baseurl"], @data.dirname].join("/")
41
-
42
- # if baseurl not given - autoguess base on dirname
43
- @data.baseurl = baseurl.squeeze("/")
3
+ module Configuration
4
+ DEVELOPMENT = {
5
+ "skip_prefix_with_cdn" => false,
6
+ "prefix" => "/assets",
7
+ "digest" => false,
8
+ "assets" => [],
9
+
10
+ "compress" => {
11
+ "css" => false,
12
+ "js" => false
13
+ },
14
+
15
+ "sources" => [
16
+ "_assets/css", "_assets/stylesheets",
17
+ "_assets/images", "_assets/img", "_assets/fonts",
18
+ "_assets/javascripts", "_assets/js"
19
+ ]
20
+ }
21
+
22
+ PRODUCTION = DEVELOPMENT.merge({
23
+ "digest" => true,
24
+ "compress" => {
25
+ "css" => true,
26
+ "js" => true
27
+ },
28
+ })
29
+
30
+ def self.defaults
31
+ %W(development test).include?(Jekyll.env) ? \
32
+ DEVELOPMENT : PRODUCTION
33
+ end
34
+
35
+ def self.merge(merge_into, config = self.defaults)
36
+ merge_into = merge_into.dup
37
+ config.each_with_object(merge_into) do |(k, v), h|
38
+ if !h.has_key?(k) || (v.is_a?(Hash) && !h[k])
39
+ h[k] = v
40
+
41
+ elsif v.is_a?(Hash)
42
+ h[k] = merge h[k], \
43
+ v
44
+ end
44
45
  end
45
46
 
46
- @data.baseurl.chomp "/"
47
- end
48
-
49
- def js_compressor
50
- compressor @data.js_compressor
51
- end
52
-
53
- def css_compressor
54
- compressor @data.css_compressor
55
- end
56
-
57
- def cachebust
58
- return :none if none?(@data.cachebust)
59
- return @data.cachebust.to_sym if @data.cachebust.to_s[/^(soft|hard)$/]
60
- fail "Unknown cachebust strategy: #{@data.cachebust}"
61
- end
62
-
63
- def cache_assets?
64
- @data.cache ? true : false
65
- end
66
-
67
- def cache_path
68
- @data.cache.is_a?(String) ? @data.cache : ".jekyll-assets-cache"
69
- end
70
-
71
- def gzip
72
- return @data.gzip if @data.gzip.is_a? Array
73
- @data.gzip ? DEFAULTS[:gzip] : []
74
- end
75
-
76
- def version
77
- @data.version
78
- end
79
-
80
- def method_missing(name, *args, &block)
81
- @data.send name, *args, &block
82
- end
83
-
84
- private
85
-
86
- def none?(val)
87
- val.to_s.empty? || "none" == val.to_s.downcase
88
- end
89
-
90
- def compressor(val)
91
- none?(val) ? nil : val.to_sym
47
+ merge_into
92
48
  end
93
49
  end
94
50
  end
@@ -0,0 +1,23 @@
1
+ module Jekyll
2
+ module Assets
3
+ class Context
4
+ def initialize(context)
5
+ patch context
6
+ end
7
+
8
+ def patch(what)
9
+ what.class_eval do
10
+ alias_method :_old_asset_path, :asset_path
11
+ def asset_path(asset, opts = {})
12
+ out = _old_asset_path asset
13
+ return unless out
14
+
15
+ environment.parent.used.add(environment.find_asset \
16
+ resolve(asset))
17
+ out
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,181 @@
1
+ require_relative "logger"
2
+ require_relative "configuration"
3
+ require_relative "context"
4
+ require_relative "cached"
5
+
6
+ module Jekyll
7
+ module Assets
8
+ class Env < Sprockets::Environment
9
+ attr_reader :jekyll, :used
10
+ class << self
11
+ attr_accessor :digest_cache, :assets_cache
12
+ def digest_cache
13
+ @_digest_cache ||= \
14
+ {}
15
+ end
16
+ end
17
+
18
+ def initialize(path, jekyll = nil)
19
+ jekyll, path = path, nil if path.is_a?(Jekyll::Site)
20
+ @used, @jekyll = Set.new, jekyll
21
+ path ? super(path) : super()
22
+ disable_erb
23
+
24
+ jekyll.config["assets"] = Configuration.merge(asset_config)
25
+ private_methods(false).select { |v| v =~ %r!\Asetup_! }.map { |v| send(v) }
26
+ Hook.trigger :env, :post_init, self
27
+ jekyll.sprockets = self
28
+ end
29
+
30
+ def in_cache_dir(*paths)
31
+ cache_dir = asset_config.fetch("cache", ".asset-cache") || nil
32
+ jekyll.in_source_dir(
33
+ cache_dir, *paths
34
+ )
35
+ end
36
+
37
+ def cached_write?
38
+ !@used.any?
39
+ end
40
+
41
+ def all_assets(cached = false)
42
+ if !cached
43
+ then Set.new(@used).merge extra_assets
44
+ else Set.new(self.class.assets_cache).merge extra_assets
45
+ end
46
+ end
47
+
48
+ def extra_assets
49
+ each_logical_path(*asset_config.fetch("assets", [])).map do |v|
50
+ find_asset v
51
+ end
52
+ end
53
+
54
+ def cdn?() !dev? && !!asset_config["cdn"] end
55
+ def baseurl() jekyll.config["baseurl"] || "" end
56
+ def dev?() %W(development test).include? Jekyll.env end
57
+ def compress?(what) !!asset_config["compress"][what] end
58
+ def asset_config() jekyll.config["assets"] ||= {} end
59
+ def digest?() !!asset_config["digest"] end
60
+ def prefix() asset_config["prefix"] end
61
+
62
+ def prefix_path(path = "")
63
+ prefix = cdn? && asset_config["skip_prefix_with_cdn"] ? "" : self.prefix
64
+ path = [baseurl, prefix, path]
65
+ if cdn? && (cdn = asset_config["cdn"])
66
+ File.join(cdn, *path).chomp(
67
+ "/"
68
+ )
69
+ else
70
+ File.join(*path).chomp(
71
+ "/"
72
+ )
73
+ end
74
+ end
75
+
76
+ def cached
77
+ Cached.new(self)
78
+ end
79
+
80
+ def write_all
81
+ if cached_write?
82
+ then write_cached_assets else write_assets
83
+ end
84
+ end
85
+
86
+ private
87
+ def write_assets(assets = self.all_assets)
88
+ self.class.assets_cache = assets
89
+ self.class.digest_cache = Hash[assets.map do |a|
90
+ [a.logical_path, a.digest]
91
+ end]
92
+
93
+ assets.each do |v|
94
+ v.write_to as_path v
95
+ end
96
+ end
97
+
98
+ private
99
+ def write_cached_assets(assets = all_assets(true))
100
+ assets.each do |a|
101
+ if !a.is_a?(Tag::ProxiedAsset)
102
+ viejo = self.class.digest_cache[a.logical_path]
103
+ nuevo = find_asset(a.logical_path).digest
104
+ path = as_path a
105
+
106
+ if nuevo == viejo && File.file?(path)
107
+ next
108
+ end
109
+ else
110
+ if File.file?(a.logical_path)
111
+ next
112
+ end
113
+ end
114
+
115
+ self.class.digest_cache[a.logical_path] = a.digest
116
+ a.write_to as_path a
117
+ end
118
+ end
119
+
120
+ private
121
+ def as_path(v)
122
+ path = digest?? v.digest_path : v.logical_path
123
+ jekyll.in_dest_dir(File.join(prefix, path))
124
+ end
125
+
126
+ def disable_erb
127
+ self.config = hash_reassoc(config, :engines) do |h|
128
+ h.delete(".erb")
129
+ h
130
+ end
131
+ end
132
+
133
+ private
134
+ def setup_css_compressor
135
+ if compress?("css")
136
+ self.css_compressor = :sass
137
+ end
138
+ end
139
+
140
+ private
141
+ def setup_js_compressor
142
+ if compress?("js")
143
+ Helpers.try_require "uglifier" do
144
+ self.js_compressor = :uglify
145
+ end
146
+ end
147
+ end
148
+
149
+ private
150
+ def setup_context
151
+ Context.new(context_class)
152
+ end
153
+
154
+ private
155
+ def setup_version
156
+ self.version = Digest::MD5.hexdigest \
157
+ jekyll.config.fetch("assets", {}).to_s
158
+ end
159
+
160
+ private
161
+ def setup_sources
162
+ asset_config["sources"].each do |v|
163
+ append_path jekyll.in_source_dir(v)
164
+ end
165
+ end
166
+
167
+ private
168
+ def setup_logger
169
+ self.logger = Logger.new
170
+ end
171
+
172
+ private
173
+ def setup_cache
174
+ if cache_dir = asset_config.fetch("cache", ".asset-cache")
175
+ self.cache = Sprockets::Cache::FileStore.new \
176
+ jekyll.in_source_dir(cache_dir)
177
+ end
178
+ end
179
+ end
180
+ end
181
+ end
@@ -0,0 +1,5 @@
1
+ Jekyll::Assets::Helpers.try_require_if_javascript? "sprockets/es6" do
2
+ if ExecJS.runtime.is_a?(ExecJS::RubyRhinoRuntime)
3
+ Jekyll.logger.warn "ES6 transpiler has trouble with RubyRhino, use Node"
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ Jekyll::Assets::Helpers.try_require "font-awesome-sass"
@@ -0,0 +1,6 @@
1
+ Jekyll::Assets::Hook.register :env, :post_init do |e|
2
+ Sprockets::Helpers.configure do |c|
3
+ c.prefix = e.prefix_path
4
+ c.digest = e.digest?
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ Jekyll::Assets::Helpers.try_require_if_javascript? "autoprefixer-rails" do
2
+ Jekyll::Assets::Hook.register :env, :post_init do |e|
3
+ AutoprefixerRails.install(e)
4
+ end
5
+ end
@@ -1,19 +1,17 @@
1
- # internal
2
- require "jekyll/assets/renderer"
3
-
4
1
  module Jekyll
5
2
  module Assets
6
3
  module Filters
7
- %w(asset asset_path image javascript stylesheet).each do |name|
8
- module_eval <<-RUBY, __FILE__, __LINE__
9
- def #{name} path # def stylesheet logical_path
10
- r = Renderer.new @context, path # r = Renderer.new @context, path
11
- r.render_#{name} # r.render_stylesheet
12
- end # end
13
- RUBY
4
+ %W(js css img image javascript stylesheet style asset_path).each do |v|
5
+ define_method v do |path, args = ""|
6
+ Tag.send(:new, v, "#{path} #{args}", "").render(
7
+ @context
8
+ )
9
+ end
14
10
  end
15
11
  end
16
12
  end
17
13
  end
18
14
 
19
- Liquid::Template.register_filter Jekyll::Assets::Filters
15
+ Liquid::Template.register_filter(
16
+ Jekyll::Assets::Filters
17
+ )