locomotive-subnav 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3b4a11d0319c1a984dc5f82dea237a0203982d7e
4
+ data.tar.gz: 4bec0b6a521647ba0d54c2088b3a8209a4b86857
5
+ SHA512:
6
+ metadata.gz: 9613635ae7f0ed362326d8743f2c0e4ba446284968815103ea23d82475d606c3c12db19733e2622446c03d97a3ef394582ea0d36988532c29bdf887003084e1c
7
+ data.tar.gz: 3768f96e426bac6b30363c3fdbc3d82a2d5d6e2d260d20cee90ce048c4454d2af1e2adc03fb11bcafa8c584a4da02f3bd0e0d1758fd0e0431662cbbc0689ff69
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.log
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format progress --out tmp/test.report
3
+ --format progress
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at tibor@gmah.net. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in locomotive-subnav.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Tibor Claassen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # locomotive-subnav
2
+
3
+ Provide your LocomotiveCMS site with a context aware set of nested navigation
4
+ links that represent the currently selected branch of the page. Typical use
5
+ cases are a subnavigation inside a sidebar or breadcrumbs.
6
+
7
+ ## Installation
8
+
9
+ Add a dependency to the Gemfile of *both* your Wagon site and the Locomotive
10
+ Rails application.
11
+
12
+ ```ruby
13
+ gem 'locomotive-subnav'
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ Put the following Liquid tags inside any of your Wagon template files:
19
+
20
+ ### breadcrumbs
21
+
22
+ For a simple breadcrumb trail use the `breadcrumbs` tag.
23
+
24
+ ```liquid
25
+ {% breadcrumbs (<options>) %}
26
+ ```
27
+
28
+ ##### Options
29
+
30
+ | Name | Type | Description | Default |
31
+ | --- | --- | --- | --- |
32
+ | `level` | `Integer` | Show ancestors including specified level. | `0` |
33
+
34
+ ##### Example
35
+
36
+ ```liquid
37
+ {% breadcrumbs level: 1 %}
38
+ ```
39
+
40
+ ### subnav
41
+
42
+ For a subtree navigation that will also display the siblings of ancestors use
43
+ the `subnav` tag.
44
+
45
+ ```liquid
46
+ {% subnav (<options>) %}
47
+ ```
48
+
49
+ ##### Options
50
+
51
+ | Name | Type | Description | Default |
52
+ | --- | --- | --- | --- |
53
+ | `level` | `Integer` | Show ancestor trail including specified level. | `0` |
54
+ | `collapse` | `Integer` or `Array` of `Integer`s | Only show selected item on given level(s). | `[]` |
55
+
56
+ ##### Example
57
+
58
+ ```liquid
59
+ {% subnav level: 1, collapse: 1 %}
60
+ ```
61
+
62
+ ## Development
63
+
64
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
65
+ `rake spec` to run the tests. You can also run `bin/console` for an interactive
66
+ prompt that will allow you to experiment.
67
+
68
+ To install this gem onto your local machine, run `bundle exec rake install`. To
69
+ release a new version, update the version number in `version.rb`, and then run
70
+ `bundle exec rake release`, which will create a git tag for the version, push
71
+ git commits and tags, and push the `.gem` file to
72
+ [rubygems.org](https://rubygems.org).
73
+
74
+ ## Contributing
75
+
76
+ Bug reports and pull requests are welcome on GitHub at
77
+ https://github.com/codebeige/locomotive-subnav. This project is intended to
78
+ be a safe, welcoming space for collaboration, and contributors are expected to
79
+ adhere to the [Contributor Covenant](http://contributor-covenant.org) code of
80
+ conduct.
81
+
82
+
83
+ ## License
84
+
85
+ The gem is available as open source under the terms of the
86
+ [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "locomotive/subnav"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,10 @@
1
+ module Locomotive
2
+ module Steam
3
+ class PageRepository
4
+ def ancestors_with_children(page, level = 0)
5
+ all(k(:parent_id, :in) => page.parent_ids + [page._id, nil],
6
+ k(:depth, :gte) => level)
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,34 @@
1
+ require_relative 'tag'
2
+
3
+ module Locomotive
4
+ module Subnav
5
+ module Liquid
6
+ module Tags
7
+ class Breadcrumbs < Tag
8
+ def render(context)
9
+ current_page = context.registers[:page]
10
+ container 'breadcrumbs' do
11
+ list do
12
+ ancestors_and_self(current_page, context).map do |page|
13
+ attrs = page_attributes(page, context)
14
+ item attrs[:title], attrs[:path]
15
+ end.join "\n"
16
+ end
17
+ end
18
+ end
19
+
20
+ protected
21
+
22
+ def list
23
+ %(<ol>#{indent yield}</ol>)
24
+ end
25
+
26
+ def ancestors_and_self(page, context)
27
+ page_repository = context.registers[:services].repositories.page
28
+ page_repository.ancestors_of(page).select { |p| display? p }
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,51 @@
1
+ require_relative 'tag'
2
+
3
+ module Locomotive
4
+ module Subnav
5
+ module Liquid
6
+ module Tags
7
+ class Subnav < Tag
8
+ def render(context)
9
+ current_page = context.registers[:page]
10
+ container 'subnav' do
11
+ levels = current_branch(current_page, context)
12
+ selection = current_page.parent_ids.push + [current_page._id]
13
+ render_levels(levels, selection, context)
14
+ end
15
+ end
16
+
17
+ def collapsed_levels
18
+ @_collapsed_levels ||= [*@attributes[:collapse]].map(&:to_i)
19
+ end
20
+
21
+ protected
22
+
23
+ def render_levels(levels, selection, context)
24
+ return if levels.empty?
25
+ current_level = levels.first
26
+ rest = levels.drop(1)
27
+ name = 'current' if rest.empty?
28
+ list(name) do
29
+ current_level.map do |page|
30
+ attrs = page_attributes(page, context)
31
+ if selection.include? page._id
32
+ item 'selected', attrs[:title], attrs[:path] do
33
+ render_levels rest, selection, context
34
+ end
35
+ elsif !collapsed_levels.include? page.depth
36
+ item(attrs[:title], attrs[:path])
37
+ end
38
+ end.compact.join "\n"
39
+ end
40
+ end
41
+
42
+ def current_branch(page, context)
43
+ page_repository = context.registers[:services].repositories.page
44
+ pages = page_repository.ancestors_with_children(page, level)
45
+ pages.select { |p| display? p }.group_by(&:depth).values
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,95 @@
1
+ module Locomotive
2
+ module Subnav
3
+ module Liquid
4
+ module Tags
5
+ class Tag < ::Liquid::Tag
6
+
7
+ def self.inherited(subclass)
8
+ tag_name = subclass.name.split('::').last.downcase.freeze
9
+ ::Liquid::Template.register_tag(tag_name, subclass)
10
+ end
11
+
12
+ DEFAULT_ATTRIBUTES = {level: 0}.freeze
13
+
14
+ def initialize(name, source, options = {})
15
+ parsed_attributes = parse_attributes(source)
16
+ @attributes = DEFAULT_ATTRIBUTES.merge(parsed_attributes)
17
+ end
18
+
19
+ def level
20
+ @attributes[:level].to_i
21
+ end
22
+
23
+ protected
24
+
25
+ def indent(markup, drop_leading_line_break = false)
26
+ unless markup.nil?
27
+ indented = %(#{markup.gsub(/^/, ' ')}\n)
28
+ if drop_leading_line_break
29
+ indented
30
+ else
31
+ %(\n#{indented})
32
+ end
33
+ else
34
+ ''
35
+ end
36
+ end
37
+
38
+ def container(name)
39
+ %(<nav class="#{name}">#{indent yield}</nav>)
40
+ end
41
+
42
+ def list(name = nil)
43
+ attrs = if name then %( class="#{name}") else '' end
44
+ %(<ul#{attrs}>#{indent yield}</ul>)
45
+ end
46
+
47
+ def item(name = nil, label, path)
48
+ markup = ''
49
+ markup << if name then %(<li class="#{name}">) else '<li>' end
50
+ link = %(<a href="#{path}">#{label}</a>)
51
+ if block_given?
52
+ markup << indent(link)
53
+ markup << indent(yield, true)
54
+ else
55
+ markup << link
56
+ end
57
+ markup << '</li>'
58
+ end
59
+
60
+ def display?(page)
61
+ page.depth >= level &&
62
+ page.published? &&
63
+ page.listed? &&
64
+ !page.templatized?
65
+ end
66
+
67
+ def page_attributes(page, context)
68
+ url_builder = context.registers[:services].url_builder
69
+ translated_page = translate_page(page, context)
70
+ {title: translated_page.title,
71
+ path: url_builder.url_for(translated_page)}
72
+ end
73
+
74
+ def translate_page(page, context)
75
+ Locomotive::Steam::Decorators::I18nDecorator.new(
76
+ page,
77
+ context.registers[:locale],
78
+ context.registers[:site].default_locale
79
+ )
80
+ end
81
+
82
+ private
83
+
84
+ def parse_attributes(source)
85
+ {}.tap do |attributes|
86
+ source.scan(::Liquid::TagAttributes) do |key, value|
87
+ attributes[key.to_sym] = value.gsub(/['"]+/, '')
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,2 @@
1
+ require_relative 'tags/breadcrumbs'
2
+ require_relative 'tags/subnav'
@@ -0,0 +1,5 @@
1
+ module Locomotive
2
+ module Subnav
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ require 'locomotive/steam'
2
+ require 'locomotive/steam/page_repository'
3
+
4
+ require_relative 'subnav/version'
5
+ require_relative 'subnav/liquid/tags'
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'locomotive/subnav/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "locomotive-subnav"
8
+ spec.version = Locomotive::Subnav::VERSION
9
+ spec.authors = ["Tibor Claassen"]
10
+ spec.email = ["tibor@gmah.net"]
11
+
12
+ spec.summary = %q{Navigate current section}
13
+ spec.description = %q(Provides Liquid tags for rendering breadcrumbs and
14
+ subnavigation of a LocomotiveCMS site.
15
+ ).gsub(/^\s*/, '')
16
+ spec.homepage = "https://github.com/codebeige/locomotive-subnav"
17
+ spec.license = "MIT"
18
+
19
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
20
+ f.match(%r{^(test|spec|features)/})
21
+ end
22
+
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_runtime_dependency "locomotivecms_steam", "~> 1.0.1"
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.11"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ spec.add_development_dependency "rspec", "~> 3.0"
32
+ spec.add_development_dependency "rspec-html-matchers", "~> 0.7.1"
33
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: locomotive-subnav
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tibor Claassen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-03-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: locomotivecms_steam
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.0.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-html-matchers
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.7.1
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.7.1
83
+ description: |
84
+ Provides Liquid tags for rendering breadcrumbs and
85
+ subnavigation of a LocomotiveCMS site.
86
+ email:
87
+ - tibor@gmah.net
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - ".rspec"
94
+ - ".travis.yml"
95
+ - CODE_OF_CONDUCT.md
96
+ - Gemfile
97
+ - LICENSE.txt
98
+ - README.md
99
+ - Rakefile
100
+ - bin/console
101
+ - bin/setup
102
+ - lib/locomotive/steam/page_repository.rb
103
+ - lib/locomotive/subnav.rb
104
+ - lib/locomotive/subnav/liquid/tags.rb
105
+ - lib/locomotive/subnav/liquid/tags/breadcrumbs.rb
106
+ - lib/locomotive/subnav/liquid/tags/subnav.rb
107
+ - lib/locomotive/subnav/liquid/tags/tag.rb
108
+ - lib/locomotive/subnav/version.rb
109
+ - locomotive-subnav.gemspec
110
+ homepage: https://github.com/codebeige/locomotive-subnav
111
+ licenses:
112
+ - MIT
113
+ metadata: {}
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.5.1
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: Navigate current section
134
+ test_files: []