Module: Jekyll::Menu

Defined in:
lib/jekyll/menu.rb,
lib/jekyll/menu/version.rb

Constant Summary collapse

VERSION =
"0.1.1"

Class Method Summary collapse

Class Method Details

.initialize(data) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/jekyll/menu.rb', line 18

def self.initialize(data)
  @data = data ? data : {}
  if @site.data.key?("menus")
    if @site.data["menus"].length
      self.setup_menus()
    end
  end
end

.item_class(path, has_items = false) ⇒ Object



134
135
136
137
138
139
140
141
142
143
# File 'lib/jekyll/menu.rb', line 134

def self.item_class(path, has_items = false)
  class_name = @options["item_class"]
  if self.item_is_active(path)
    class_name = "#{class_name} #{@options['item_active_class']}"
  end
  if has_items
    class_name = "#{class_name} #{@options['item_sub_class']}"
  end
  class_name
end

.item_is_active(path) ⇒ Object



123
124
125
126
127
128
129
130
131
132
# File 'lib/jekyll/menu.rb', line 123

def self.item_is_active(path)
  url = @data.url
  unless path === "/"
    path = path.gsub(/\/$/, "")
  end
  unless url === "/"
    url = url.gsub(/\/$/, "")
  end
  url === path
end


109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/jekyll/menu.rb', line 109

def self.item_link(item, path)
  if item.key?("link")
    item["link"]
  else
    unless path === "/"
      title = item["title"]
      link = self.slugify(title)
      "#{path}/#{link}"
    else
      path
    end
  end
end


145
146
147
148
149
150
151
# File 'lib/jekyll/menu.rb', line 145

def self.link_class(path)
  class_name = @options["link_class"]
  if self.item_is_active(path)
    class_name = "#{class_name} #{@options['item_active_class']}"
  end
  class_name
end

.liquify(input) ⇒ Object



68
69
70
71
# File 'lib/jekyll/menu.rb', line 68

def self.liquify(input)
    output = Liquid::Template.parse(input)
    output.render(@data)
end


83
84
85
86
87
88
89
90
91
92
# File 'lib/jekyll/menu.rb', line 83

def self.menu_class(sub = false, items = [])
  class_name = @options["menu_class"]
  if sub
    class_name = "#{class_name} #{@options['menu_sub_class']}"
    if self.menu_has_active_item(items)
      class_name = "#{class_name} #{@options['menu_active_class']}"
    end
  end
  class_name
end


94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/jekyll/menu.rb', line 94

def self.menu_has_active_item(items = [], has_active = false)
  unless has_active
    items.each do |item|
      if item["items"]
        self.menu_has_active_item(item["items"], has_active)
      end
      if item["active"]
        has_active = true
        break
      end
    end
  end
  has_active
end


77
78
79
80
81
# File 'lib/jekyll/menu.rb', line 77

def self.menu_path(title)
  path = @options["url_path"]
  title = self.slugify(title)
  "#{path}#{title}"
end

.setup_menu(items, title, sub = true) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/jekyll/menu.rb', line 38

def self.setup_menu(items, title, sub = true)
  new_menu = {}
  if title
    new_menu["title"] = title
  end
  path = menu_path(title)
  new_menu["path"] = path
  new_menu["items"] = self.setup_menu_items(items, path)
  new_menu["link"] = "#"
  new_menu["class"] = self.menu_class(sub, new_menu["items"])
  new_menu
end

.setup_menu_items(items, path) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/jekyll/menu.rb', line 51

def self.setup_menu_items(items, path)
  path = path ? path : @options["url_path"]
  items.map { |i|
    item = {}
    item["title"] = i["title"]
    item["link"] = self.item_link(i, path)
    item["class"] = self.item_class(item["link"], i.key?("items"))
    item["link_class"] = self.link_class(item["link"])
    item["active"] = self.item_is_active(item["link"])
    if i.key?("items")
      item["menu"] = self.setup_menu(i["items"], i["title"], true)
      item["link"] = "#"
    end
    item
  }
end

.setup_menusObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/jekyll/menu.rb', line 27

def self.setup_menus
  menus = @site.data["menus"].map { |m|
    title = m[0]
    items = m[1]
    sub = false
    m[1] = self.setup_menu(items, title, sub)
    m
  }
  @data.data["menu"] = Hash[*menus.flatten(1)]
end

.slugify(path) ⇒ Object



73
74
75
# File 'lib/jekyll/menu.rb', line 73

def self.slugify(path)
  path.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
end