Skip to content

Commit

Permalink
Initial commit, it's almost ready world.
Browse files Browse the repository at this point in the history
  • Loading branch information
envygeeks committed Sep 14, 2016
0 parents commit ae78db7
Show file tree
Hide file tree
Showing 23 changed files with 642 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/.bundle
/vendor/bundle
/spec/fixture/_site
/Gemfile.lock
/*.gem
26 changes: 26 additions & 0 deletions Gem.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Frozen-string-literal: true
# Copyright: 2015 Forestry.io - MIT License
# Encoding: utf-8

$LOAD_PATH.unshift(File.expand_path("../lib", __FILE__))
require "jekyll/menus/version"

Gem::Specification.new do |spec|
spec.authors = ["Jordon Bedwell"]
spec.version = Jekyll::Menus::VERSION
spec.homepage = "http:https://github.com/forestryio/jekyll-menus/"
spec.description = "Menus (site navigation) for your Jekyll website"
spec.summary = "Menus (navigation) for your very own Jekyll website."
spec.files = %W(Rakefile Gemfile) + Dir["lib/**/*"]
spec.required_ruby_version = ">= 2.1.0"
spec.email = ["[email protected]"]
spec.require_paths = ["lib"]
spec.name = "jekyll-menus"
spec.has_rdoc = false
spec.license = "MIT"

spec.add_runtime_dependency("jekyll", "~> 3.1")
spec.add_development_dependency(
"rspec", ">= 3", "< 4"
)
end
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
source "https://rubygems.org"
gemspec

group :development do
gem "pry"
end
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2015-2016 Forestry.io

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.
92 changes: 92 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Jekyll Menus

Complex menus for Jekyll.

## Usage

You can create `_data/menu.yml`, `_data/menus.yml`, or both, or add menu items
via your front-matter. Both are merged into the same menus so you can even
split off menus between the two, so that you can have menus that have
internal and external links! Just make sure the identifiers match.

### Front-Matter Examples
#### String Key Menu Item

```yml
---
menus: main
---
```

The above configuration will infer all data (url, title, identifier, weight)
from your page data, the default weight is "-1" and the identifier is made from
the pages slug, which Jekyll itself generates. It will also place the page
on the "main" menu on your behalf after inferring said data.

#### Array of String Key Menu Items

```yml
---
menus:
- header
- footer
---
```

The above configuration will infer all data (url, title, identifier, weight)
from your page data, the default weight is "-1" and the identifier is made from
the pages slug, which Jekyll itself generates. It will also place the page
on the "header", and "footer" menus after inferring said data.

#### Hash with Key as Identifier

```yml
---
menus:
main:
url: "/custom-url"
---
```

The above will place a menu on "main" and override the URL for you inferring
the rest of the data on your behalf. You can add multiple keys with hashes
if you wish to place the item on multiple menus, and you can override as much
as you wish at that time. ***Data is not inferred between items, so if you
override in one you must override in all, or the default values will be
used.***

### `_data/menu.yml`, `_data/menus.yml`

***All data within menu(s).yml must provide url, title, identifier, and
weight (actually weight is optional)***

Menu items within data files must follow a key array format, or a key hash
format, we do not accept string formats because we do not infer data, it's
impossible to infer such data efficiently and the data files are mostly built
for you to add external links or links to other parts of your site that are
considered sub-domains. Examples:

```yml
main:
- title: Title
identifier: title
url: url
```

```yml
main:
title: Title
identifier: title
url: url
```

***It should be noted that _data/menu.yml and _data/menus.yml are both read
and merged, so you can have one, or both... we won't judge you if you happen
to use both of these files at once, it's your choice!***

### Custom Menu Data

You can add any amount of custom data you wish to an item, we do not remove
data, and we do not block it, we will pass any data you wish to put into the
into the menu item. It is up to you what data you put there, we only check
that our own keys exist, and if they don't then we fail in certain scenarios.
5 changes: 5 additions & 0 deletions lib/jekyll-menus.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Frozen-string-literal: true
# Copyright: 2015 Forestry.io - MIT License
# Encoding: utf-8

