jekyll-assets 2.4.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (92) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +21 -17
  3. data/LICENSE +1 -1
  4. data/README.md +365 -297
  5. data/Rakefile +13 -2
  6. data/lib/jekyll/assets.rb +15 -11
  7. data/lib/jekyll/assets/config.rb +91 -55
  8. data/lib/jekyll/assets/context.rb +32 -0
  9. data/lib/jekyll/assets/default.rb +98 -0
  10. data/lib/jekyll/assets/drop.rb +62 -0
  11. data/lib/jekyll/assets/env.rb +135 -278
  12. data/lib/jekyll/assets/extensible.rb +86 -0
  13. data/lib/jekyll/assets/filters.rb +22 -0
  14. data/lib/jekyll/assets/hook.rb +115 -48
  15. data/lib/jekyll/assets/html.rb +88 -0
  16. data/lib/jekyll/assets/logger.rb +17 -59
  17. data/lib/jekyll/assets/manifest.rb +15 -126
  18. data/lib/jekyll/assets/map.rb +57 -0
  19. data/lib/jekyll/assets/map/css.rb +43 -0
  20. data/lib/jekyll/assets/map/javascript.rb +43 -0
  21. data/lib/jekyll/assets/map/writer.rb +192 -0
  22. data/lib/jekyll/assets/patches/cached_env.rb +79 -0
  23. data/lib/jekyll/assets/patches/functions.rb +31 -0
  24. data/lib/jekyll/assets/patches/obsolete_files.rb +42 -0
  25. data/lib/jekyll/assets/patches/sprockets.rb +9 -0
  26. data/lib/jekyll/assets/patches/sprockets_data_uri.rb +12 -0
  27. data/lib/jekyll/assets/plugins.rb +9 -0
  28. data/lib/jekyll/assets/plugins/bootstrap.rb +5 -0
  29. data/lib/jekyll/assets/plugins/font-awesome.rb +8 -0
  30. data/lib/jekyll/assets/plugins/frontmatter.rb +28 -0
  31. data/lib/jekyll/assets/plugins/html/audio.rb +32 -0
  32. data/lib/jekyll/assets/plugins/html/css.rb +24 -0
  33. data/lib/jekyll/assets/plugins/html/defaults/audio.rb +69 -0
  34. data/lib/jekyll/assets/plugins/html/defaults/css.rb +48 -0
  35. data/lib/jekyll/assets/plugins/html/defaults/img.rb +54 -0
  36. data/lib/jekyll/assets/plugins/html/defaults/js.rb +48 -0
  37. data/lib/jekyll/assets/plugins/html/defaults/vid.rb +63 -0
  38. data/lib/jekyll/assets/plugins/html/img.rb +76 -0
  39. data/lib/jekyll/assets/plugins/html/js.rb +30 -0
  40. data/lib/jekyll/assets/plugins/html/pic.rb +125 -0
  41. data/lib/jekyll/assets/plugins/html/svg.rb +37 -0
  42. data/lib/jekyll/assets/plugins/html/vid.rb +26 -0
  43. data/lib/jekyll/assets/plugins/liquid.rb +57 -0
  44. data/lib/jekyll/assets/plugins/magick.rb +7 -0
  45. data/lib/jekyll/assets/plugins/optim.rb +7 -0
  46. data/lib/jekyll/assets/plugins/prefixer.rb +28 -0
  47. data/lib/jekyll/assets/plugins/proxy/magick.rb +131 -0
  48. data/lib/jekyll/assets/plugins/proxy/optim.rb +64 -0
  49. data/lib/jekyll/assets/plugins/searcher.rb +72 -0
  50. data/lib/jekyll/assets/proxy.rb +109 -0
  51. data/lib/jekyll/assets/tag.rb +176 -0
  52. data/lib/jekyll/assets/url.rb +23 -0
  53. data/lib/jekyll/assets/utils.rb +348 -0
  54. data/lib/jekyll/assets/version.rb +2 -2
  55. data/lib/jekyll/assets/writer.rb +36 -0
  56. metadata +102 -81
  57. data/lib/jekyll-assets.rb +0 -7
  58. data/lib/jekyll/assets/addons/autoprefixer.rb +0 -11
  59. data/lib/jekyll/assets/addons/bootstrap.rb +0 -9
  60. data/lib/jekyll/assets/addons/fontawesome.rb +0 -9
  61. data/lib/jekyll/assets/addons/javascript.rb +0 -11
  62. data/lib/jekyll/assets/cached.rb +0 -30
  63. data/lib/jekyll/assets/helpers.rb +0 -74
  64. data/lib/jekyll/assets/hooks/cache.rb +0 -21
  65. data/lib/jekyll/assets/hooks/compression.rb +0 -25
  66. data/lib/jekyll/assets/hooks/config.rb +0 -12
  67. data/lib/jekyll/assets/hooks/erb.rb +0 -15
  68. data/lib/jekyll/assets/hooks/jekyll/drops.rb +0 -9
  69. data/lib/jekyll/assets/hooks/jekyll/setup.rb +0 -11
  70. data/lib/jekyll/assets/hooks/jekyll/write.rb +0 -11
  71. data/lib/jekyll/assets/hooks/logger.rb +0 -11
  72. data/lib/jekyll/assets/hooks/sources.rb +0 -13
  73. data/lib/jekyll/assets/hooks/sprockets.rb +0 -9
  74. data/lib/jekyll/assets/hooks/version.rb +0 -11
  75. data/lib/jekyll/assets/liquid/context.rb +0 -19
  76. data/lib/jekyll/assets/liquid/drop.rb +0 -81
  77. data/lib/jekyll/assets/liquid/filters.rb +0 -49
  78. data/lib/jekyll/assets/liquid/tag.rb +0 -244
  79. data/lib/jekyll/assets/liquid/tag/defaults.rb +0 -27
  80. data/lib/jekyll/assets/liquid/tag/defaults/image.rb +0 -81
  81. data/lib/jekyll/assets/liquid/tag/defaults/sha.rb +0 -58
  82. data/lib/jekyll/assets/liquid/tag/parser.rb +0 -202
  83. data/lib/jekyll/assets/liquid/tag/proxied_asset.rb +0 -166
  84. data/lib/jekyll/assets/liquid/tag/proxies.rb +0 -130
  85. data/lib/jekyll/assets/patches/jekyll/cleaner.rb +0 -15
  86. data/lib/jekyll/assets/patches/jekyll/site.rb +0 -5
  87. data/lib/jekyll/assets/patches/kernel.rb +0 -29
  88. data/lib/jekyll/assets/patches/sprockets/asset.rb +0 -28
  89. data/lib/jekyll/assets/processors/less.rb +0 -70
  90. data/lib/jekyll/assets/processors/liquid.rb +0 -46
  91. data/lib/jekyll/assets/proxies/image_optim.rb +0 -80
  92. data/lib/jekyll/assets/proxies/magick.rb +0 -194
