module Jekyll::Filters

def array_to_sentence_string(array)

Returns the formatted String.

# => "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 cgi_escape(input)

Returns the escaped String.

# => "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)

Returns the formatted String.

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)

Returns the formatted String.

# => "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)

Returns the formatting String.

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)

Returns the formatted String.

# => "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)

"items" => [...] } # all the items where `property` == "larry"
{"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 item_property(item, property)

def item_property(item, property)
  if item.respond_to?(:data)
    item.data[property.to_s]
  else
    item[property.to_s]
  end
end

def jsonify(input)

Returns the converted json string

input - The Array or Hash to be converted

Convert the input into json string
def jsonify(input)
  input.to_json
end

def markdownify(input)

Returns the HTML formatted String.

input - The Markdown String to convert.

Convert a Markdown string into HTML output.
def markdownify(input)
  site = @context.registers[:site]
  converter = site.getConverterImpl(Jekyll::Converters::Markdown)
  converter.convert(input)
end

def number_of_words(input)

Returns the Integer word count.

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 textilize(input)

Returns the HTML formatted String.

input - The Textile String to convert.

Convert a Textile string into HTML output.
def textilize(input)
  site = @context.registers[:site]
  converter = site.getConverterImpl(Jekyll::Converters::Textile)
  converter.convert(input)
end

def time(input)

def time(input)
  case input
  when Time
    input
  when String
    Time.parse(input)
  else
    Jekyll.logger.error "Invalid Date:", "'#{input}' is not a valid datetime."
    exit(1)
  end
end

def uri_escape(input)

Returns the escaped String.

# => "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, key, value)

Returns the filtered array of objects

value - desired value
key - key within each object to filter by
input - the object array

Filter an array of objects
def where(input, key, value)
  return input unless input.is_a?(Array)
  input.select { |object| object[key] == value }
end

def xml_escape(input)

Returns the escaped String.

# => "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)
end