Module: Jekyll::Filters
- Includes:
- DateFilters, GroupingFilters, URLFilters
- Defined in:
- lib/jekyll/filters.rb,
lib/jekyll/filters/url_filters.rb,
lib/jekyll/filters/date_filters.rb,
lib/jekyll/filters/grouping_filters.rb
Defined Under Namespace
Modules: DateFilters, GroupingFilters, URLFilters
Instance Method Summary collapse
-
#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.
-
#cgi_escape(input) ⇒ Object
CGI escape a string for use in a URL.
-
#inspect(input) ⇒ Object
Convert an object into its String representation for debugging.
-
#jsonify(input) ⇒ Object
Convert the input into json string.
-
#markdownify(input) ⇒ Object
Convert a Markdown string into HTML output.
-
#normalize_whitespace(input) ⇒ Object
Replace any whitespace in the input string with a single space.
-
#number_of_words(input) ⇒ Object
Count the number of words in the input string.
- #pop(array, num = 1) ⇒ Object
- #push(array, input) ⇒ Object
- #sample(input, num = 1) ⇒ Object
-
#sassify(input) ⇒ Object
Convert a Sass string into CSS output.
-
#scssify(input) ⇒ Object
Convert a Scss string into CSS output.
- #shift(array, num = 1) ⇒ Object
-
#slugify(input, mode = nil) ⇒ Object
Slugify a filename or title.
-
#smartify(input) ⇒ Object
Convert quotes into smart quotes.
-
#sort(input, property = nil, nils = "first") ⇒ Object
Sort an array of objects.
-
#to_integer(input) ⇒ Object
Convert the input into integer.
- #unshift(array, input) ⇒ Object
-
#uri_escape(input) ⇒ Object
URI escape a string.
-
#where(input, property, value) ⇒ Object
Filter an array of objects.
-
#where_exp(input, variable, expression) ⇒ Object
Filters an array of objects against an expression.
-
#xml_escape(input) ⇒ Object
XML escape a string for use.
Methods included from DateFilters
#date_to_long_string, #date_to_rfc822, #date_to_string, #date_to_xmlschema
Methods included from GroupingFilters
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.
140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/jekyll/filters.rb', line 140 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.
92 93 94 |
# File 'lib/jekyll/filters.rb', line 92 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.
301 302 303 |
# File 'lib/jekyll/filters.rb', line 301 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
158 159 160 |
# File 'lib/jekyll/filters.rb', line 158 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.
16 17 18 19 20 |
# File 'lib/jekyll/filters.rb', line 16 def markdownify(input) @context.registers[:site].find_converter_instance( Jekyll::Converters::Markdown ).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
115 116 117 |
# File 'lib/jekyll/filters.rb', line 115 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.
124 125 126 |
# File 'lib/jekyll/filters.rb', line 124 def number_of_words(input) input.split.length end |
#pop(array, num = 1) ⇒ Object
251 252 253 254 255 256 257 258 |
# File 'lib/jekyll/filters.rb', line 251 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
260 261 262 263 264 265 266 |
# File 'lib/jekyll/filters.rb', line 260 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
285 286 287 288 289 290 291 292 293 294 |
# File 'lib/jekyll/filters.rb', line 285 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.
38 39 40 41 42 |
# File 'lib/jekyll/filters.rb', line 38 def sassify(input) @context.registers[:site].find_converter_instance( Jekyll::Converters::Sass ).convert(input) end |
#scssify(input) ⇒ Object
Convert a Scss string into CSS output.
input - The Scss String to convert.
Returns the CSS formatted String.
49 50 51 52 53 |
# File 'lib/jekyll/filters.rb', line 49 def scssify(input) @context.registers[:site].find_converter_instance( Jekyll::Converters::Scss ).convert(input) end |
#shift(array, num = 1) ⇒ Object
268 269 270 271 272 273 274 275 |
# File 'lib/jekyll/filters.rb', line 268 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.
62 63 64 |
# File 'lib/jekyll/filters.rb', line 62 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.
27 28 29 30 31 |
# File 'lib/jekyll/filters.rb', line 27 def smartify(input) @context.registers[:site].find_converter_instance( Jekyll::Converters::SmartyPants ).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
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/jekyll/filters.rb', line 232 def sort(input, property = nil, nils = "first") raise ArgumentError, "Cannot sort a null object." if input.nil? 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
218 219 220 221 222 223 |
# File 'lib/jekyll/filters.rb', line 218 def to_integer(input) return 1 if input == true return 0 if input == false input.to_i end |
#unshift(array, input) ⇒ Object
277 278 279 280 281 282 283 |
# File 'lib/jekyll/filters.rb', line 277 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.
106 107 108 |
# File 'lib/jekyll/filters.rb', line 106 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 - the property within each object to filter by. value - the desired value.
Cannot be an instance of Array nor Hash since calling #to_s on them returns
their `#inspect` string object.
Returns the filtered array of objects
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/jekyll/filters.rb', line 171 def where(input, property, value) return input if !property || value.is_a?(Array) || value.is_a?(Hash) 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| compare_property_vs_target(item_property(object, property), value) end.to_a 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
199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/jekyll/filters.rb', line 199 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 "bar" <baz>"
Returns the escaped String.
77 78 79 |
# File 'lib/jekyll/filters.rb', line 77 def xml_escape(input) input.to_s.encode(:xml => :attr).gsub(%r!\A"|"\Z!, "") end |