Module: Jekyll::Filters

Includes:
GroupingFilters, URLFilters
Defined in:
lib/jekyll/filters.rb,
lib/jekyll/filters/url_filters.rb,
lib/jekyll/filters/grouping_filters.rb

Defined Under Namespace

Modules: GroupingFilters, URLFilters

Instance Method Summary collapse

Methods included from GroupingFilters

#group_by, #group_by_exp

Methods included from URLFilters

#absolute_url, #relative_url, #strip_index

Instance Method Details

#array_to_sentence_string(array, connector = "and") ⇒ Object

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. connector - Word used to connect the last 2 items in the array

Examples

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

Returns the formatted String.



193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/jekyll/filters.rb', line 193

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) ⇒ Object

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.



145
146
147
# File 'lib/jekyll/filters.rb', line 145

def cgi_escape(input)
  CGI.escape(input)
end

#date_to_long_string(date) ⇒ Object

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

date - The Time to format.

Returns the formatted String.



84
85
86
87
# File 'lib/jekyll/filters.rb', line 84

def date_to_long_string(date)
  return date if date.to_s.empty?
  time(date).strftime("%d %B %Y")
end

#date_to_rfc822(date) ⇒ Object

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.



114
115
116
117
# File 'lib/jekyll/filters.rb', line 114

def date_to_rfc822(date)
  return date if date.to_s.empty?
  time(date).rfc822
end

#date_to_string(date) ⇒ Object

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

date - the Time to format.

Returns the formatting String.



75
76
77
# File 'lib/jekyll/filters.rb', line 75

def date_to_string(date)
  time(date).strftime("%d %b %Y")
end

#date_to_xmlschema(date) ⇒ Object

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.



99
100
101
102
# File 'lib/jekyll/filters.rb', line 99

def date_to_xmlschema(date)
  return date if date.to_s.empty?
  time(date).xmlschema
end

#inspect(input) ⇒ Object

Convert an object into its String representation for debugging

input - The Object to be converted

Returns a String representation of the object.



333
334
335
# File 'lib/jekyll/filters.rb', line 333

def inspect(input)
  xml_escape(input.inspect)
end

#jsonify(input) ⇒ Object

Convert the input into json string

input - The Array or Hash to be converted

Returns the converted json string



211
212
213
# File 'lib/jekyll/filters.rb', line 211

def jsonify(input)
  as_liquid(input).to_json
end

#markdownify(input) ⇒ Object

Convert a Markdown string into HTML output.

input - The Markdown String to convert.

Returns the HTML formatted String.



20
21
22
23
24
# File 'lib/jekyll/filters.rb', line 20

def markdownify(input)
  site = @context.registers[:site]
  converter = site.find_converter_instance(Jekyll::Converters::Markdown)
  converter.convert(input.to_s)
end

#normalize_whitespace(input) ⇒ Object

Replace any whitespace in the input string with a single space

input - The String on which to operate.

Returns the formatted String



168
169
170
# File 'lib/jekyll/filters.rb', line 168

def normalize_whitespace(input)
  input.to_s.gsub(%r!\s+!, " ").strip
end

#number_of_words(input) ⇒ Object

Count the number of words in the input string.

input - The String on which to operate.

Returns the Integer word count.



177
178
179
# File 'lib/jekyll/filters.rb', line 177

def number_of_words(input)
  input.split.length
end

#pop(array, num = 1) ⇒ Object



288
289
290
291
292
293
294
# File 'lib/jekyll/filters.rb', line 288

def pop(array, num = 1)
  return array unless array.is_a?(Array)
  num = Liquid::Utils.to_integer(num)
  new_ary = array.dup
  new_ary.pop(num)
  new_ary
end

#push(array, input) ⇒ Object



296
297
298
299
300
301
# File 'lib/jekyll/filters.rb', line 296

def push(array, input)
  return array unless array.is_a?(Array)
  new_ary = array.dup
  new_ary.push(input)
  new_ary
end

#sample(input, num = 1) ⇒ Object



318
319
320
321
322
323
324
325
326
# File 'lib/jekyll/filters.rb', line 318

def sample(input, num = 1)
  return input unless input.respond_to?(:sample)
  num = Liquid::Utils.to_integer(num) rescue 1
  if num == 1
    input.sample
  else
    input.sample(num)
  end
end

#sassify(input) ⇒ Object

Convert a Sass string into CSS output.