require "jekyll/menus"
188 changes: 188 additions & 0 deletions lib/jekyll/menus.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
# Frozen-string-literal: true
# Copyright: 2015 Forestry.io - MIT License
# Encoding: utf-8

module Jekyll
class Menus
autoload :Utils, "jekyll/menus/utils"
autoload :Drops, "jekyll/menus/drops"

def initialize(site)
@site = site
end

#

def to_liquid_drop
Drops::All.new(Utils.deep_merge(
_config_menus, _page_menus
))
end

#

def _config_menus
mns = [@site.data["menu"], @site.data["menus"]]
out = {}; mns.compact.each do |menus|
menus.each do |key, menu|
if menu.is_a?(Hash) || menu.is_a?(Array)
(menu = [menu].flatten).each do |item|
_validate_config_menu_item(
item
)
end

else
_throw_invalid_menu_entry(
menu
)
end

out = Utils.deep_merge(out, {
key => menu
})
end
end

out
end

#

def _page_menus
out = {}

@site.pages.select { |p| p.data.keys.grep(/menus?/).size > 0 }.each_with_object({}) do |page|
[page.data["menus"], page.data["menu"]].flatten.compact.map do |menu|

# --
# menu: key
# menu:
# - key1
# - key2
# --

if menu.is_a?(Array) || menu.is_a?(String)
_simple_front_matter_menu(menu, {
:mergeable => out, :page => page
})

#

elsif menu.is_a?(Hash)
menu.each do |key, item|
out[key] ||= []

# --
# menu:
# key: identifier
# --

if item.is_a?(String)
out[key] << _fill_front_matter_menu({ "identifier" => item }, {
:page => page
})

# --
# menu:
# key:
# url: /url
# --

elsif item.is_a?(Hash)
out[key] << _fill_front_matter_menu(item, {
:page => page
})

# --
# menu:
# key:
# - url: /url
# --

else
_throw_invalid_menu_entry(
item
)
end
end

# --
# menu:
# key: 3
# --

else
_throw_invalid_menu_entry(
menu
)
end
end
end

out
end

#

private
def _simple_front_matter_menu(menu, mergeable: nil, page: nil)
if menu.is_a?(Array)
then menu.each do |item|
if !item.is_a?(String)
_throw_invalid_menu_entry(
item
)

else
_simple_front_matter_menu(item, {
:mergeable => mergeable, :page => page
})
end
end

else
mergeable[menu] ||= []
mergeable[menu] << _fill_front_matter_menu(nil, {
:page => page
})
end
end

#

private
def _fill_front_matter_menu(val, page: nil)
raise ArgumentError, "Kwd 'page' is required." unless page
val ||= {}

val["url"] ||= page.url
val["title"] ||= page.data["title"]
val["identifier"] ||= File.basename(page.name, page.ext)
val["weight"] ||= -1
val
end

#

private
def _validate_config_menu_item(item)
unless item.is_a?(Hash) && item.values_at("url", "title", "identifier").compact.size == 3
_throw_invalid_menu_entry(
item
)
end
end

#

private
def _throw_invalid_menu_entry(data)
raise RuntimeError, "Invalid menu item given: #{
data.inspect
}"
end
end
end

require "jekyll/menus/hook"
13 changes: 13 additions & 0 deletions lib/jekyll/menus/drops.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Frozen-string-literal: true
# Copyright: 2015 Forestry.io - MIT License
# Encoding: utf-8

module Jekyll
class Menus
module Drops
autoload :Menu, "jekyll/menus/drops/menu"
autoload :All, "jekyll/menus/drops/all"
autoload :Item, "jekyll/menus/drops/item"
end
end
end
Loading

0 comments on commit ae78db7

Please sign in to comment.