@@ -0,0 +1,86 @@
1
+ # Frozen-string-literal: true
2
+ # Copyright: 2012 - 2017 - MIT License
3
+ # Encoding: utf-8
4
+
5
+ module Jekyll
6
+ module Assets
7
+ class Extensible
8
+ attr_reader :ctx
9
+ attr_reader :args
10
+ attr_reader :jekyll
11
+ attr_reader :asset
12
+ attr_reader :env
13
+
14
+ # --
15
+ def initialize(asset:, args:, ctx:)
16
+ @args = args
17
+ @env = ctx.registers[:site].sprockets
18
+ @jekyll = ctx.registers[:site]
19
+ @jekyll = @env.jekyll
20
+ @asset = asset
21
+ @ctx = ctx
22
+ end
23
+
24
+ # --
25
+ # Allows us to keep track of inheritence.
26
+ # @note you should be using this when using extensible.
27
+ # @param [<Any>] kls the class.
28
+ # @return [Array<Class>]
29
+ # --
30
+ def self.inherited(kls = nil)
31
+ @inherited ||= []
32
+ return @inherited if kls.nil?
33
+ @inherited << kls
34
+ end
35
+
36
+ # --
37
+ def self.requirements
38
+ @requirements ||= {
39
+ args: [], types: [
40
+ #
41
+ ]
42
+ }
43
+ end
44
+
45
+ # --
46
+ # @note a type is a "content type"
47
+ # Allows you to use types to determine if this class fits.
48
+ # @param [String] type the content type.
49
+ # @param [Hash] args the arguments.
50
+ # @return [true, false]
51
+ # --
52
+ def self.for?(type:, args:)
53
+ for_type?(type) && for_args?(args)
54
+ end
55
+
56
+ # --
57
+ def self.for_args?(args)
58
+ return true if arg_keys.empty?
59
+ arg_keys.collect { |k| args.key?(k) }.none? do |k|
60
+ k == false
61
+ end
62
+ end
63
+
64
+ # --
65
+ def self.for_type?(type)
66
+ return true if content_types.empty?
67
+ content_types.any? do |k|
68
+ k.is_a?(Regexp) ? type =~ k : k.to_s == type.to_s
69
+ end
70
+ end
71
+
72
+ # --
73
+ # Creates `#arg_keys`, and `#content_types` so that you
74
+ # can limit your surface for an extensible plugin. For
75
+ # example if you only work for
76
+ # --
77
+ [%i(arg_keys args), %i(content_types types)].each do |(k, v)|
78
+ instance_eval <<-RUBY
79
+ def self.#{k}(*a)
80
+ requirements[:#{v}].concat(a)
81
+ end
82
+ RUBY
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,22 @@
1
+ # Frozen-string-literal: true
2
+ # Copyright: 2012 - 2017 - MIT License
3
+ # Encoding: utf-8
4
+
5
+ require_relative "context"
6
+ require_relative "tag"
7
+
8
+ module Jekyll
9
+ module Assets
10
+ module Filters
11
+ def asset(path, args = "")
12
+ args = "#{path} #{args}"
13
+ ctx = Liquid::ParseContext.new
14
+ tag = Tag.new("asset", args, ctx)
15
+ tag.render(@context)
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ filter = Jekyll::Assets::Filters
22
+ Liquid::Template.register_filter(filter)
@@ -1,77 +1,144 @@
1
- # ----------------------------------------------------------------------------
2
1
  # Frozen-string-literal: true
3
- # Copyright: 2012 - 2016 - MIT License
2
+ # Copyright: 2012 - 2017 - MIT License
4
3
  # Encoding: utf-8
5
- # ----------------------------------------------------------------------------
6
4
 
7
5
  module Jekyll
8
6
  module Assets
9
7
  class Hook
10
8
  class UnknownHookError < RuntimeError
11
- def initialize(base: nil, point: nil)
12
- super "Unknown hook #{base ? "base" : "point"} '#{base || point}'"
9
+ def initialize(point)
10
+ super "Unknown hook point `#{point}'"
13
11
  end
14
12
  end
15
13
 
16
- # ----------------------------------------------------------------------
14
+ # --
15
+ class << self
16
+ attr_reader :points
17
+ end
17
18
 
18
- HOOK_ALIASES = {
19
- :env => {
20
- :post_init => :init,
21
- :pre_init => :init
22
- }
23
- }
19
+ # --
20
+ @points = {
21
+ env: {
22
+ before_init: {
23
+ 1 => [],
24
+ 2 => [],
25
+ 3 => [],
26
+ },
27
+
28
+ after_init: {
29
+ 1 => [],
30
+ 2 => [],
31
+ 3 => [],
32
+ },
33
+ },
24
34
 
25
- # ----------------------------------------------------------------------
35
+ config: {
36
+ before_merge: {
37
+ 1 => [],
38
+ 2 => [],
39
+ 3 => [],
40
+ },
41
+ },
26
42
 
27
- HOOK_POINTS = {
28
- :env => [
29
- :init
30
- ]
43
+ asset: {
44
+ before_compile: {
45
+ 1 => [],
46
+ 2 => [],
47
+ 3 => [],
48
+ },
49
+ },
50
+
51
+ liquid: {
52
+ pre_render: {
53
+ 1 => [],
54
+ 2 => [],
55
+ 3 => [],
56
+ },
57
+ },
31
58
  }
32
59
 
33
- # ----------------------------------------------------------------------
60
+ # --
61
+ # Create a hook point to attach hooks to.
62
+ # @param [Array<String,Symbol>] point the parent and child.
63
+ # @note plugins can create their own points if wished.
64
+ # @return [Hash<Hash<Array>>]
65
+ # --
66
+ def self.add_point(*point)
67
+ raise ArgumentError, "only give 2 points" if point.count > 2
34
68
 
35
- def self.all
36
- @_all ||= {}
37
- end
69
+ @points[point[0]] ||= {}
70
+ @points[point[0]][point[1]] ||= {
71
+ #
72
+ }
38
73
 
39
- # ----------------------------------------------------------------------
40
- # Trigger a hook, giving an optional block where we pass you the,
41
- # proc we got and then you can do as you please (such as instance eval)
42
- # but if you do not give us one then we simply pass the args.
43
- # ----------------------------------------------------------------------
44
-
45
- def self.trigger(base, point_, *args, &block)
46
- raise ArgumentError, "Do not give args with a block" if !args.empty? && block_given?
47
- if all.key?(base) && all[base].key?(point_)
48
- Set.new.merge(point(base, point_, :early)).merge(point(base, point_)).map do |v|
49
- block_given?? block.call(v) : v.call(*args)
50
- end
74
+ 1.upto(3).each do |i|
75
+ @points[point[0]][point[1]][i] ||= [
76
+ #
77
+ ]
51
78
  end
79
+
80
+ @points
52
81
  end
53
82
 
54
- # ----------------------------------------------------------------------
83
+ # --
84
+ # @return [Array<Proc>]
85
+ # @param [Array<String,Symbol>] point the parent and child.
86
+ # @note this is really internal.
87
+ # Get a hook point.
88
+ # --
89
+ def self.get_point(*point)
90
+ check_point(*point)
55
91
 
56
- def self.point(base, point, when_ = :late)
57
- point = all[base][point] ||= {
58
- :early => Set.new,
59
- :late => Set.new
60
- }
92
+ @points[point[0]][point[1]]
93
+ .each_with_object([]) do |(_, v), a|
94
+ a.concat(v)
95
+ end
96
+ end
61
97
 
62
- point[when_]
98
+ # --
99
+ # Trigger a hook point.
100
+ # @note plugins can trigger their own hooks.
101
+ # @param [Array<String,Symbol>] point the parent and child.
102
+ # @param [Proc{}] block the code to run.
103
+ # @see self.add_point
104
+ # @return [nil]
105
+ # --
106
+ def self.trigger(*point, &block)
107
+ get_point(*point).map do |v|
108
+ block.call(v)
109
+ end
63
110
  end
64
111
 
65
- # ----------------------------------------------------------------------
112
+ # --
113
+ # Register a hook on a hook point.
114
+ # @param [Array<String,Symbol>] point the parent and child.
115
+ # @param [Integer] priority your priority.
116
+ # @note this is what plugins should use.
117
+ # @return [nil]
118
+ # --
119
+ def self.register(*point, priority: 2, &block)
120
+ if priority > 3
121
+ raise ArgumentError,
122
+ "priority must be between 1 and 3"
123
+ end
66
124
 
67
- def self.register(base, point, when_ = :late, &block)
68
- raise UnknownHookError, base: base unless HOOK_POINTS.key?(base)
69
- point = HOOK_ALIASES[base][point] if HOOK_ALIASES.fetch(base, {}).key?(point)
70
- raise UnknownHookError, point: point unless HOOK_POINTS[base].include?(point)
71
- all[base] ||= {}
125
+ check_point(*point)
126
+ out = @points[point[0]]
127
+ out = out[point[1]]
128
+ out = out[priority]
129
+ out << block
130
+ end
72
131
 
73
- point(base, point, when_) \
74
- .add(block)
132
+ # --
133
+ # @param point the points to check.
134
+ # Checks that a point exists or raises an error.
135
+ # @return [nil]
136
+ # --
137
+ def self.check_point(*point)
138
+ raise ArgumentError, "only give 2 points" if point.count > 2
139
+ if !@points.key?(point[0]) || !@points[point[0]].key?(point[1])
140
+ raise ArgumentError, "Unknown hook #{point}"
141
+ end
75
142
  end
76
143
  end
77
144
  end
@@ -0,0 +1,88 @@
1
+ # Frozen-string-literal: true
2
+ # Copyright: 2012 - 2017 - MIT License
3
+ # Encoding: utf-8
4
+
5
+ require_relative "extensible"
6
+ require "nokogiri"
7
+
8
+ module Jekyll
9
+ module Assets
10
+ class HTML < Extensible
11
+ attr_reader :doc
12
+
13
+ # --
14
+ # rubocop:disable Style/RedundantReturn
15
+ # --
16
+ def self.skips
17
+ return %i(
18
+ inline path data pic
19
+ )
20
+ end
21
+
22
+ # --
23
+ def initialize(doc:, **kwd)
24
+ super(**kwd)
25
+ @doc = doc
26
+ end
27
+
28
+ # --
29
+ # @param [Hash] args the arguments.
30
+ # @param [String] type the content type.
31
+ # Search for plugins and runners and then run them.
32
+ # @param [Sprockets::Asset] the current asset.
33
+ # @note look inside of plugins for examples.
34
+ # rubocop:enable Style/RedundantReturn
35
+ # @return [String] the final result.
36
+ # @param [Env] env the env.
37
+ # --
38
+ def self.build(args:, asset:, ctx:)
39
+ rtn = inherited.select do |o|
40
+ o.for?({
41
+ type: asset.content_type,
42
+ args: args,
43
+ })
44
+ end
45
+
46
+ doc = make_doc(rtn, asset: asset)
47
+ rtn.each do |o|
48
+ o = o.new({
49
+ doc: doc,
50
+ asset: asset,
51
+ args: args,
52
+ ctx: ctx,
53
+ })
54
+
55
+ o.run
56
+ end
57
+
58
+ # SVG will need to_xml!
59
+ out = doc.is_a?(Nokogiri::XML::Document) ? doc.to_xml : doc.to_html
60
+ rtn.select { |v| v.respond_to?(:cleanup) }.each do |o|
61
+ out = o.cleanup(out)
62
+ end
63
+ out
64
+ end
65
+
66
+ # --
67
+ # Allows a plugin to inform us if they want XML.
68
+ # @note technically this can break sub-plugins later.
69
+ # @return [true, false]
70
+ # --
71
+ def self.wants_xml?
72
+ false
73
+ end
74
+
75
+ # --
76
+ # Make an HTML/XML doc to work on.
77
+ # @note see `self.wants_html?` to control this.
78
+ # @return [Nokogiri::Document]
79
+ # --
80
+ def self.make_doc(builders, asset:)
81
+ wants = builders.map(&:wants_xml?).uniq
82
+ raise "incompatible wants xml/html for builders" if wants.size > 1
83
+ !wants[0] ? Nokogiri::HTML::DocumentFragment.parse("") :
84
+ Nokogiri::XML.parse(asset.to_s)
85
+ end
86
+ end
87
+ end
88
+ end
@@ -1,75 +1,33 @@
1
- # ----------------------------------------------------------------------------
2
1
  # Frozen-string-literal: true
3
- # Copyright: 2012 - 2016 - MIT License
2
+ # Copyright: 2012 - 2017 - MIT License
4
3
  # Encoding: utf-8
5
- # ----------------------------------------------------------------------------
4
+
5
+ require "jekyll"
6
6
 
7
7
  module Jekyll
8
8
  module Assets
9
9
  class Logger
10
- PREFIX = "Jekyll Assets:"
11
-
12
- class << self
13
-
14
- # --
15
- # @param [String] msg the message you wish to send out.
16
- # Deprecate a method and warn the user about it.
17
- # --
18
- def deprecate(msg, instance)
19
- filepath = caller[1].split(/\.rb:/).first + ".rb"
20
- filepath = Pathutil.new(filepath).relative_path_from(instance.in_source_dir)
21
- Jekyll.logger.error("", format("%s: %s", msg.red, filepath))
22
- yield if block_given?
23
- end
10
+ PREFIX = "Assets: "
11
+ def self.logger
12
+ self
24
13
  end
25
14
 
26
15
  # --
27
- # @return [Jekyll:Logger]
28
- # The logger.
29
- # --
30
- def log
31
- return @log ||= Jekyll.logger
32
- end
33
-
34
- # --
35
- # Log Level: 1
36
- # --
37
- def warn(msg = nil)
38
- log.warn(PREFIX,
39
- block_given?? yield : msg
40
- )
41
- end
42
-
16
+ # @note this is to be removed after 3.6.
17
+ # Creates self methods so that we can accept blocks.
18
+ # @param [String,Proc] message the message that to log.
19
+ # @return nil
43
20
  # --
44
- # Log Level: 1
45
- # --
46
- def error(msg = nil)
47
- log.error(PREFIX,
48
- block_given?? yield : msg
49
- )
50
- end
51
-
52
- # --
53
- # Log Level: 2
54
- # --
55
- def info(msg = nil)
56
- log.info(PREFIX,
57
- block_given?? yield : msg
58
- )
59
- end
60
-
61
- # --
62
- # Log Level: 3
63
- # --
64
- def debug(msg = nil)
65
- log.debug(PREFIX,
66
- block_given?? yield : msg
67
- )
21
+ %i(warn error info debug).each do |v|
22
+ define_singleton_method v do |message = nil, &block|
23
+ message = block.call if block
24
+ Jekyll.logger.send(v, PREFIX, message)
25
+ end
68
26
  end
69
27
 
70
28
  # --
71
- def log_level=(*)
72
- raise "Please set log levels on Jekyll.logger"
29
+ def self.efile(file)
30
+ Jekyll.logger.error("Asset File", file)
73
31
  end
74
32
  end
75
33
  end