Module: Jekyll::Filters
- Defined in:
- lib/jekyll/filters.rb
Instance Method Summary collapse
-
#array_to_sentence_string(array) ⇒ 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.
-
#date_to_long_string(date) ⇒ Object
Format a date in long format e.g.
-
#date_to_rfc822(date) ⇒ Object
Format a date according to RFC-822.
-
#date_to_string(date) ⇒ Object
Format a date in short format e.g.
-
#date_to_xmlschema(date) ⇒ Object
Format a date for use in XML.
-
#group_by(input, property) ⇒ Object
Group an array of items by a property.
-
#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.
-
#number_of_words(input) ⇒ Object
Count the number of words in the input string.
- #pop(array, input = 1) ⇒ Object
- #push(array, input) ⇒ Object
-
#sassify(input) ⇒ Object
Convert a Sass string into CSS output.
-
#scssify(input) ⇒ Object
Convert a Scss string into CSS output.
- #shift(array, input = 1) ⇒ Object
-
#slugify(input) ⇒ Object
Slugify a filename or title.
-
#sort(input, property = nil, nils = "first") ⇒ Object
Sort an array of objects.
-
#textilize(input) ⇒ Object
Convert a Textile string into HTML output.
- #unshift(array, input) ⇒ Object
-
#uri_escape(input) ⇒ Object
URI escape a string.
-
#where(input, property, value) ⇒ Object
Filter an array of objects.
-
#xml_escape(input) ⇒ Object
XML escape a string for use.
Instance Method Details
#array_to_sentence_string(array) ⇒ 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.
Examples
array_to_sentence_string(["apples", "oranges", "grapes"])
# => "apples, oranges, and grapes"
Returns the formatted String.
171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/jekyll/filters.rb', line 171 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.
133 134 135 |
# File 'lib/jekyll/filters.rb', line 133 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.
75 76 77 |
# File 'lib/jekyll/filters.rb', line 75 def date_to_long_string(date) 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.
103 104 105 |
# File 'lib/jekyll/filters.rb', line 103 def date_to_rfc822(date) 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.
66 67 68 |
# File 'lib/jekyll/filters.rb', line 66 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.
89 90 91 |
# File 'lib/jekyll/filters.rb', line 89 def date_to_xmlschema(date) time(date).xmlschema end |
#group_by(input, property) ⇒ Object
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"
202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/jekyll/filters.rb', line 202 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 |
#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.
296 297 298 |
# File 'lib/jekyll/filters.rb', line 296 def inspect(input) CGI.escapeHTML(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
190 191 192 |
# File 'lib/jekyll/filters.rb', line 190 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.
22 23 24 25 26 |
# 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) ⇒ Object
Count the number of words in the input string.
input - The String on which to operate.
Returns the Integer word count.
156 157 158 |
# File 'lib/jekyll/filters.rb', line 156 def number_of_words(input) input.split.length end |
#pop(array, input = 1) ⇒ Object
263 264 265 266 267 268 |
# File 'lib/jekyll/filters.rb', line 263 def pop(array, input = 1) return array unless array.is_a?(Array) new_ary = array.dup new_ary.pop(input.to_i || 1) new_ary end |
#push(array, input) ⇒ Object
270 271 272 273 274 275 |
# File 'lib/jekyll/filters.rb', line 270 def push(array, input) return array unless array.is_a?(Array) new_ary = array.dup new_ary.push(input) new_ary end |
#sassify(input) ⇒ Object
Convert a Sass string into CSS output.
input - The Sass String to convert.
Returns the CSS formatted String.
33 34 35 36 37 |
# 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) ⇒ Object
Convert a Scss string into CSS output.
input - The Scss String to convert.
Returns the CSS formatted String.
44 45 46 47 48 |
# File 'lib/jekyll/filters.rb', line 44 def scssify(input) site = @context.registers[:site] converter = site.getConverterImpl(Jekyll::Converters::Scss) converter.convert(input) end |
#shift(array, input = 1) ⇒ Object
277 278 279 280 281 282 |
# File 'lib/jekyll/filters.rb', line 277 def shift(array, input = 1) return array unless array.is_a?(Array) new_ary = array.dup new_ary.shift(input.to_i || 1) new_ary end |
#slugify(input) ⇒ Object
Slugify a filename or title.
input - The filename or title to slugify.
Returns the given filename or title as a lowercase String, with every sequence of spaces and non-alphanumeric characters replaced with a hyphen.
57 58 59 |
# File 'lib/jekyll/filters.rb', line 57 def slugify(input) Utils.slugify(input) 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
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/jekyll/filters.rb', line 234 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) ⇒ Object
Convert a Textile string into HTML output.
input - The Textile String to convert.
Returns the HTML formatted String.
11 12 13 14 15 |
# File 'lib/jekyll/filters.rb', line 11 def textilize(input) site = @context.registers[:site] converter = site.getConverterImpl(Jekyll::Converters::Textile) converter.convert(input) end |
#unshift(array, input) ⇒ Object
284 285 286 287 288 289 |
# File 'lib/jekyll/filters.rb', line 284 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.
147 148 149 |
# File 'lib/jekyll/filters.rb', line 147 def uri_escape(input) URI.escape(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
221 222 223 224 225 |
# File 'lib/jekyll/filters.rb', line 221 def where(input, property, value) return input unless input.is_a?(Enumerable) input = input.values if input.is_a?(Hash) input.select { |object| item_property(object, property) == value } 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.
118 119 120 |
# File 'lib/jekyll/filters.rb', line 118 def xml_escape(input) CGI.escapeHTML(input.to_s) end |