sigmund_liquid 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZTZhMzJhMjYzNTdiMTBlMmRjMjYzMWJjMGQ1OTRkZTkwYTVlYzE2ZQ==
4
+ MDhkNjEyOTVmOWZmMDlhNmM0YmY1M2M1MWQ5NGZhMzZmNDIyNTgwNQ==
5
5
  data.tar.gz: !binary |-
6
- Y2VlODc3YjYzZjY4MzcyOThlMjllZWQwZDEwMzhhZWZiYjUzYWEwZQ==
6
+ YzU5OTNlOGI4NDM1ZTc2ZDg4ZGRhZTE4NDRiZGY0ZjUwNDFlZDE3Mg==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- MDlhNmM3OWQyMWFkZDMwZGNhYjY2MDViZjQyMGU1OTU2NzczZmVmNGExOWFi
10
- YjAwZDk3Y2NiZGQ3MjQwYzZlZDJhYzFjZWYwMzk1NDYxY2QyMmNjZWFiYmI5
11
- MjIyOTQ3MWM4ZmZmNGI3NzE2YzJjOGZkM2FmNjYxZWZhMjAxZDk=
9
+ MWU0ZTEzN2ZjMzcxN2FjYTgwMzgwMDdhMTIzN2U1YzRiMzU3OGQxM2YyZjNh
10
+ ZjUyZmQ3NjcwMWQ1Y2ZmYmY5YTA3M2UyNWExZGE0ZDE5ZDZlZDZmYTQ0NDgx
11
+ MDE1MzcwYTlhMGIxZDUzYjZjM2RlNjllNTdlNTY2NzYyYzZkMGU=
12
12
  data.tar.gz: !binary |-