input - The Sass String to convert.

Returns the CSS formatted String.



42
43
44
45
46
# File 'lib/jekyll/filters.rb', line 42

def sassify(input)
  site = @context.registers[:site]
  converter = site.find_converter_instance(Jekyll::Converters::Sass)
  converter.convert(input)
end

#scssify(input) ⇒ Object

Convert a Scss string into CSS output.

input - The Scss String to convert.

Returns the CSS formatted String.



53
54
55
56
57
# File 'lib/jekyll/filters.rb', line 53

def scssify(input)
  site = @context.registers[:site]
  converter = site.find_converter_instance(Jekyll::Converters::Scss)
  converter.convert(input)
end

#shift(array, num = 1) ⇒ Object



303
304
305
306
307
308
309
# File 'lib/jekyll/filters.rb', line 303

def shift(array, num = 1)
  return array unless array.is_a?(Array)
  num = Liquid::Utils.to_integer(num)
  new_ary = array.dup
  new_ary.shift(num)
  new_ary
end

#slugify(input, mode = nil) ⇒ Object

Slugify a filename or title.

input - The filename or title to slugify. mode - how string is slugified

Returns the given filename or title as a lowercase URL String. See Utils.slugify for more detail.



66
67
68
# File 'lib/jekyll/filters.rb', line 66

def slugify(input, mode = nil)
  Utils.slugify(input, :mode => mode)
end

#smartify(input) ⇒ Object

Convert quotes into smart quotes.

input - The String to convert.

Returns the smart-quotified String.



31
32
33
34
35
# File 'lib/jekyll/filters.rb', line 31

def smartify(input)
  site = @context.registers[:site]
  converter = site.find_converter_instance(Jekyll::Converters::SmartyPants)
  converter.convert(input.to_s)
end

#sort(input, property = nil, nils = "first") ⇒ Object

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



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/jekyll/filters.rb', line 268

def sort(input, property = nil, nils = "first")
  if input.nil?
    raise ArgumentError, "Cannot sort a null object."
  end
  if property.nil?
    input.sort
  else
    if nils == "first"
      order = - 1
    elsif nils == "last"
      order = + 1
    else
      raise ArgumentError, "Invalid nils order: " \
        "'#{nils}' is not a valid nils order. It must be 'first' or 'last'."
    end

    sort_input(input, property, order)
  end
end

#to_integer(input) ⇒ Object

Convert the input into integer

input - the object string

Returns the integer value



255
256
257
258
259
# File 'lib/jekyll/filters.rb', line 255

def to_integer(input)
  return 1 if input == true
  return 0 if input == false
  input.to_i
end

#unshift(array, input) ⇒ Object



311
312
313
314
315
316
# File 'lib/jekyll/filters.rb', line 311

def unshift(array, input)
  return array unless array.is_a?(Array)
  new_ary = array.dup
  new_ary.unshift(input)
  new_ary
end

#uri_escape(input) ⇒ Object

URI escape a string.

input - The String to escape.

Examples

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

Returns the escaped String.



159
160
161
# File 'lib/jekyll/filters.rb', line 159

def uri_escape(input)
  Addressable::URI.normalize_component(input)
end

#where(input, property, value) ⇒ Object

Filter an array of objects

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

Returns the filtered array of objects



222
223
224
225
226
227
228
# File 'lib/jekyll/filters.rb', line 222

def where(input, property, value)
  return input unless input.respond_to?(:select)
  input = input.values if input.is_a?(Hash)
  input.select do |object|
    Array(item_property(object, property)).map(&:to_s).include?(value.to_s)
  end || []
end

#where_exp(input, variable, expression) ⇒ Object

Filters an array of objects against an expression

input - the object array variable - the variable to assign each item to in the expression expression - a Liquid comparison expression passed in as a string

Returns the filtered array of objects



237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/jekyll/filters.rb', line 237

def where_exp(input, variable, expression)
  return input unless input.respond_to?(:select)
  input = input.values if input.is_a?(Hash) # FIXME

  condition = parse_condition(expression)
  @context.stack do
    input.select do |object|
      @context[variable] = object
      condition.evaluate(@context)
    end
  end || []
end

#xml_escape(input) ⇒ Object

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.



130
131
132
# File 'lib/jekyll/filters.rb', line 130

def xml_escape(input)
  input.to_s.encode(:xml => :attr).gsub(%r!\A"|"\Z!, "")
end