module Jekyll::Filters
def array_to_sentence_string(array)
# => "apples, oranges, and grapes"
array_to_sentence_string(["apples", "oranges", "grapes"])
Examples
array - The Array of Strings to join.
word "and" for the last one.
Join an array of things into a string by separating with commas and the
def array_to_sentence_string(array) connector = "and" case array.length when 0 "" when 1 array[0].to_s when 2 "#{array[0]} #{connector} #{array[1]}" else "#{array[0...-1].join(', ')}, #{connector} #{array[-1]}" end end
def as_liquid(item)
def as_liquid(item) case item when Hash pairs = item.map { |k, v| as_liquid([k, v]) } Hash[pairs] when Array item.map { |i| as_liquid(i) } else if item.respond_to?(:to_liquid) liquidated = item.to_liquid # prevent infinite recursion for simple types (which return `self`) if liquidated == item item else as_liquid(liquidated) end else item end end end
def cgi_escape(input)
# => "foo%2Cbar%3Bbaz%3F"
cgi_escape('foo,bar;baz?')
Examples
input - The String to escape.
with appropriate %XX replacements.
CGI escape a string for use in a URL. Replaces any special characters
def cgi_escape(input) CGI::escape(input) end
def date_to_long_string(date)
date - The Time to format.
Format a date in long format e.g. "27 January 2011".
def date_to_long_string(date) time(date).strftime("%d %B %Y") end
def date_to_rfc822(date)
# => "Sun, 24 Apr 2011 12:34:46 +0000"
date_to_rfc822(Time.now)
Examples
date - The Time to format.
Format a date according to RFC-822
def date_to_rfc822(date) time(date).rfc822 end
def date_to_string(date)
date - the Time to format.
Format a date in short format e.g. "27 Jan 2011".
def date_to_string(date) time(date).strftime("%d %b %Y") end
def date_to_xmlschema(date)
# => "2011-04-24T20:34:46+08:00"
date_to_xmlschema(Time.now)
Examples
date - The Time to format.
Format a date for use in XML.
def date_to_xmlschema(date) time(date).xmlschema end
def group_by(input, property)
{"name" => "larry"
Returns an array of Hashes, each looking something like this:
property - the property
input - the inputted Enumerable
Group an array of items by a property
def group_by(input, property) if groupable?(input) input.group_by do |item| item_property(item, property).to_s end.inject([]) do |memo, i| memo << { "name" => i.first, "items" => i.last } end else input end end
def groupable?(element)
def groupable?(element) element.respond_to?(:group_by) end
def inspect(input)
input - The Object to be converted
Convert an object into its String representation for debugging
def inspect(input) CGI.escapeHTML(input.inspect) end
def item_property(item, property)
def item_property(item, property) if item.respond_to?(:to_liquid) item.to_liquid[property.to_s] elsif item.respond_to?(:data) item.data[property.to_s] else item[property.to_s] end end
def jsonify(input)
input - The Array or Hash to be converted
Convert the input into json string
def jsonify(input) as_liquid(input).to_json end
def markdownify(input)
input - The Markdown String to convert.
Convert a Markdown string into HTML output.
def markdownify(input) site = @context.registers[:site] converter = site.find_converter_instance(Jekyll::Converters::Markdown) converter.convert(input) end
def number_of_words(input)
input - The String on which to operate.
Count the number of words in the input string.
def number_of_words(input) input.split.length end
def pop(array, input = 1)
def pop(array, input = 1) return array unless array.is_a?(Array) new_ary = array.dup new_ary.pop(input.to_i || 1) new_ary end
def push(array, input)
def push(array, input) return array unless array.is_a?(Array) new_ary = array.dup new_ary.push(input) new_ary end
def sample(input, num = 1)
def sample(input, num = 1) return input unless input.respond_to?(:sample) n = num.to_i rescue 1 if n == 1 input.sample else input.sample(n) end end
def sassify(input)
input - The Sass String to convert.
Convert a Sass string into CSS output.
def sassify(input) site = @context.registers[:site] converter = site.find_converter_instance(Jekyll::Converters::Sass) converter.convert(input) end
def scssify(input)
input - The Scss String to convert.
Convert a Scss string into CSS output.
def scssify(input) site = @context.registers[:site] converter = site.find_converter_instance(Jekyll::Converters::Scss) converter.convert(input) end
def shift(array, input = 1)
def shift(array, input = 1) return array unless array.is_a?(Array) new_ary = array.dup new_ary.shift(input.to_i || 1) new_ary end
def slugify(input, mode=nil)
Returns the given filename or title as a lowercase URL String.
mode - how string is slugified
input - The filename or title to slugify.
Slugify a filename or title.
def slugify(input, mode=nil) Utils.slugify(input, :mode => mode) end
def smartify(input)
input - The Markdown String to convert.
Convert a Markdown string into HTML output.
def smartify(input) site = @context.registers[:site] converter = site.find_converter_instance(Jekyll::Converters::SmartyPants) converter.convert(input) end
def sort(input, property = nil, nils = "first")
nils ('first' | 'last') - nils appear before or after non-nil values
property - property within each object to filter by
input - the object array
Sort an array of objects
def sort(input, property = nil, nils = "first") if input.nil? raise ArgumentError.new("Cannot sort a null object.") end if property.nil? input.sort else case when nils == "first" order = - 1 when nils == "last" order = + 1 else raise ArgumentError.new("Invalid nils order: " \ "'#{nils}' is not a valid nils order. It must be 'first' or 'last'.") end input.sort do |apple, orange| apple_property = item_property(apple, property) orange_property = item_property(orange, property) if !apple_property.nil? && orange_property.nil? - order elsif apple_property.nil? && !orange_property.nil? + order else apple_property <=> orange_property end end end end
def time(input)
def time(input) case input when Time input when Date input.to_time when String Time.parse(input) rescue Time.at(input.to_i) when Numeric Time.at(input) else Jekyll.logger.error "Invalid Date:", "'#{input}' is not a valid datetime." exit(1) end.localtime end
def unshift(array, input)
def unshift(array, input) return array unless array.is_a?(Array) new_ary = array.dup new_ary.unshift(input) new_ary end
def uri_escape(input)
# => "foo,%20bar%20%5Cbaz?"
uri_escape('foo, bar \\baz?')
Examples
input - The String to escape.
URI escape a string.
def uri_escape(input) URI.escape(input) end
def where(input, property, value)
value - desired value
property - property within each object to filter by
input - the object array
Filter an array of objects
def where(input, property, value) return input unless input.is_a?(Enumerable) input = input.values if input.is_a?(Hash) input.select { |object| item_property(object, property).to_s == value.to_s } end
def xml_escape(input)
# => "foo "bar" <baz>"
xml_escape('foo "bar"
Examples
input - The String to escape.
appropriate HTML entity replacements.
XML escape a string for use. Replaces any special characters with
def xml_escape(input) CGI.escapeHTML(input.to_s) end