13
- YzYxZGUyZGQ3YzU0ZjAzOGJiMjE1ZDY5NDI1YmE3ZDg1YzI2NGY2YTJmY2I3
14
- MmY1ZmQ5MGZkMDI4NjgyZDZmNmU3OWQ4NDIxYjhjYmU0N2EyY2VhN2M4ZDQw
15
- YmUzYzQ2N2IyOTk0NDFmNTFlOWMwZTA3MjNhN2ExOGRiOTcwMjM=
13
+ NjU0MjU5YTUwODM1MWE0NmEwYmY2OTZjMjRlNWNiZjg5ZjcxMWQ1OWUzMDNh
14
+ YzkzNGNhNzE2MDE1OTA0ZDA2Mjg0NjNiZWEzYTc2NmRlOTllNTNiYTQyZjBh
15
+ MDQ3MmI5M2FkNzUwNWM2MGYwYTZkYTgyODg5MTc0YTY1ZDcyNTM=
@@ -0,0 +1,164 @@
1
+ module Sigmund
2
+ module Liquid
3
+ module Tags
4
+ # Display the children pages of the site, current page or the parent page. If not precised, nav is applied on the current page.
5
+ # The html output is based on the ul/li tags.
6
+ #
7
+ # Passing through depth will control how many nested children are output
8
+ #
9
+ # Usage:
10
+ #
11
+ # {% nav site %} => <ul class="nav"><li class="on"><a href="/features">Features</a></li></ul>
12
+ #
13
+ # {% nav site, no_wrapper: true, exclude: 'contact|about', id: 'main-nav', class: 'nav', active_class: 'on' }
14
+ #
15
+ class CustomNav < ::Liquid::Tag
16
+
17
+ Syntax = /(#{::Liquid::Expression}+)?/
18
+
19
+ def initialize(tag_name, markup, tokens, context)
20
+ if markup =~ Syntax
21
+ @source = ($1 || 'page').gsub(/"|'/, '')
22
+ @options = { id: 'nav', depth: 1, class: '', active_class: 'on', bootstrap: false }
23
+ markup.scan(::Liquid::TagAttributes) { |key, value| @options[key.to_sym] = value.gsub(/"|'/, '') }
24
+
25
+ @options[:exclude] = Regexp.new(@options[:exclude]) if @options[:exclude]
26
+
27
+ @options[:add_attributes] = []
28
+ if @options[:snippet]
29
+ template = @options[:snippet].include?('{') ? @options[:snippet] : context[:site].snippets.where(slug: @options[:snippet] ).try(:first).try(:template)
30
+ unless template.blank?
31
+ @options[:liquid_render] = ::Liquid::Template.parse(template)
32
+ @options[:add_attributes] = ['editable_elements']
33
+ end
34
+ end
35
+
36
+ else
37
+ raise ::Liquid::SyntaxError.new("Syntax Error in 'nav' - Valid syntax: nav <page|site> <options>")
38
+ end
39
+
40
+ super
41
+ end
42
+
43
+ def render(context)
44
+ children_output = []
45
+
46
+ entries = fetch_entries(context)
47
+
48
+ entries.each_with_index do |p, index|
49
+ css = []
50
+ css << 'first' if index == 0
51
+ css << 'last' if index == entries.size - 1
52
+
53
+ children_output << render_entry_link(context, p, css.join(' '), 1)
54
+ end
55
+
56
+ output = children_output.join("\n")
57
+
58
+ if @options[:no_wrapper] != 'true'
59
+ list_class = !@options[:class].blank? ? %( class="#{@options[:class]}") : ''
60
+ output = %{<nav id="#{@options[:id]}"#{list_class}><ul>\n#{output}</ul></nav>}
61
+ end
62
+
63
+ output
64
+ end
65
+
66
+ private
67
+
68
+ # Determines root node for the list
69
+ def fetch_entries(context)
70
+ @site, @page = context.registers[:site], context.registers[:page]
71
+
72
+ children = (case @source
73
+ when 'site' then @site.pages.root.minimal_attributes(@options[:add_attributes]).first # start from home page
74
+ when 'parent' then @page.parent || @page
75
+ when 'page' then @page
76
+ else
77
+ @site.pages.fullpath(@source).minimal_attributes(@options[:add_attributes]).first
78
+ end).children_with_minimal_attributes(@options[:add_attributes]).to_a
79
+
80
+ children.delete_if { |p| !include_page?(p) }
81
+ end
82
+
83
+ # Returns a list element, a link to the page and its children
84
+ def render_entry_link(context, page, css, depth)
85
+ selected = @page.fullpath =~ /^#{page.fullpath}(\/.*)?$/ ? " #{@options[:active_class]}" : ''
86
+
87
+ icon = @options[:icon] ? '<span></span>' : ''
88
+ return title = render_title(context, page)
89
+ label = %{#{icon if @options[:icon] != 'after' }#{title}#{icon if @options[:icon] == 'after' }}
90
+
91
+ link_options = caret = ''
92
+ href = File.join('/', @site.localized_page_fullpath(page))
93
+
94
+ if render_children_for_page?(page, depth) && bootstrap?
95
+ css += ' dropdown'
96
+ link_options = %{ class="dropdown-toggle" data-toggle="dropdown"}
97
+ href = '#'
98
+ caret = %{ <b class="caret"></b>}
99
+ end
100
+
101
+ output = %{<li id="#{page.slug.to_s.dasherize}-link" class="link#{selected} #{css}">}
102
+ output << %{<a href="#{href}"#{link_options}>#{label}#{caret}</a>}
103
+ output << render_entry_children(context, page, depth.succ) if (depth.succ <= @options[:depth].to_i)
104
+ output << %{</li>}
105
+
106
+ output.strip
107
+ end
108
+
109
+ def render_children_for_page?(page, depth)
110
+ depth.succ <= @options[:depth].to_i && page.children.reject { |c| !include_page?(c) }.any?
111
+ end
112
+
113
+ # Recursively creates a nested unordered list for the depth specified
114
+ def render_entry_children(context, page, depth)
115
+ output = %{}
116
+
117
+ children = page.children_with_minimal_attributes(@options[:add_attributes]).reject { |c| !include_page?(c) }
118
+ if children.present?
119
+ output = %{<ul id="#{@options[:id]}-#{page.slug.to_s.dasherize}" class="#{bootstrap? ? 'dropdown-menu' : ''}">}
120
+ children.each do |c, page|
121
+ css = []
122
+ css << 'first' if children.first == c
123
+ css << 'last' if children.last == c
124
+
125
+ output << render_entry_link(context, c, css.join(' '), depth)
126
+ end
127
+ output << %{</ul>}
128
+ end
129
+
130
+ output
131
+ end
132
+
133
+ def render_title(context, page)
134
+ if @options[:liquid_render]
135
+ context.stack do
136
+ context['page'] = page
137
+ @options[:liquid_render].render(context)
138
+ end
139
+ else
140
+ page.title
141
+ end
142
+ end
143
+
144
+ # Determines whether or not a page should be a part of the menu
145
+ def include_page?(page)
146
+ if !page.listed? || page.templatized? || !page.published?
147
+ false
148
+ elsif @options[:exclude]
149
+ (page.fullpath =~ @options[:exclude]).nil?
150
+ else
151
+ true
152
+ end
153
+ end
154
+
155
+ def bootstrap?
156
+ @options[:bootstrap] == 'true'
157
+ end
158
+
159
+ end
160
+
161
+ ::Liquid::Template.register_tag('custom_nav', CustomNav)
162
+ end
163
+ end
164
+ end
@@ -1,5 +1,5 @@
1
1
  module Sigmund
2
2
  module Liquid
3
- VERSION = "1.0.1"
3
+ VERSION = "1.0.2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sigmund_liquid
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - amainguy