Created
July 29, 2016 13:31
-
-
Save Jetroid/31949b08e4aa1c2c811bdf70f5493a57 to your computer and use it in GitHub Desktop.
A plugin to allow union of arrays with Jekyll.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Jekyll | |
module UnionFilter | |
def union(input, array) | |
unless array.respond_to?(:to_ary) | |
array = Array.new | |
end | |
InputIterator.new(input).concat(array).uniq | |
end | |
class InputIterator | |
include Enumerable | |
def initialize(input) | |
@input = if input.is_a?(Array) | |
input.flatten | |
elsif input.is_a?(Hash) | |
[input] | |
elsif input.is_a?(Enumerable) | |
input | |
else | |
Array(input) | |
end | |
end | |
def concat(args) | |
to_a.concat(args) | |
end | |
def each | |
@input.each do |e| | |
yield(e.respond_to?(:to_liquid) ? e.to_liquid : e) | |
end | |
end | |
end | |
end | |
end | |
Liquid::Template.register_filter(Jekyll::UnionFilter) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment