Class: Jekyll::Assets::Tag

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/jekyll/assets/tag.rb

Defined Under Namespace

Classes: InvalidExternal, MixedArg

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag, args, tokens) ⇒ Tag



43
44
45
46
47
48
# File 'lib/jekyll/assets/tag.rb', line 43

def initialize(tag, args, tokens)
  @tag = tag.to_sym
  @tokens = tokens
  @args = args
  super
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



39
40
41
# File 'lib/jekyll/assets/tag.rb', line 39

def args
  @args
end

#nameObject (readonly)



37
38
39
# File 'lib/jekyll/assets/tag.rb', line 37

def name
  @name
end

#tagObject (readonly)

Returns the value of attribute tag.



40
41
42
# File 'lib/jekyll/assets/tag.rb', line 40

def tag
  @tag
end

#tokensObject (readonly)

Returns the value of attribute tokens.



38
39
40
# File 'lib/jekyll/assets/tag.rb', line 38

def tokens
  @tokens
end

Instance Method Details

#external(ctx, args:) ⇒ Url

– Set’s up an external url using ‘Url` –

Parameters:

  • ctx (Liquid::Context)

Returns:



149
150
151
152
153
154
155
# File 'lib/jekyll/assets/tag.rb', line 149

def external(ctx, args:)
  env = ctx.registers[:site].sprockets
  out = env.external_asset(args[:argv1], args: args)
  Default.set(args, ctx: ctx, asset: out)

  out
end

#internal(ctx, args:) ⇒ Sprockets::Asset

– Set’s up an internal asset using ‘Sprockets::Asset` –

Parameters:

  • ctx (Liquid::Context)

Returns:



162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/jekyll/assets/tag.rb', line 162

def internal(ctx, args:)
  env = ctx.registers[:site].sprockets
  original = env.find_asset!(args[:argv1])
  Default.set(args, ctx: ctx, asset: original)
  out = Proxy.proxy(original, args: args, ctx: ctx)
  env.assets_to_write |= [out.logical_path]

  Default.set(args, {
    ctx: ctx, asset: out
  })

  out
end

#on_data(args, ctx:, asset:) ⇒ String

– Returns the data uri of an object. –

Examples:

asset img.png @data-url %

asset img.png @data_uri %

Returns:

  • (String)


136
137
138
139
140
141
142
# File 'lib/jekyll/assets/tag.rb', line 136

def on_data(args, ctx:, asset:)
  env = ctx.registers[:site].sprockets

  return unless args[:data]
  raise InvalidExternal "@data" if env.external?(args)
  asset.data_uri
end

#on_path(args, ctx:, asset:) ⇒ String

– Returns the path to the asset. –

Examples:

asset img.png @path %

Returns:

  • (String)

Raises:



122
123
124
125
126
127
128
# File 'lib/jekyll/assets/tag.rb', line 122

def on_path(args, ctx:, asset:)
  env = ctx.registers[:site].sprockets

  return unless args[:path]
  raise InvalidExternal, "@path" if env.external?(args)
  env.prefix_url(asset.digest_path)
end

#render(ctx) ⇒ String

Note:

Defaults are ran twice just incase the content type changes, at that point there might be something that has to change in the new content.

– Render the tag, run the proxies, set the defaults. –

Returns:

  • (String)


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/jekyll/assets/tag.rb', line 69

def render(ctx)
  env = ctx.registers[:site].sprockets; args, asset = render_raw(ctx)
  env.logger.debug args.to_h(html: false).inspect

  return_or_build(ctx, args: args, asset: asset) do
    HTML.build({
      args: args,
      asset: asset,
      ctx: ctx,
    })
  end
# --
rescue Sprockets::FileNotFound => e
  e_not_found(e, {
    ctx: ctx,
  })
# --
rescue ExecJS::RuntimeError => e
  e_exjs(e, {
    args: args,
    ctx: ctx,
  })
# --
# @note you can --trace to get this same info
# Handle errors that Sass ships because Jekyll finds
# error handling hard, and makes it even harder, so we
# need to ship debug info to the user, or they'll
# never get it. That's not very good.
# --
rescue Sass::SyntaxError => e
  e_sass(e, {
    args: args,
    ctx: ctx,
  })
end

#render_raw(ctx) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/jekyll/assets/tag.rb', line 51

def render_raw(ctx)
  env = ctx.registers[:site].sprockets

  args = Liquid::Tag::Parser.new(@args)
  args = env.parse_liquid(args, ctx: ctx)
  raise_unfound_asset_on(ctx: ctx, with: args) unless args.key?(:argv1)
  asset = external(ctx, args: args) if env.external?(args)
  asset ||= internal(ctx, args: args)
  [args, asset]
end

#return_or_build(ctx, args:, asset:) ⇒ Object



106
107
108
109
110
111
112
113
114
115
# File 'lib/jekyll/assets/tag.rb', line 106

def return_or_build(ctx, args:, asset:)
  methods.grep(%r!^on_(?\!or_build$)!).each do |m|
    out = send(m, args, ctx: ctx, asset: asset)
    if out
      return out
    end
  end

  yield
end