jekyll-uj-powertools 1.5.1 → 1.6.0
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 +4 -4
- data/README.md +27 -5
- data/jekyll-uj-powertools.gemspec +1 -1
- data/lib/filters/main.rb +1 -1
- data/lib/generators/inject-properties.rb +56 -11
- data/lib/jekyll-uj-powertools.rb +2 -1
- data/lib/tags/iffalsy.rb +38 -0
- data/lib/tags/iftruthy.rb +38 -0
- metadata +3 -2
- data/lib/tags/ifistruthy.rb +0 -161
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bee69fe37cc10927e8f51898ed41337c5f71a2a013f1270f3ff0dea6b9318a42
|
4
|
+
data.tar.gz: 6a07ff1145e4d2fc93dd2f269ce38ed51d42606206d435afb9a47c909b8575ff
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7b87aa8225fd7cbebb64c20a21711e04f1faefd7221ddfc351a322424f46fb26c3b9459ce1b62938c430842c27b8b4aebf910c49517c846ceacab01fc6664c19
|
7
|
+
data.tar.gz: 7b8e91ed9fb513e952163f53519f4eaf065a516832422dbc11ad9f5c39c0e0d535c4bfba312f36d9272f0ff11bb234475121dee9ad5367b04c98fc76e458c887
|
data/README.md
CHANGED
@@ -78,7 +78,7 @@ Use the `site.uj.cache_breaker` variable to append a cache-busting query paramet
|
|
78
78
|
<link rel="stylesheet" href="{{ "/assets/css/style.css" | prepend: site.baseurl }}?v={{ site.uj.cache_breaker }}">
|
79
79
|
```
|
80
80
|
|
81
|
-
|
81
|
+
## Page Variables
|
82
82
|
### `page.random_id` Variable
|
83
83
|
Generate a random ID for each page, useful for sorting randomly or for unique identifiers.
|
84
84
|
|
@@ -126,6 +126,24 @@ Resolves the site, layout, and page data into a single object, which can be usef
|
|
126
126
|
{{ page.my.variable | default: layout.my.variable | default: site.my.variable }}
|
127
127
|
```
|
128
128
|
|
129
|
+
## Tags
|
130
|
+
### `iftruthy` Tag
|
131
|
+
A custom Liquid tag that checks if a variable is truthy (not nil, not false, not empty string, not 0) and renders the content inside the tag if it is truthy.
|
132
|
+
```liquid
|
133
|
+
{% iftruthy my_variable %}
|
134
|
+
<p>This content will only be rendered if my_variable is truthy.</p>
|
135
|
+
{% endiftruthy %}
|
136
|
+
```
|
137
|
+
|
138
|
+
### `iffalsy` Tag
|
139
|
+
A custom Liquid tag that checks if a variable is falsy (nil, false, empty string, or 0) and renders the content inside the tag if it is falsy.
|
140
|
+
```liquid
|
141
|
+
{% iffalsy my_variable %}
|
142
|
+
<p>This content will only be rendered if my_variable is falsy.</p>
|
143
|
+
{% endifalsy %}
|
144
|
+
```
|
145
|
+
|
146
|
+
## Final notes
|
129
147
|
These examples show how you can use the features of `jekyll-uj-powertools` in your Jekyll site.
|
130
148
|
|
131
149
|
## 🔧 Development
|
@@ -133,16 +151,20 @@ After checking out the repo, run `bin/setup` to install dependencies. You can al
|
|
133
151
|
|
134
152
|
To install this gem onto your local machine, run `bundle exec rake install`.
|
135
153
|
|
136
|
-
To release a new version, update the version number in the `.gemspec` and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
137
|
-
|
138
154
|
## ⚠️ Testing
|
139
155
|
Run the tests
|
140
156
|
```shell
|
141
|
-
bundle install
|
142
|
-
|
157
|
+
bundle install && bundle exec rspec
|
158
|
+
```
|
159
|
+
|
160
|
+
Test in your [Ultimate Jekyll Site](http://github.com/itw-creative-works/ultimate-jekyll)
|
161
|
+
```shell
|
162
|
+
npm start -- --ujPluginDevMode=true
|
143
163
|
```
|
144
164
|
|
145
165
|
## 💎 Build + Publish the Gem
|
166
|
+
To release a new version, update the version number in the `.gemspec` and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
167
|
+
|
146
168
|
```shell
|
147
169
|
# Release
|
148
170
|
bundle exec rake release
|
data/lib/filters/main.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# Libraries
|
2
2
|
# ...
|
3
3
|
|
4
|
-
#
|
4
|
+
# Generator
|
5
5
|
module Jekyll
|
6
6
|
class InjectData < Generator
|
7
7
|
safe true
|
@@ -72,6 +72,39 @@ module Jekyll
|
|
72
72
|
]
|
73
73
|
end
|
74
74
|
|
75
|
+
def get_layout_chain(layout_name, site)
|
76
|
+
chain = []
|
77
|
+
current_layout_name = layout_name
|
78
|
+
|
79
|
+
# Traverse up the layout hierarchy
|
80
|
+
while current_layout_name
|
81
|
+
layout = site.layouts[current_layout_name]
|
82
|
+
break unless layout
|
83
|
+
|
84
|
+
chain.unshift(layout) # Add to beginning to maintain parent->child order
|
85
|
+
current_layout_name = layout.data['layout']
|
86
|
+
end
|
87
|
+
|
88
|
+
chain
|
89
|
+
end
|
90
|
+
|
91
|
+
def filter_front_matter(data)
|
92
|
+
# Jekyll internal properties that shouldn't be in resolved data
|
93
|
+
jekyll_internals = [
|
94
|
+
'layout', 'permalink', 'published', 'date', 'categories', 'tags',
|
95
|
+
'path', 'relative_path', 'collection', 'type', 'id', 'url',
|
96
|
+
'next', 'previous', 'draft', 'ext', 'excerpt', 'output'
|
97
|
+
]
|
98
|
+
|
99
|
+
filtered = {}
|
100
|
+
data.each do |key, value|
|
101
|
+
next if jekyll_internals.include?(key)
|
102
|
+
filtered[key] = value
|
103
|
+
end
|
104
|
+
|
105
|
+
filtered
|
106
|
+
end
|
107
|
+
|
75
108
|
def inject_data(item, site)
|
76
109
|
# Inject a random number into the item's data
|
77
110
|
item.data['random_id'] = rand(100) # Random number between 0 and 99
|
@@ -82,8 +115,8 @@ module Jekyll
|
|
82
115
|
end
|
83
116
|
|
84
117
|
# Set resolved data for site, layout, and page
|
85
|
-
# Create a deep merge of site ->
|
86
|
-
# Priority: page (highest) ->
|
118
|
+
# Create a deep merge of site -> child layouts -> parent layouts -> page data
|
119
|
+
# Priority: page (highest) -> parent layouts -> child layouts -> site (lowest)
|
87
120
|
resolved = {}
|
88
121
|
|
89
122
|
# Start with site data
|
@@ -93,19 +126,31 @@ module Jekyll
|
|
93
126
|
resolved = deep_merge(resolved, filtered_config)
|
94
127
|
end
|
95
128
|
|
96
|
-
# Merge layout data if available
|
129
|
+
# Merge layout data if available (traverse the entire layout chain)
|
97
130
|
if item.data['layout']
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
131
|
+
layout_chain = get_layout_chain(item.data['layout'], site)
|
132
|
+
|
133
|
+
# Merge each layout in reverse order (child to parent)
|
134
|
+
# This gives parent layouts (base layouts) higher priority
|
135
|
+
layout_chain.reverse.each do |layout|
|
136
|
+
if layout && layout.data
|
137
|
+
# Filter out Jekyll internal layout properties
|
138
|
+
layout_data = filter_front_matter(layout.data)
|
139
|
+
resolved = deep_merge(resolved, layout_data)
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
# Also add layout_data for backward compatibility (immediate layout only)
|
144
|
+
immediate_layout = site.layouts[item.data['layout']]
|
145
|
+
if immediate_layout && immediate_layout.data
|
146
|
+
item.data['layout_data'] = immediate_layout.data
|
104
147
|
end
|
105
148
|
end
|
106
149
|
|
107
150
|
# Finally merge page data (highest priority)
|
108
|
-
|
151
|
+
# Filter out Jekyll internal properties
|
152
|
+
page_data = filter_front_matter(item.data)
|
153
|
+
resolved = deep_merge(resolved, page_data)
|
109
154
|
|
110
155
|
# Add the resolved data to the item
|
111
156
|
item.data['resolved'] = resolved
|
data/lib/jekyll-uj-powertools.rb
CHANGED
data/lib/tags/iffalsy.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# Libraries
|
2
|
+
# ...
|
3
|
+
|
4
|
+
# Tag
|
5
|
+
module Jekyll
|
6
|
+
module UJPowertools
|
7
|
+
class IfFalsyTag < Liquid::Block
|
8
|
+
def initialize(tag_name, markup, tokens)
|
9
|
+
super
|
10
|
+
@variable = markup.strip
|
11
|
+
end
|
12
|
+
|
13
|
+
def render(context)
|
14
|
+
# Use Liquid's variable lookup to handle nested properties
|
15
|
+
value = context.scopes.last[@variable] || context[@variable]
|
16
|
+
|
17
|
+
# For nested properties like page.my.variable
|
18
|
+
if @variable.include?('.')
|
19
|
+
parts = @variable.split('.')
|
20
|
+
value = context[parts.first]
|
21
|
+
parts[1..-1].each do |part|
|
22
|
+
value = value.is_a?(Hash) ? value[part] : nil
|
23
|
+
break if value.nil?
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# Check if the value is falsy (nil, false, empty string, or 0)
|
28
|
+
if value.nil? || value == false || value == "" || value == 0
|
29
|
+
super
|
30
|
+
else
|
31
|
+
""
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
Liquid::Template.register_tag('iffalsy', Jekyll::UJPowertools::IfFalsyTag)
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# Libraries
|
2
|
+
# ...
|
3
|
+
|
4
|
+
# Tag
|
5
|
+
module Jekyll
|
6
|
+
module UJPowertools
|
7
|
+
class IfTruthyTag < Liquid::Block
|
8
|
+
def initialize(tag_name, markup, tokens)
|
9
|
+
super
|
10
|
+
@variable = markup.strip
|
11
|
+
end
|
12
|
+
|
13
|
+
def render(context)
|
14
|
+
# Use Liquid's variable lookup to handle nested properties
|
15
|
+
value = context.scopes.last[@variable] || context[@variable]
|
16
|
+
|
17
|
+
# For nested properties like page.my.variable
|
18
|
+
if @variable.include?('.')
|
19
|
+
parts = @variable.split('.')
|
20
|
+
value = context[parts.first]
|
21
|
+
parts[1..-1].each do |part|
|
22
|
+
value = value.is_a?(Hash) ? value[part] : nil
|
23
|
+
break if value.nil?
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# Check if the value is truthy (not nil, not false, not empty string, not 0)
|
28
|
+
if value && value != false && value != "" && value != 0
|
29
|
+
super
|
30
|
+
else
|
31
|
+
""
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
Liquid::Template.register_tag('iftruthy', Jekyll::UJPowertools::IfTruthyTag)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jekyll-uj-powertools
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ITW Creative Works
|
@@ -103,7 +103,8 @@ files:
|
|
103
103
|
- lib/generators/inject-properties.rb
|
104
104
|
- lib/hooks/inject-properties.rb
|
105
105
|
- lib/jekyll-uj-powertools.rb
|
106
|
-
- lib/tags/
|
106
|
+
- lib/tags/iffalsy.rb
|
107
|
+
- lib/tags/iftruthy.rb
|
107
108
|
homepage: https://github.com/itw-creative-works/jekyll-uj-powertools
|
108
109
|
licenses:
|
109
110
|
- MIT
|
data/lib/tags/ifistruthy.rb
DELETED
@@ -1,161 +0,0 @@
|
|
1
|
-
module Jekyll
|
2
|
-
module UJPowertools
|
3
|
-
class IfIsTruthyTag < Liquid::Block
|
4
|
-
Syntax = /(\w+)/
|
5
|
-
|
6
|
-
def initialize(tag_name, markup, tokens)
|
7
|
-
super
|
8
|
-
|
9
|
-
if markup =~ Syntax
|
10
|
-
@variable_name = $1
|
11
|
-
else
|
12
|
-
raise SyntaxError, "Invalid syntax for ifistruthy tag. Usage: {% ifistruthy variable_name %}"
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
def render(context)
|
17
|
-
variable = context[@variable_name]
|
18
|
-
is_truthy = check_truthy(variable)
|
19
|
-
|
20
|
-
# Split content at else tag
|
21
|
-
else_index = nil
|
22
|
-
@nodelist.each_with_index do |node, index|
|
23
|
-
if node.respond_to?(:tag_name) && node.tag_name == 'else'
|
24
|
-
else_index = index
|
25
|
-
break
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
if is_truthy
|
30
|
-
if else_index
|
31
|
-
render_nodelist(@nodelist[0...else_index], context)
|
32
|
-
else
|
33
|
-
super(context)
|
34
|
-
end
|
35
|
-
else
|
36
|
-
if else_index
|
37
|
-
render_nodelist(@nodelist[(else_index + 1)..-1], context)
|
38
|
-
else
|
39
|
-
''
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
def unknown_tag(tag_name, markup, tokens)
|
45
|
-
if tag_name == 'else'
|
46
|
-
@nodelist << Liquid::ElseTag.new(tag_name, markup, tokens)
|
47
|
-
else
|
48
|
-
super
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
private
|
53
|
-
|
54
|
-
def check_truthy(value)
|
55
|
-
return false if value.nil?
|
56
|
-
return false if value.respond_to?(:empty?) && value.empty?
|
57
|
-
return false if value.to_s.downcase == 'null'
|
58
|
-
return false if value == false
|
59
|
-
true
|
60
|
-
end
|
61
|
-
|
62
|
-
def render_nodelist(nodelist, context)
|
63
|
-
output = []
|
64
|
-
nodelist.each do |token|
|
65
|
-
case token
|
66
|
-
when String
|
67
|
-
output << token
|
68
|
-
else
|
69
|
-
if token.respond_to?(:render)
|
70
|
-
output << token.render(context)
|
71
|
-
else
|
72
|
-
output << token.to_s
|
73
|
-
end
|
74
|
-
end
|
75
|
-
end
|
76
|
-
output.join
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
class UnlessIsTruthyTag < Liquid::Block
|
81
|
-
Syntax = /(\w+)/
|
82
|
-
|
83
|
-
def initialize(tag_name, markup, tokens)
|
84
|
-
super
|
85
|
-
|
86
|
-
if markup =~ Syntax
|
87
|
-
@variable_name = $1
|
88
|
-
else
|
89
|
-
raise SyntaxError, "Invalid syntax for unlessistruthy tag. Usage: {% unlessistruthy variable_name %}"
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
def render(context)
|
94
|
-
variable = context[@variable_name]
|
95
|
-
is_truthy = check_truthy(variable)
|
96
|
-
|
97
|
-
# Split content at else tag
|
98
|
-
else_index = nil
|
99
|
-
@nodelist.each_with_index do |node, index|
|
100
|
-
if node.respond_to?(:tag_name) && node.tag_name == 'else'
|
101
|
-
else_index = index
|
102
|
-
break
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
if !is_truthy
|
107
|
-
if else_index
|
108
|
-
render_nodelist(@nodelist[0...else_index], context)
|
109
|
-
else
|
110
|
-
super(context)
|
111
|
-
end
|
112
|
-
else
|
113
|
-
if else_index
|
114
|
-
render_nodelist(@nodelist[(else_index + 1)..-1], context)
|
115
|
-
else
|
116
|
-
''
|
117
|
-
end
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
|
-
def unknown_tag(tag_name, markup, tokens)
|
122
|
-
if tag_name == 'else'
|
123
|
-
@nodelist << Liquid::ElseTag.new(tag_name, markup, tokens)
|
124
|
-
else
|
125
|
-
super
|
126
|
-
end
|
127
|
-
end
|
128
|
-
|
129
|
-
private
|
130
|
-
|
131
|
-
def check_truthy(value)
|
132
|
-
return false if value.nil?
|
133
|
-
return false if value.respond_to?(:empty?) && value.empty?
|
134
|
-
return false if value.to_s.downcase == 'null'
|
135
|
-
return false if value == false
|
136
|
-
true
|
137
|
-
end
|
138
|
-
|
139
|
-
def render_nodelist(nodelist, context)
|
140
|
-
output = []
|
141
|
-
nodelist.each do |token|
|
142
|
-
case token
|
143
|
-
when String
|
144
|
-
output << token
|
145
|
-
else
|
146
|
-
if token.respond_to?(:render)
|
147
|
-
output << token.render(context)
|
148
|
-
else
|
149
|
-
output << token.to_s
|
150
|
-
end
|
151
|
-
end
|
152
|
-
end
|
153
|
-
output.join
|
154
|
-
end
|
155
|
-
end
|
156
|
-
end
|
157
|
-
end
|
158
|
-
|
159
|
-
# Register the tags
|
160
|
-
Liquid::Template.register_tag('ifistruthy', Jekyll::UJPowertools::IfIsTruthyTag)
|
161
|
-
Liquid::Template.register_tag('unlessistruthy', Jekyll::UJPowertools::UnlessIsTruthyTag)
|