Class: Jekyll::AutoImage

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/jekyll-auto-image-include.rb

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, path, tokens) ⇒ AutoImage

Returns a new instance of AutoImage.



8
9
10
11
# File 'lib/jekyll-auto-image-include.rb', line 8

def initialize(tag_name, path, tokens)
  super
  @path = path
end

Instance Method Details

#render(context) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/jekyll-auto-image-include.rb', line 13

def render(context)
  site = context.registers[:site]
  base_dir = site.config['source']

  recursion = '/'
  pattern = '*.{jpg,jpeg,png,gif,bmp,tif,tiff,svg}'

  page = context.environments.first["page"]
  # page:

  # {
  #   "layout"=>"default",
  #   "title"=>"Startseite",
  #   "content"=> "<h1>Notes</h1>\n\n<ul>\n ...",
  #   "dir"=>"",
  #   "name"=>"index.html",
  #   "path"=>"index.html",
  #   "url"=>"/"
  # }
  if @path == nil || @path == ""
    dir = page["dir"]
  else
    dir = @path.strip
  end

  if site.config['auto_image_include']
    config = site.config['auto_image_include']
    if config['pattern']
      pattern = config['pattern']
    end

    if config['recursive']
      recursion = '/**/'
    end
  end

  output = ''

  folder = File.join(base_dir, dir, recursion + pattern)
  files = Dir.glob(folder, File::FNM_CASEFOLD).sort
  files.each do |image_fullpath|
    image_path = image_fullpath.sub(base_dir, '')
    image_name = File.basename(image_fullpath)
    output += "\n<a href=\"#{image_path}\"><img src=\"#{image_path}\" alt=\"#{image_name}\"></a>"
  end
  output
end