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
- .initialize(data) ⇒ Object
- .item_class(path, has_items = false) ⇒ Object
- .item_is_active(path) ⇒ Object
- .item_link(item, path) ⇒ Object
- .link_class(path) ⇒ Object
- .liquify(input) ⇒ Object
- .menu_class(sub = false, items = []) ⇒ Object
- .menu_has_active_item(items = [], has_active = false) ⇒ Object
- .menu_path(title) ⇒ Object
- .setup_menu(items, title, sub = true) ⇒ Object
- .setup_menu_items(items, path) ⇒ Object
- .setup_menus ⇒ Object
- .slugify(path) ⇒ Object
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.() 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 |
.item_link(item, path) ⇒ Object
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 |
.link_class(path) ⇒ Object
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 |
.menu_class(sub = false, items = []) ⇒ Object
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/jekyll/menu.rb', line 83 def self.(sub = false, items = []) class_name = @options["menu_class"] if sub class_name = "#{class_name} #{@options['menu_sub_class']}" if self.(items) class_name = "#{class_name} #{@options['menu_active_class']}" end end class_name end |
.menu_has_active_item(items = [], has_active = false) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/jekyll/menu.rb', line 94 def self.(items = [], has_active = false) unless has_active items.each do |item| if item["items"] self.(item["items"], has_active) end if item["active"] has_active = true break end end end has_active end |
.menu_path(title) ⇒ Object
77 78 79 80 81 |
# File 'lib/jekyll/menu.rb', line 77 def self.(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.(items, title, sub = true) = {} if title ["title"] = title end path = (title) ["path"] = path ["items"] = self.(items, path) ["link"] = "#" ["class"] = self.(sub, ["items"]) 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.(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.(i["items"], i["title"], true) item["link"] = "#" end item } end |
.setup_menus ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/jekyll/menu.rb', line 27 def self. = @site.data["menus"].map { |m| title = m[0] items = m[1] sub = false m[1] = self.(items, title, sub) m } @data.data["menu"] = Hash[*.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 |