Files

Class/Module Index [+]

Quicksearch

Jekyll::Filters

Public Instance Methods

array_to_sentence_string(array) click to toggle source

Join an array of things into a string by separating with commas and the word “and” for the last one.

array - The Array of Strings to join.

Examples

array_to_sentence_string(["apples", "oranges", "grapes"])
# => "apples, oranges, and grapes"

Returns the formatted String.

# File lib/jekyll/filters.rb, line 160
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
cgi_escape(input) click to toggle source

CGI escape a string for use in a URL. Replaces any special characters with appropriate %XX replacements.

input - The String to escape.

Examples

cgi_escape('foo,bar;baz?')
# => "foo%2Cbar%3Bbaz%3F"

Returns the escaped String.

# File lib/jekyll/filters.rb, line 122
def cgi_escape(input)
  CGI::escape(input)
end
date_to_long_string(date) click to toggle source

Format a date in long format e.g. “27 January 2011”.

date - The Time to format.

Returns the formatted String.

# File lib/jekyll/filters.rb, line 64
def date_to_long_string(date)
  time(date).strftime("%d %B %Y")
end
date_to_rfc822(date) click to toggle source

Format a date according to RFC-822

date - The Time to format.

Examples

date_to_rfc822(Time.now)
# => "Sun, 24 Apr 2011 12:34:46 +0000"

Returns the formatted String.

# File lib/jekyll/filters.rb, line 92
def date_to_rfc822(date)
  time(date).rfc822
end
date_to_string(date) click to toggle source

Format a date in short format e.g. “27 Jan 2011”.

date - the Time to format.

Returns the formatting String.

# File lib/jekyll/filters.rb, line 55
def date_to_string(date)
  time(date).strftime("%d %b %Y")
end
date_to_xmlschema(date) click to toggle source

Format a date for use in XML.

date - The Time to format.

Examples

date_to_xmlschema(Time.now)
# => "2011-04-24T20:34:46+08:00"

Returns the formatted String.

# File lib/jekyll/filters.rb, line 78
def date_to_xmlschema(date)
  time(date).xmlschema
end
group_by(input, property) click to toggle source

Group an array of items by a property

input - the inputted Enumerable property - the property

Returns an array of Hashes, each looking something like this:

{"name"  => "larry"
 "items" => [...] } # all the items where `property` == "larry"
# File lib/jekyll/filters.rb, line 191
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
jsonify(input) click to toggle source

Convert the input into json string

input - The Array or Hash to be converted

Returns the converted json string

# File lib/jekyll/filters.rb, line 179
def jsonify(input)
  as_liquid(input).to_json
end
markdownify(input) click to toggle source

Convert a Markdown string into HTML output.

input - The Markdown String to convert.

Returns the HTML formatted String.

# File lib/jekyll/filters.rb, line 22
def markdownify(input)
  site = @context.registers[:site]
  converter = site.getConverterImpl(Jekyll::Converters::Markdown)
  converter.convert(input)
end
number_of_words(input) click to toggle source

Count the number of words in the input string.

input - The String on which to operate.

Returns the Integer word count.

# File lib/jekyll/filters.rb, line 145
def number_of_words(input)
  input.split.length
end
sassify(input) click to toggle source

Convert a Sass string into CSS output.

input - The Sass String to convert.

Returns the CSS formatted String.

# File lib/jekyll/filters.rb, line 33
def sassify(input)
  site = @context.registers[:site]
  converter = site.getConverterImpl(Jekyll::Converters::Sass)
  converter.convert(input)
end
scssify(input) click to toggle source

Convert a Scss string into CSS output.

input - The Scss String to convert.

Returns the CSS formatted String.

# File lib/jekyll/filters.rb, line 44
def scssify(input)
  site = @context.registers[:site]
  converter = site.getConverterImpl(Jekyll::Converters::Scss)
  converter.convert(input)
end
sort(input, property = nil, nils = "first") click to toggle source

Sort an array of objects

input - the object array property - property within each object to filter by nils (‘first’ | ‘last’) - nils appear before or after non-nil values

Returns the filtered array of objects

# File lib/jekyll/filters.rb, line 222
def sort(input, property = nil, nils = "first")
  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 { |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
textilize(input) click to toggle source

Convert a Textile string into HTML output.

input - The Textile String to convert.

Returns the HTML formatted String.

# File lib/jekyll/filters.rb, line 11
def textilize(input)
  site = @context.registers[:site]
  converter = site.getConverterImpl(Jekyll::Converters::Textile)
  converter.convert(input)
end
uri_escape(input) click to toggle source

URI escape a string.

input - The String to escape.

Examples

uri_escape('foo, bar \\baz?')
# => "foo,%20bar%20%5Cbaz?"

Returns the escaped String.

# File lib/jekyll/filters.rb, line 136
def uri_escape(input)
  URI.escape(input)
end
where(input, property, value) click to toggle source

Filter an array of objects

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

Returns the filtered array of objects

# File lib/jekyll/filters.rb, line 210
def where(input, property, value)
  return input unless input.is_a?(Array)
  input.select { |object| item_property(object, property) == value }
end
xml_escape(input) click to toggle source

XML escape a string for use. Replaces any special characters with appropriate HTML entity replacements.

input - The String to escape.

Examples

xml_escape('foo "bar" <baz>')
# => "foo &quot;bar&quot; &lt;baz&gt;"

Returns the escaped String.

# File lib/jekyll/filters.rb, line 107
def xml_escape(input)
  CGI.escapeHTML(input.to_s)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.