Skip to content

Commit

Permalink
change list filter behavior and make associated plugin changes
Browse files Browse the repository at this point in the history
  • Loading branch information
vincerubinetti committed Aug 7, 2024
1 parent 697129c commit 1a795e3
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 21 deletions.
3 changes: 1 addition & 2 deletions _includes/button.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{% assign button = include %}
{% assign button = button | hash_default: site.data.types[include.type] %}
{% assign button = include | hash_default: site.data.types[include.type] %}

{% if button.link or button.icon or button.text %}
<div class="button-wrapper">
Expand Down
2 changes: 1 addition & 1 deletion _includes/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{% assign data = site.data[include.data]
| default: site[include.data]
| default: emptyarray
| data_filter: include.filters
| data_filter: include.filter
%}

{% assign years = data
Expand Down
2 changes: 1 addition & 1 deletion _plugins/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Jekyll
module HashFilters
# merge main hash with another hash of defaults
def hash_default(hash, defaults)
if not hash.is_a?(Hash) or not defaults.is_a?(Hash)
if not defaults.is_a?(Hash)
return hash
end
defaults.each do |key, value|
Expand Down
56 changes: 43 additions & 13 deletions _plugins/misc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,52 @@ def object_items(object)
end

# filter a list of hashes by comma-sep'd field:value pairs
def data_filter(data, filters)
if not data.is_a?(Array) or not filters.is_a?(String)
def empty_binding
binding
end

# make arbitrary string into valid ruby variable name
def safe_var_name(name)
return name.to_s.gsub /[^a-z]+/, "_"
end

# jekyll ruby plugin
def data_filter(data, filter)
if not filter.is_a?(String)
return data
end
data = data.clone
for filter in array_filter(filters.split(","))
key, value = array_filter(filter.split(":"))
# find unspecified fields
if value == nil
data.select!{|d| d[key] == nil}
# find fields that match regex
elsif value.is_a?(String)
data.select!{|d| d[key].to_s =~ /#{value}/m}

# filter data
return data.clone.select{
|item|
# start with empty context of local variables
b = empty_binding
# add item as local variable
b.local_variable_set("item", item)
# if jekyll doc collection, get hash of doc data
if item.is_a? Jekyll::Document
item = item.data
end
end
return data
# also set each item field as local variable when evaluating filter
item.each do |var, val|
b.local_variable_set(safe_var_name(var), val)
end
# whether to keep item
keep = true
while true
begin
# evaluate filter code, coerce to true/false
keep = !!eval(filter, b)
# if no error, done
break
rescue NameError => e
# if var in filter doesn't exist, define it and re-evaluate
b.local_variable_set(safe_var_name(e.name), nil)
end
end
# keep/discard item
keep
}
end

# from css text, find font family definitions and construct google font url
Expand Down
4 changes: 2 additions & 2 deletions projects/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliqu

## Featured

{% include list.html component="card" data="projects" filters="group: featured" %}
{% include list.html component="card" data="projects" filter="group == 'featured'" %}

{% include section.html %}

## More

{% include list.html component="card" data="projects" filters="group: " style="small" %}
{% include list.html component="card" data="projects" filter="!group" style="small" %}
4 changes: 2 additions & 2 deletions team/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

{% include section.html %}

{% include list.html data="members" component="portrait" filters="role: pi" %}
{% include list.html data="members" component="portrait" filters="role: ^(?!pi$)" %}
{% include list.html data="members" component="portrait" filter="role == 'pi'" %}
{% include list.html data="members" component="portrait" filter="role != 'pi'" %}

{% include section.html background="images/background.jpg" dark=true %}

Expand Down

0 comments on commit 1a795e3

Please sign in to comment.