Class: Shi::Jekyll::ImageTag

Inherits:
Liquid::Tag
  • Object
show all
Includes:
Tools
Defined in:
lib/shi/jekyll/images/image.rb

Constant Summary collapse

DEFAULT_THUMB_BOUNDS =
'320'
DEFAULT_WIDTH =
Shi::Args::Value::Measure::px(320)

Instance Method Summary collapse

Instance Method Details

#clean_path(path) ⇒ Object



35
36
37
# File 'lib/shi/jekyll/images/image.rb', line 35

def clean_path path
  path.split(/\/?#/)[0]
end

#render(context) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/shi/jekyll/images/image.rb', line 39

def render context
  args = Shi::Args::Params::parse context, @markup
  extra_args = context['extra_args'] || {}

  source = args[:src] || args[:source]
  if source == nil && Jekyll::StaticFile === args[0]
    source = args[0]
  end
  if !Jekyll::StaticFile === source
    raise ArgumentError, "Invalid source: #{source.inspect}!"
  end

  link = args[:link]
  link = false if args[:no_link]
  link = true if link == nil
  # if link == false only thumbnail needed

  site = context.registers[:site]
  page_path = clean_path(context['page.path'])
  if page_path == nil
    p context.registers[:page]
  end
  page = site.documents.find { |d| d.relative_path == page_path }
  page ||= site.pages.find { |p| p.relative_path == page_path }

  gen_big = false
  href = if link == false # nothing
      nil
    elsif Jekyll::StaticFile === link && !link.write? # uncopyable static file => picture
      bounds = args[:bounds] || extra_args[:bounds]
      Jekyll::PathManager::join '', Shi::Jekyll::Images::File::create(page, link, bounds, nil).url
    elsif link.respond_to?(:url) && link.url # document, page or static file
      Jekyll::PathManager::join '', link.url
    elsif String === link # external link
      link
    elsif link == true # generated big picture
      gen_big = true
      bounds = args[:bounds] || extra_args[:bounds]
      Jekyll::PathManager::join '', Shi::Jekyll::Images::File::create(page, source, bounds, nil).url
    else
      raise ArgumentError, "Invalid link: #{link.inspect}!"
    end

  thumb = args[:thumb]
  thumb = false if args[:no_thumb]
  thumb = true if thumb == nil
  # if thumb == false use full size as thumbnail

  if !gen_big && !thumb
    raise ArgumentError, "You can't disable large image and thumbnail at the same time!"
  end

  src = if thumb
      bounds = thumb_bounds context, args, extra_args
      crop = args[:crop] || extra_args[:crop]
      Shi::Jekyll::Images::File::create(page, source, bounds, crop).url
    else
      href
    end
  src = Jekyll::PathManager::join '', src

  cls = '__image'
  cls += ' ' + args[:class] if args[:class]
  cls += ' ' + extra_args[:class] if extra_args[:class]

  width = args[:width] || extra_args[:width] || DEFAULT_WIDTH

  style = ""
  style += extra_args[:style] if extra_args[:style]
  style += args[:style] if args[:style]

  shape = args[:shape] || extra_args[:shape]
  case shape
  when Jekyll::StaticFile
    shape_url = if shape.write?
        shape.url
      else
        bounds = thumb_bounds context, args, extra_args
        crop = args[:crop] || extra_args[:crop]
        Jekyll::PathManager::join '', Shi::Jekyll::Images::File::create(page, shape, bounds, crop).url
      end
    style += "shape-outside:url(#{shape_url});"
  when String
    cls += " __shape_#{shape}"
  when true
    style += "shape-outside:url(#{src});"
  end

  caption = args[:caption]
  title = args[:title] || caption
  alt = args[:alt] || title
  id = args[:id]

  attrs = "class=\"#{cls}\""
  attrs += " style=\"#{style}\"" if style && !style.empty?
  attrs += " alt=\"#{alt}\"" if alt
  attrs += " title=\"#{title}\"" if title
  attrs += " id=\"#{id}\""

  figure = args[:figure]
  if figure && !extra_args.empty?
    raise ArgumentError, 'Nested figures not allowed!'
  end

  result = ''
  if figure
    fig_class = '__figure __implicit_figure'
    place = args[:place]
    if place == 'right' || args[:right]
      fig_class += ' __right'
    elsif place == 'left' || args[:left]
      fig_class += ' __left'
    else
      fig_class += ' __center'
    end
    fig_class += ' ' + args[:fig_class] if args[:fig_class]
    fig_style = "max-width:#{width.value};"
    fig_style += args[:fig_style] if args[:fig_style]
    case shape
    when Jekyll::StaticFile
      shape_url = if shape.write?
          shape.url
        else
          bounds = thumb_bounds context, args, extra_args
          crop = args[:crop] || extra_args[:crop]
          Jekyll::PathManager::join '', Shi::Jekyll::Images::File::create(page, shape, bounds, crop).url
        end
      fig_style += "shape-outside:url(#{shape_url});"
    when String
      fig_class += " __shape_#{shape}"
    when true
      fig_style += "shape-outside:url(#{src});"
    end
    result += "<figure class=\"#{fig_class}\" style=\"#{fig_style}\" markdown=\"0\">"
  end
  if link != false
    if gen_big
      result += "<a href=\"#{href}\" class=\"__image_link\">"
    else
      result += "<a href=\"#{href}\">"
    end
  end
  # src = Jekyll::PathManager::join '', src
  result += "<img src=\"#{src}\" #{attrs}>"
  if link != false
    result += '</a>'
  end
  if figure
    if caption
      result += "<figcaption markdown=\"span\">#{caption}</figcaption>"
    end
    result += '</figure>'
  end

  result
end

#thumb_bounds(context, args, extra) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/shi/jekyll/images/image.rb', line 25

def thumb_bounds context, args, extra
  bounds = args[:thumb_bounds] || extra[:thumb_bounds]
  width = args[:width] || extra[:width]
  if width && !bounds
    bounds = width_to_bounds width
  end
  bounds ||= lookup_with(context, 'thumb_bounds', ['page', 'layout', 'site.shi_images']) || DEFAULT_THUMB_BOUNDS
  bounds
end

#width_to_bounds(width) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/shi/jekyll/images/image.rb', line 14

def width_to_bounds width
  if Shi::Args::Value::Measure === width
    width.to_px.to_s
  else
    width.to_s
  end
end