Add logic to exclude some pages based on some tags

This commit is contained in:
Clément Fournier
2018-05-21 09:41:41 +02:00
parent bb51792ba7
commit 918a812d92
3 changed files with 27 additions and 20 deletions

View File

@ -1,13 +1,23 @@
<!-- Argument summary: -->
<!-- tags: tag determining the pages included in the panel -->
<!-- tags: comma-separated tags determining the pages included in the panel -->
<!-- except_tags: comma-separated tags to filter out some pages -->
<!-- A page is included if it has all the include tags and none of the exclude tags -->
<!-- title: Display title of the panel -->
<!-- description: Description of the panel -->
<!-- datagroups: json array containing the data groups for the shuffle logic -->
<!-- image (optional) : Name of the image to use, as a css class, eg "fa-list" -->
<!-- titlemaker (optional) : name of a variable that's evaluated to determine the displayed title of the page. Default is page.title -->
{% capture titlemaker %} {{ include.titlemaker | default: "page.title" }} {% endcapture %}
{% assign include_tags = {{ include.tags | split: "," }} %}
{% assign exclude_tags = {{ include.except_tags | split: "," }} %}
<div class="col-xs-6 col-sm-4 col-md-4" data-groups='{{ include.datagroups }}'>
<div class="panel panel-default">
@ -30,9 +40,10 @@
</p>
<ul>
{% for page in site.pages %}
{% capture alltags %}{{ page.tags | contains_all: include.tags }}{% endcapture %}
<!-- ugly, but one can only capture string values -->
{% if alltags contains "true" %}
{% capture included %}{{ page.tags | intersect: include_tags | equals: include_tags }}{% endcapture %}
{% capture excluded %}{{ page.tags | intersect: exclude_tags | empty }}{% endcapture %}
{% if included contains "true" and excluded contains "true" %}
<li><a href="{{page.url | remove: '/'}}">{% eval titlemaker %}</a></li>
{% endif %}
{% endfor %}

View File

@ -1,27 +1,24 @@
module CustomFilters
def contains_all(input, test)
if !test
!input
elsif !input
false
def intersect(xs, ys)
if !xs || !ys
[]
else
test.split(",").all? {|val| input.include?(val)}
Array(xs) & Array(ys)
end
end
def intersect(xs, ys)
xs & ys
def equals(xs, ys)
a = Array(xs)
b = Array(ys)
((a | b) - (a & b)).empty?
end
def union(xs, ys)
xs | ys
def empty(xs)
Array(xs).empty?
end
def diff(xs, ys)
xs - ys
end
end
Liquid::Template.register_filter(CustomFilters)

View File

@ -1,6 +1,5 @@
# This tag takes a variable name as an input, and evaluates its value twice
# (follows one indirection)
# This tag takes a variable name as an input, and evaluates its value twice (dereferences it once)
# E.g. if the symbol table is E = {"foo" => "bar", "bar" => "baz"},
# then {% eval foo %} ~> E[E["foo"]] ~> E["bar"] ~> "baz"