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.
- checksums.yaml +4 -4
- data/Gemfile +21 -17
- data/LICENSE +1 -1
- data/README.md +365 -297
- data/Rakefile +13 -2
- data/lib/jekyll/assets.rb +15 -11
- data/lib/jekyll/assets/config.rb +91 -55
- data/lib/jekyll/assets/context.rb +32 -0
- data/lib/jekyll/assets/default.rb +98 -0
- data/lib/jekyll/assets/drop.rb +62 -0
- data/lib/jekyll/assets/env.rb +135 -278
- data/lib/jekyll/assets/extensible.rb +86 -0
- data/lib/jekyll/assets/filters.rb +22 -0
- data/lib/jekyll/assets/hook.rb +115 -48
- data/lib/jekyll/assets/html.rb +88 -0
- data/lib/jekyll/assets/logger.rb +17 -59
- data/lib/jekyll/assets/manifest.rb +15 -126
- data/lib/jekyll/assets/map.rb +57 -0
- data/lib/jekyll/assets/map/css.rb +43 -0
- data/lib/jekyll/assets/map/javascript.rb +43 -0
- data/lib/jekyll/assets/map/writer.rb +192 -0
- data/lib/jekyll/assets/patches/cached_env.rb +79 -0
- data/lib/jekyll/assets/patches/functions.rb +31 -0
- data/lib/jekyll/assets/patches/obsolete_files.rb +42 -0
- data/lib/jekyll/assets/patches/sprockets.rb +9 -0
- data/lib/jekyll/assets/patches/sprockets_data_uri.rb +12 -0
- data/lib/jekyll/assets/plugins.rb +9 -0
- data/lib/jekyll/assets/plugins/bootstrap.rb +5 -0
- data/lib/jekyll/assets/plugins/font-awesome.rb +8 -0
- data/lib/jekyll/assets/plugins/frontmatter.rb +28 -0
- data/lib/jekyll/assets/plugins/html/audio.rb +32 -0
- data/lib/jekyll/assets/plugins/html/css.rb +24 -0
- data/lib/jekyll/assets/plugins/html/defaults/audio.rb +69 -0
- data/lib/jekyll/assets/plugins/html/defaults/css.rb +48 -0
- data/lib/jekyll/assets/plugins/html/defaults/img.rb +54 -0
- data/lib/jekyll/assets/plugins/html/defaults/js.rb +48 -0
- data/lib/jekyll/assets/plugins/html/defaults/vid.rb +63 -0
- data/lib/jekyll/assets/plugins/html/img.rb +76 -0
- data/lib/jekyll/assets/plugins/html/js.rb +30 -0
- data/lib/jekyll/assets/plugins/html/pic.rb +125 -0
- data/lib/jekyll/assets/plugins/html/svg.rb +37 -0
- data/lib/jekyll/assets/plugins/html/vid.rb +26 -0
- data/lib/jekyll/assets/plugins/liquid.rb +57 -0
- data/lib/jekyll/assets/plugins/magick.rb +7 -0
- data/lib/jekyll/assets/plugins/optim.rb +7 -0
- data/lib/jekyll/assets/plugins/prefixer.rb +28 -0
- data/lib/jekyll/assets/plugins/proxy/magick.rb +131 -0
- data/lib/jekyll/assets/plugins/proxy/optim.rb +64 -0
- data/lib/jekyll/assets/plugins/searcher.rb +72 -0
- data/lib/jekyll/assets/proxy.rb +109 -0
- data/lib/jekyll/assets/tag.rb +176 -0
- data/lib/jekyll/assets/url.rb +23 -0
- data/lib/jekyll/assets/utils.rb +348 -0
- data/lib/jekyll/assets/version.rb +2 -2
- data/lib/jekyll/assets/writer.rb +36 -0
- metadata +102 -81
- data/lib/jekyll-assets.rb +0 -7
- data/lib/jekyll/assets/addons/autoprefixer.rb +0 -11
- data/lib/jekyll/assets/addons/bootstrap.rb +0 -9
- data/lib/jekyll/assets/addons/fontawesome.rb +0 -9
- data/lib/jekyll/assets/addons/javascript.rb +0 -11
- data/lib/jekyll/assets/cached.rb +0 -30
- data/lib/jekyll/assets/helpers.rb +0 -74
- data/lib/jekyll/assets/hooks/cache.rb +0 -21
- data/lib/jekyll/assets/hooks/compression.rb +0 -25
- data/lib/jekyll/assets/hooks/config.rb +0 -12
- data/lib/jekyll/assets/hooks/erb.rb +0 -15
- data/lib/jekyll/assets/hooks/jekyll/drops.rb +0 -9
- data/lib/jekyll/assets/hooks/jekyll/setup.rb +0 -11
- data/lib/jekyll/assets/hooks/jekyll/write.rb +0 -11
- data/lib/jekyll/assets/hooks/logger.rb +0 -11
- data/lib/jekyll/assets/hooks/sources.rb +0 -13
- data/lib/jekyll/assets/hooks/sprockets.rb +0 -9
- data/lib/jekyll/assets/hooks/version.rb +0 -11
- data/lib/jekyll/assets/liquid/context.rb +0 -19
- data/lib/jekyll/assets/liquid/drop.rb +0 -81
- data/lib/jekyll/assets/liquid/filters.rb +0 -49
- data/lib/jekyll/assets/liquid/tag.rb +0 -244
- data/lib/jekyll/assets/liquid/tag/defaults.rb +0 -27
- data/lib/jekyll/assets/liquid/tag/defaults/image.rb +0 -81
- data/lib/jekyll/assets/liquid/tag/defaults/sha.rb +0 -58
- data/lib/jekyll/assets/liquid/tag/parser.rb +0 -202
- data/lib/jekyll/assets/liquid/tag/proxied_asset.rb +0 -166
- data/lib/jekyll/assets/liquid/tag/proxies.rb +0 -130
- data/lib/jekyll/assets/patches/jekyll/cleaner.rb +0 -15
- data/lib/jekyll/assets/patches/jekyll/site.rb +0 -5
- data/lib/jekyll/assets/patches/kernel.rb +0 -29
- data/lib/jekyll/assets/patches/sprockets/asset.rb +0 -28
- data/lib/jekyll/assets/processors/less.rb +0 -70
- data/lib/jekyll/assets/processors/liquid.rb +0 -46
- data/lib/jekyll/assets/proxies/image_optim.rb +0 -80
- data/lib/jekyll/assets/proxies/magick.rb +0 -194
@@ -0,0 +1,28 @@
|
|
1
|
+
# Frozen-string-literal: true
|
2
|
+
# Copyright: 2012 - 2017 - MIT License
|
3
|
+
# Encoding: utf-8
|
4
|
+
|
5
|
+
require "jekyll/assets"
|
6
|
+
|
7
|
+
Jekyll::Assets::Utils.javascript? do
|
8
|
+
Jekyll::Assets::Utils.try_require "autoprefixer-rails" do
|
9
|
+
Jekyll::Assets::Hook.register :config, :before_merge do |c|
|
10
|
+
c.deep_merge!({
|
11
|
+
plugins: {
|
12
|
+
css: {
|
13
|
+
autoprefixer: {
|
14
|
+
# Your config here.
|
15
|
+
},
|
16
|
+
},
|
17
|
+
},
|
18
|
+
})
|
19
|
+
end
|
20
|
+
|
21
|
+
Jekyll::Assets::Hook.register :env, :after_init do
|
22
|
+
config = asset_config[:plugins][:css][:autoprefixer]
|
23
|
+
AutoprefixerRails.install(self, jekyll.safe ? config : {
|
24
|
+
# Your configuration here.
|
25
|
+
})
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,131 @@
|
|
1
|
+
# Frozen-string-literal: true
|
2
|
+
# Copyright: 2012 - 2017 - MIT License
|
3
|
+
# Encoding: utf-8
|
4
|
+
|
5
|
+
require "jekyll/assets"
|
6
|
+
|
7
|
+
module Jekyll
|
8
|
+
module Assets
|
9
|
+
module Plugins
|
10
|
+
class MiniMagick < Proxy
|
11
|
+
arg_keys :magick
|
12
|
+
content_types "image/webp"
|
13
|
+
content_types "image/jpeg"
|
14
|
+
content_types "image/svg+xml"
|
15
|
+
content_types "image/tiff"
|
16
|
+
content_types "image/bmp"
|
17
|
+
content_types "image/gif"
|
18
|
+
content_types "image/png"
|
19
|
+
|
20
|
+
class SameType < StandardError
|
21
|
+
def initialize(type)
|
22
|
+
"Trying to convert #{type} to #{type} won't work."
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def process
|
27
|
+
img = ::MiniMagick::Image.open(@file)
|
28
|
+
magick_format(img) if @args[:magick][:format]
|
29
|
+
img.combine_options do |c|
|
30
|
+
runners.each do |m|
|
31
|
+
method(m).arity == 2 ? send(m, img, c) : send(m, c)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
img.write(@file)
|
36
|
+
@file
|
37
|
+
ensure
|
38
|
+
img&.destroy!
|
39
|
+
end
|
40
|
+
|
41
|
+
def runners
|
42
|
+
private_methods(true).select do |v|
|
43
|
+
v =~ %r!^magick_! && v != :magick_format
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
def magick_format(img)
|
49
|
+
exts = @env.mime_exts.select do |k, v|
|
50
|
+
k == @args[:magick][:format] || v == @args[:magick][:format]
|
51
|
+
end
|
52
|
+
|
53
|
+
if exts.first
|
54
|
+
ext, type = exts.first
|
55
|
+
new_ = @file.sub_ext(ext)
|
56
|
+
raise SameType, type if type == asset.content_type
|
57
|
+
img.format(ext.sub(".", ""))
|
58
|
+
@file.cp(new_)
|
59
|
+
@file.rm
|
60
|
+
@file =
|
61
|
+
new_
|
62
|
+
end
|
63
|
+
|
64
|
+
img.write(@file)
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
def magick_compress(cmd)
|
69
|
+
if @args[:magick].key?(:compress)
|
70
|
+
cmd.compress @args[:magick][:compress]
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
private
|
75
|
+
def magick_quality(cmd)
|
76
|
+
if @args[:magick].key?(:quality)
|
77
|
+
cmd.quality @args[:magick][:quality]
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
private
|
82
|
+
def magick_resize(cmd)
|
83
|
+
if @args[:magick].key?(:resize)
|
84
|
+
cmd.resize @args[:magick][:resize]
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
private
|
89
|
+
def magick_rotate(cmd)
|
90
|
+
if @args[:magick].key?(:rotate)
|
91
|
+
cmd.rotate @args[:magick][:rotate]
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
private
|
96
|
+
def magick_flip(cmd)
|
97
|
+
if @args[:magick].key?(:flip)
|
98
|
+
cmd.flip @args[:magick][:flip]
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
private
|
103
|
+
def magick_crop(cmd)
|
104
|
+
if @args[:magick].key?(:crop)
|
105
|
+
cmd.crop @args[:magick][:crop]
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
private
|
110
|
+
def magick_gravity(cmd)
|
111
|
+
if @args[:magick].key?(:gravity)
|
112
|
+
cmd.gravity @args[:magick][:gravity]
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
private
|
117
|
+
def magick_strip(cmd)
|
118
|
+
cmd.strip
|
119
|
+
end
|
120
|
+
|
121
|
+
private
|
122
|
+
def magick_preset_resize(img, cmd)
|
123
|
+
# rubocop:disable Metrics/LineLength
|
124
|
+
width, height = img.width * 2, img.height * 2 if @args[:magick].key?(:double)
|
125
|
+
width, height = img.width / 2, img.height / 2 if @args[:magick].key?(:half)
|
126
|
+
cmd.resize "#{width}x#{height}" if width && height
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# Frozen-string-literal: true
|
2
|
+
# Copyright: 2012 - 2017 - MIT License
|
3
|
+
# Encoding: utf-8
|
4
|
+
|
5
|
+
require "jekyll/assets"
|
6
|
+
|
7
|
+
module Jekyll
|
8
|
+
module Assets
|
9
|
+
module Plugins
|
10
|
+
class ImageOptim < Proxy
|
11
|
+
arg_keys :optim
|
12
|
+
content_types "image/webp"
|
13
|
+
content_types "image/jpeg"
|
14
|
+
content_types "image/svg+xml"
|
15
|
+
content_types "image/tiff"
|
16
|
+
content_types "image/bmp"
|
17
|
+
content_types "image/gif"
|
18
|
+
content_types "image/png"
|
19
|
+
|
20
|
+
class UnknownPresetError < RuntimeError
|
21
|
+
def initialize(name)
|
22
|
+
"Unknown image_optim preset `#{name}'"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class MultiplePredefinedPresetsSpecified < RuntimeError
|
27
|
+
def initialize
|
28
|
+
super "Specifying multiple pre-defined presets " \
|
29
|
+
"at the same time is not supported"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def process
|
34
|
+
optimc = @env.asset_config[:plugins][:img][:optim]
|
35
|
+
preset = @args[:optim].keys
|
36
|
+
if preset.count > 1
|
37
|
+
raise MultiplePredefinedPresetsSpecified
|
38
|
+
end
|
39
|
+
|
40
|
+
preset = preset.first
|
41
|
+
# rubocop:disable Metrics/LineLength
|
42
|
+
raise UnknownPreset, preset if preset != :default && !optimc.key?(preset)
|
43
|
+
oc = optimc[preset] unless preset == :default
|
44
|
+
optim = ::ImageOptim.new(oc || {})
|
45
|
+
optim.optimize_image!(@file)
|
46
|
+
@file
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
# rubocop:enable Metrics/LineLength
|
54
|
+
Jekyll::Assets::Hook.register :config, :before_merge do |c|
|
55
|
+
c.deep_merge!({
|
56
|
+
plugins: {
|
57
|
+
img: {
|
58
|
+
optim: {
|
59
|
+
# Your config here.
|
60
|
+
},
|
61
|
+
},
|
62
|
+
},
|
63
|
+
})
|
64
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
# Frozen-string-literal: true
|
2
|
+
# Copyright: 2012 - 2017 - MIT License
|
3
|
+
# Encoding: utf-8
|
4
|
+
|
5
|
+
require "pathutil"
|
6
|
+
require_relative "../utils"
|
7
|
+
require "jekyll/assets"
|
8
|
+
require "jekyll"
|
9
|
+
|
10
|
+
module Jekyll
|
11
|
+
module Assets
|
12
|
+
module Plugins
|
13
|
+
# --
|
14
|
+
# Searches for `<img>` that have `<img asset>` or
|
15
|
+
# `<img asset="args">` and runs them through the asset
|
16
|
+
# system. This allows you to use real assets inside
|
17
|
+
# of your Markdown pre-convert.
|
18
|
+
# --
|
19
|
+
class Searcher
|
20
|
+
def initialize(doc)
|
21
|
+
@doc = doc
|
22
|
+
end
|
23
|
+
|
24
|
+
# --
|
25
|
+
def run
|
26
|
+
html.search("img[@asset]").each do |v|
|
27
|
+
raise ArgumentError, "src is empty" unless v[:src]
|
28
|
+
args = "#{v.delete('src')&.value} #{v.delete('asset')&.value}"
|
29
|
+
pctx = ::Liquid::ParseContext.new
|
30
|
+
|
31
|
+
attrs = v.attributes.keys
|
32
|
+
args, = Tag.new("asset", args, pctx).render_raw(ctx)
|
33
|
+
args.to_h(html: true).each do |k, vv|
|
34
|
+
unless attrs.include?(k.to_s)
|
35
|
+
v.set_attribute(k, vv)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
out = html.to_html
|
41
|
+
@doc.output = out
|
42
|
+
end
|
43
|
+
|
44
|
+
# --
|
45
|
+
private
|
46
|
+
def ctx
|
47
|
+
::Liquid::Context.new({}, {}, {
|
48
|
+
site: @doc.site,
|
49
|
+
})
|
50
|
+
end
|
51
|
+
|
52
|
+
# --
|
53
|
+
private
|
54
|
+
def html
|
55
|
+
@html ||= begin
|
56
|
+
out = @doc.output
|
57
|
+
# @see https://github.com/sparklemotion/nokogiri/issues/553
|
58
|
+
good, buggy = Encoding::UTF_8, Encoding::ASCII_8BIT
|
59
|
+
out = out.encode good if out.encoding == buggy
|
60
|
+
Nokogiri::HTML.send((@doc.output.strip
|
61
|
+
.start_with?("<!DOCTYPE ") ? :parse :
|
62
|
+
:fragment), out)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
Jekyll::Hooks.register [:pages, :documents, :posts], :post_render do |d|
|
71
|
+
Jekyll::Assets::Plugins::Searcher.new(d).run
|
72
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
# Frozen-string-literal: true
|
2
|
+
# Copyright: 2012 - 2017 - MIT License
|
3
|
+
# Encoding: utf-8
|
4
|
+
|
5
|
+
require "pathutil"
|
6
|
+
require_relative "extensible"
|
7
|
+
require_relative "hook"
|
8
|
+
require "digest"
|
9
|
+
|
10
|
+
module Jekyll
|
11
|
+
module Assets
|
12
|
+
class Proxy < Extensible
|
13
|
+
attr_reader :file
|
14
|
+
DIG = Digest::SHA256
|
15
|
+
DIR = "proxied"
|
16
|
+
|
17
|
+
class Deleted < StandardError
|
18
|
+
def initialize(obj)
|
19
|
+
super "#{obj} violated a contract and " \
|
20
|
+
"deleted your proxy file"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# --
|
25
|
+
# @param [Hash] args the args.
|
26
|
+
# @param [Sprockets::Asset] asset the asset.
|
27
|
+
# @param [String] type the assets content_type
|
28
|
+
# @param [Env] env the environment.
|
29
|
+
# Run all your proxies on assets.
|
30
|
+
# @return [Sprockets::Asset]
|
31
|
+
# --
|
32
|
+
def self.proxy(asset, args:, ctx:)
|
33
|
+
proxies = Proxy.inherited.select do |o|
|
34
|
+
o.for?(type: asset.content_type, args: args)
|
35
|
+
end
|
36
|
+
|
37
|
+
return asset if proxies.empty?
|
38
|
+
env = ctx.registers[:site].sprockets
|
39
|
+
file = copy(asset, args: args, ctx: ctx)
|
40
|
+
cache = file.basename.sub_ext("").to_s
|
41
|
+
|
42
|
+
env.cache.fetch(cache) do
|
43
|
+
proxies.each do |o|
|
44
|
+
obj = o.new(file, {
|
45
|
+
args: args,
|
46
|
+
asset: asset,
|
47
|
+
ctx: ctx,
|
48
|
+
})
|
49
|
+
|
50
|
+
o = obj.process
|
51
|
+
file = o if o.is_a?(Pathutil) && file != o
|
52
|
+
raise Deleted, o unless file.exist?
|
53
|
+
end
|
54
|
+
|
55
|
+
true
|
56
|
+
end
|
57
|
+
|
58
|
+
env.find_asset!(file)
|
59
|
+
end
|
60
|
+
|
61
|
+
# --
|
62
|
+
# Copy the asset to the proxied directory.
|
63
|
+
# @note this is done so we do not directly alter.
|
64
|
+
# @param [Sprockets::Asset] asset the current asset.
|
65
|
+
# @param [Env] env the environment.
|
66
|
+
# @param [Hash] args the args.
|
67
|
+
# @return [Pathutil]
|
68
|
+
# --
|
69
|
+
def self.copy(asset, ctx:, args:)
|
70
|
+
env = ctx.registers[:site].sprockets
|
71
|
+
raw = args.instance_variable_get(:@raw)
|
72
|
+
key = DIG.hexdigest(raw)[0, 6]
|
73
|
+
|
74
|
+
path = env.in_cache_dir(DIR)
|
75
|
+
extname = File.extname(args[:argv1])
|
76
|
+
out = Pathutil.new(path).join(key).sub_ext(extname)
|
77
|
+
out.dirname.mkdir_p unless out.dirname.exist?
|
78
|
+
Pathutil.new(asset.filename).cp(out)
|
79
|
+
|
80
|
+
out
|
81
|
+
end
|
82
|
+
|
83
|
+
# --
|
84
|
+
# @return [Symbol] the argument key.
|
85
|
+
# Allows you to tell the proxier which args are yours.
|
86
|
+
# @note we will not run your proxy if the argkey doen't match.
|
87
|
+
# @param [Symbol] key the key.
|
88
|
+
# --
|
89
|
+
def self.args_key(key = nil)
|
90
|
+
unless key.nil?
|
91
|
+
@key =
|
92
|
+
key
|
93
|
+
end
|
94
|
+
|
95
|
+
@key
|
96
|
+
end
|
97
|
+
|
98
|
+
# --
|
99
|
+
def initialize(file, **kwd)
|
100
|
+
super(**kwd)
|
101
|
+
@file = file
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
Jekyll::Assets::Hook.register :env, :after_init do
|
108
|
+
append_path(in_cache_dir(Jekyll::Assets::Proxy::DIR))
|
109
|
+
end
|
@@ -0,0 +1,176 @@
|
|
1
|
+
# Frozen-string-literal: true
|
2
|
+
# Copyright: 2012 - 2017 - MIT License
|
3
|
+
# Encoding: utf-8
|
4
|
+
|
5
|
+
require "fastimage"
|
6
|
+
require_relative "html"
|
7
|
+
require "liquid/tag/parser"
|
8
|
+
require "active_support/hash_with_indifferent_access"
|
9
|
+
require "active_support/core_ext/hash/indifferent_access"
|
10
|
+
require "active_support/core_ext/hash/deep_merge"
|
11
|
+
require_relative "default"
|
12
|
+
require_relative "proxy"
|
13
|
+
require "nokogiri"
|
14
|
+
|
15
|
+
module Jekyll
|
16
|
+
module Assets
|
17
|
+
class Tag < Liquid::Tag
|
18
|
+
class << self
|
19
|
+
public :new
|
20
|
+
end
|
21
|
+
|
22
|
+
# --
|
23
|
+
class MixedArg < StandardError
|
24
|
+
def initialize(arg, mixed)
|
25
|
+
super "cannot use #{arg} w/ #{mixed}"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# --
|
30
|
+
class InvalidExternal < StandardError
|
31
|
+
def initialize(arg)
|
32
|
+
super "cannot use `#{arg}' with external url's"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# --
|
37
|
+
attr_reader :name
|
38
|
+
attr_reader :tokens
|
39
|
+
attr_reader :args
|
40
|
+
attr_reader :tag
|
41
|
+
|
42
|
+
# --
|
43
|
+
def initialize(tag, args, tokens)
|
44
|
+
@tag = tag.to_sym
|
45
|
+
@args = Liquid::Tag::Parser.new(args)
|
46
|
+
@tokens = tokens
|
47
|
+
@og_args = args
|
48
|
+
super
|
49
|
+
end
|
50
|
+
|
51
|
+
# --
|
52
|
+
def render_raw(ctx)
|
53
|
+
env = ctx.registers[:site].sprockets
|
54
|
+
args = env.parse_liquid(@args, ctx: ctx)
|
55
|
+
raise Sprockets::FileNotFound, "UNKNOWN" unless args.key?(:argv1)
|
56
|
+
asset = external(ctx, args: args) if env.external?(args)
|
57
|
+
asset ||= internal(ctx)
|
58
|
+
[args, asset]
|
59
|
+
end
|
60
|
+
|
61
|
+
# --
|
62
|
+
# @return [String]
|
63
|
+
# rubocop:disable Metrics/AbcSize
|
64
|
+
# Render the tag, run the proxies, set the defaults.
|
65
|
+
# @note Defaults are ran twice just incase the content type
|
66
|
+
# changes, at that point there might be something that
|
67
|
+
# has to change in the new content.
|
68
|
+
# --
|
69
|
+
def render(ctx)
|
70
|
+
env = ctx.registers[:site].sprockets
|
71
|
+
|
72
|
+
args, asset = render_raw(ctx)
|
73
|
+
env.logger.debug args&.to_h(html: false).inspect
|
74
|
+
return_or_build(ctx, args: args, asset: asset) do
|
75
|
+
HTML.build({
|
76
|
+
args: args,
|
77
|
+
asset: asset,
|
78
|
+
ctx: ctx,
|
79
|
+
})
|
80
|
+
end
|
81
|
+
# --
|
82
|
+
rescue ExecJS::RuntimeError => e
|
83
|
+
env.logger.error e.message
|
84
|
+
env.logger.efile @args[:argv1]
|
85
|
+
raise ExecJS::RuntimeError, \
|
86
|
+
"JS Error"
|
87
|
+
# --
|
88
|
+
# @note you can --trace to get this same info
|
89
|
+
# Handle errors that Sass ships because Jekyll finds
|
90
|
+
# error handling hard, and makes it even harder, so we
|
91
|
+
# need to ship debug info to the user, or they'll
|
92
|
+
# never get it. That's not very good.
|
93
|
+
# --
|
94
|
+
rescue Sass::SyntaxError => e
|
95
|
+
env.logger.error e.message
|
96
|
+
env.logger.efile env.strip_paths(e.backtrace.first)
|
97
|
+
raise Sass::SyntaxError, "Sass Error"
|
98
|
+
end
|
99
|
+
|
100
|
+
# --
|
101
|
+
def return_or_build(ctx, args:, asset:)
|
102
|
+
methods.grep(%r!^on_(?\!or_build$)!).each do |m|
|
103
|
+
out = send(m, args, ctx: ctx, asset: asset)
|
104
|
+
if out
|
105
|
+
return out
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
yield
|
110
|
+
end
|
111
|
+
|
112
|
+
# --
|
113
|
+
# Returns the path to the asset.
|
114
|
+
# @example {% asset img.png @path %}
|
115
|
+
# @return [String]
|
116
|
+
# --
|
117
|
+
def on_path(args, ctx:, asset:)
|
118
|
+
env = ctx.registers[:site].sprockets
|
119
|
+
|
120
|
+
return unless args[:path]
|
121
|
+
raise InvalidExternal, "@path" if env.external?(args)
|
122
|
+
env.prefix_url(asset.digest_path)
|
123
|
+
end
|
124
|
+
|
125
|
+
# --
|
126
|
+
# Returns the data uri of an object.
|
127
|
+
# @example {% asset img.png @data-url %}
|
128
|
+
# @example {% asset img.png @data_uri %}
|
129
|
+
# @return [String]
|
130
|
+
# --
|
131
|
+
def on_data(args, ctx:, asset:)
|
132
|
+
env = ctx.registers[:site].sprockets
|
133
|
+
|
134
|
+
return unless args[:data]
|
135
|
+
raise InvalidExternal "@data" if env.external?(args)
|
136
|
+
asset.data_uri
|
137
|
+
end
|
138
|
+
|
139
|
+
# --
|
140
|
+
# @param [Liquid::Context] ctx
|
141
|
+
# Set's up an external url using `Url`
|
142
|
+
# @return [Url]
|
143
|
+
# --
|
144
|
+
def external(ctx, args:)
|
145
|
+
env = ctx.registers[:site].sprockets
|
146
|
+
out = env.external_asset(args[:argv1], args: args)
|
147
|
+
Default.set(args, ctx: ctx, asset: out)
|
148
|
+
|
149
|
+
out
|
150
|
+
end
|
151
|
+
|
152
|
+
# --
|
153
|
+
# @param [Liquid::Context] ctx
|
154
|
+
# Set's up an internal asset using `Sprockets::Asset`
|
155
|
+
# @return [Sprockets::Asset]
|
156
|
+
# --
|
157
|
+
def internal(ctx)
|
158
|
+
env = ctx.registers[:site].sprockets
|
159
|
+
original = env.find_asset!(args[:argv1])
|
160
|
+
Default.set(args, ctx: ctx, asset: original)
|
161
|
+
out = Proxy.proxy(original, args: args, ctx: ctx)
|
162
|
+
env.manifest.compile(out.logical_path)
|
163
|
+
|
164
|
+
Default.set(args, {
|
165
|
+
ctx: ctx, asset: out
|
166
|
+
})
|
167
|
+
|
168
|
+
out
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
# --
|
175
|
+
|
176
|
+
Liquid::Template.register_tag "asset", Jekyll::Assets::Tag
|