Module: Jekyll::Filters

Included in:
LiquiDocFilters
Defined in:
lib/liquid/filters/jekyll.rb,
lib/liquid/filters/url_filters.rb,
lib/liquid/filters/date_filters.rb,
lib/liquid/filters/grouping_filters.rb

Defined Under Namespace

Modules: DateFilters, GroupingFilters, URLFilters

Instance Method Summary collapse

Instance Method Details

#array_to_serial(array, connector = "and", serializer = ", ") ⇒ Object

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

Based on but differs from array_to_sentence_string, not available to LiquiDoc

array - The Array of Strings to join. connector - Word used to connect the last 2 items in the array

Examples

array_to_serial(["apples", "oranges", "grapes"])
# => "apples, oranges, and grapes"
array_to_serial(["apples", "oranges", "grapes"], "")
# => "apples, oranges, grapes"
Improved behavior::
array_to_serial(["apples", "oranges"], "")
# => "apples, oranges"

Returns the formatted String.



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/liquid/filters/jekyll.rb', line 254

def array_to_serial(array, connector="and", serializer=", ")
  con = "#{connector} " unless connector.empty?
  case array.length
  when 0
    out = ""
  when 1
    out = array[0].to_s
  when 2
    ser = serializer if connector.empty?
    out = "#{array[0]}#{ser} #{con}#{array[1]}"
  else
    out = "#{array[0...-1].join(serializer)}#{serializer}#{con}#{array[-1]}"
  end
  out
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.



199
200
201
# File 'lib/liquid/filters/jekyll.rb', line 199

def cgi_escape(input)
  CGI.escape(input)
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.



401
402
403
# File 'lib/liquid/filters/jekyll.rb', line 401

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



275
276
277
# File 'lib/liquid/filters/jekyll.rb', line 275

def jsonify(input)
  as_liquid(input).to_json
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



222
223
224
# File 'lib/liquid/filters/jekyll.rb', line 222

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.



231
232
233
# File 'lib/liquid/filters/jekyll.rb', line 231

def number_of_words(input)
  input.split.length
end

#pop(array, num = 1) ⇒ Object



364
365
366
367
368
369
# File 'lib/liquid/filters/jekyll.rb', line 364

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)
end

#push(array, add) ⇒ Object



371
372
373
374
375
# File 'lib/liquid/filters/jekyll.rb', line 371

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

#sample(array, num = 1) ⇒ Object



390
391
392
393
394
# File 'lib/liquid/filters/jekyll.rb', line 390

def sample(array, num=1)
  return array unless array.respond_to?(:sample)
  num = Liquid::Utils.to_integer(num)
  array.sample(num)
end

#shift(array, num = 1) ⇒ Object



377
378
379
380
381
382
# File 'lib/liquid/filters/jekyll.rb', line 377

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



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/liquid/filters/jekyll.rb', line 344

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



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

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

#unshift(array, add) ⇒ Object



384
385
386
387
388
# File 'lib/liquid/filters/jekyll.rb', line 384

def unshift(array, add)
  return array unless array.is_a?(Array)
  new_ary = array.dup
  new_ary.unshift(add)
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.



213
214
215
# File 'lib/liquid/filters/jekyll.rb', line 213

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



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/liquid/filters/jekyll.rb', line 286

def where(input, property, value)
  return input if property.nil? || value.nil?
  return input unless input.respond_to?(:select)
  input    = input.values if input.is_a?(Hash)
  input_id = input.hash

  # implement a hash based on method parameters to cache the end-result
  # for given parameters.
  @where_filter_cache ||= {}
  @where_filter_cache[input_id] ||= {}
  @where_filter_cache[input_id][property] ||= {}

  # stash or retrive results to return
  @where_filter_cache[input_id][property][value] ||= begin
    input.select do |object|
      Array(item_property(object, property)).map!(&:to_s).include?(value.to_s)
    end || []
  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



313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/liquid/filters/jekyll.rb', line 313

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.



184
185
186
# File 'lib/liquid/filters/jekyll.rb', line 184

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