Class: JekyllIconList::IconList

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/jekyll_icon_list.rb

Overview

This tag looks for commands in the following format: icon_list item1 item2 item3 –ul class=“example” –li class=“example2” % And renders an unordered list of icons and labels. Items are a space separated list of names defined in _data/icons.yml. Acceptable commands are –ul, –li, –svg, and –img. Their arguments are inserted into their respective HTML elements upon render.

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, raw_input, tokens) ⇒ IconList

Returns a new instance of IconList.



18
19
20
21
22
# File 'lib/jekyll_icon_list.rb', line 18

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

Instance Method Details

#build_anchor(url, content) ⇒ Object



136
137
138
139
140
141
142
143
144
# File 'lib/jekyll_icon_list.rb', line 136

def build_anchor(url, content)
  a = DoubleTag.new(
    'a',
    attributes: { href: url },
    oneline: true,
    content: content
  )
  a.add_attributes @attributes['a'] if @attributes['a']
end

#build_html(all_items_data) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/jekyll_icon_list.rb', line 56

def build_html(all_items_data)
  list = DoubleTag.new 'ul', attributes: @attributes['ul']

  @item_shortnames.each do |n|
    this_item_data = all_items_data[n] || {}

    icon_location = find_icon n, this_item_data

    label = build_label(n, this_item_data)

    list.add_content build_li(this_item_data, icon_location, label)
  end

  list.to_s
end

#build_image_tag(icon_filename) ⇒ Object



112
113
114
115
116
117
118
# File 'lib/jekyll_icon_list.rb', line 112

def build_image_tag(icon_filename)
  if icon_filename.split('.').pop.casecmp('svg').zero?
    build_svg(icon_filename)
  else
    build_img(icon_filename)
  end
end

#build_img(icon_filename) ⇒ Object



131
132
133
134
# File 'lib/jekyll_icon_list.rb', line 131

def build_img(icon_filename)
  img = SingleTag.new 'img', attributes: { src: icon_filename }
  img.add_attributes @attributes['img'] if @attributes['img']
end

#build_label(shortname, this_item_data) ⇒ Object



95
96
97
98
# File 'lib/jekyll_icon_list.rb', line 95

def build_label(shortname, this_item_data)
  this_item_data['label'] ||
    shortname.split(/[-_]/).map(&:capitalize).join(' ')
end

#build_li(this_item_data, icon_location, label) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/jekyll_icon_list.rb', line 100

def build_li(this_item_data, icon_location, label)
  li = DoubleTag.new(
    'li',
    attributes: @attributes['li'],
    content: [build_image_tag(icon_location), label],
    oneline: true
  )
  return li unless this_item_data['url']

  li.reset_content build_anchor(this_item_data['url'], li.content)
end

#build_settingsObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/jekyll_icon_list.rb', line 39

def build_settings
  # raw_input will look something like this:
  # 'item1 item2 item3 --ul attribute="value" --(...)'

  @attributes = @icon_list_settings['defaults'].dup || {}
  # {'ul' => 'class="awesome" (...)', (...)}

  raw_input_array = @raw_input.split('--').map { |i| i.strip.split(' ') }
  # [['item1', 'item2', 'item3'], ['ul', 'attr="value', 'value2"'],(...)]

  @item_shortnames = raw_input_array.shift
  # ['ul, 'attribute="value1 value2"', (...)]

  raw_input_array.each { |a| @attributes[a.shift] = a.join ' ' }
  # {'ul' => 'attribute="value1 value2 value3"'}
end

#build_svg(icon_filename) ⇒ Object



120
121
122
123
124
125
126
127
128
129
# File 'lib/jekyll_icon_list.rb', line 120

def build_svg(icon_filename)
  params = icon_filename
  params << ' ' + @attributes['svg'] if @attributes['svg']
  Jekyll::Tags::JekyllInlineSvg.send(
    :new,
    'svg',
    params,
    @tokens
  ).render(@context)
end

#find_icon(item_shortname, this_item_data) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/jekyll_icon_list.rb', line 72

def find_icon(item_shortname, this_item_data)
  icon_filename = this_item_data['icon']
  path = @icon_list_settings['default_path'] || ''

  if icon_filename
    path + icon_filename
  else
    path = '/images/icons/' if path.empty?
    search_path(path, item_shortname)
  end
end

#render(context) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/jekyll_icon_list.rb', line 24

def render(context)
  @context = context

  site_settings = @context.registers[:site]
  raise 'could not load website configuration data' unless site_settings

  @icon_list_settings = site_settings.config['icon_list'] || {}

  all_items_data = site_settings.data['icon_list'] || {}

  build_settings

  build_html(all_items_data)
end

#search_path(path, item) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/jekyll_icon_list.rb', line 84

def search_path(path, item)
  # If there is a leading slash, we have to strip it for Dir to know it's
  # relative:
  search_path = path[0] == '/' ? path[1..-1] : path
  search_results = Dir.glob(search_path + item + '.*')
  raise "No icon found at #{path + item} .*" unless search_results.any?

  # And put it back so that pages outside of the root directory keep working
  search_results.first.prepend '/'
end