Module: Jekyll::J1_Filters
- Defined in:
- lib/starter_web/_plugins/filters.rb
Constant Summary collapse
- EMPTY =
''
- EMPTY_LINE =
EMPTY_LINE = /^s*n/
/^\s*$\n/
- MULTIPLE_SPACES =
/ +/
- ALL_SPACES =
/\s+/
- COMMENT_LINE =
/^\s*#.*\n|\s*#.*\n/
- HTML_COMMENT_LINE =
HTML_COMMENT_LINE = /^s*<!–.*–>|s*<!–.*–>/
/<!--[\d\D]*?-->/
- JS_COMMENT_LINE =
/^\s*\/\/[\d\D]*?\s*$/
- NOTHING =
''.freeze
- SPACE =
' '.freeze
- ADOC_TAG_LINE =
/^\s*(:\S+\:.*$)/
- JBX_INDEX_TAG =
/\s+!!(\S+)!!\s+/
- ADOC_HEAD_LINE =
/^\s*(=+\s+\S+.*$)/
- LIQUID_TAG =
/({%\s*\S+\s*%})/
- ADOC_INLINE_COMMENT =
/^\s*(\/\/.*$)/
Instance Method Summary collapse
-
#contain_substr(input, substr) ⇒ Object
————————————————————————– contain_substr: check if a string contains a substring.
-
#contains(input, substr) ⇒ Object
————————————————————————– contains: check if a string contains a substring.
-
#difference(input, arr) ⇒ Object
————————————————————————– Managing Arrays input - arr: returns any elements from input that are NOT in arr arr - input: returns any elements from arr that are not in a arr | input: returns the unique set from input AND arr arr & input: returns the intersection elements of input AND arr pipe (|): returns the unique set of elements for BOTH arrays.
- #intersection(input, arr) ⇒ Object
- #is_array(input) ⇒ Object
- #is_fixnum(input) ⇒ Object
- #is_hash(input) ⇒ Object
- #is_numeric(input) ⇒ Object
-
#is_string(input) ⇒ Object
————————————————————————– is_XXXX:.
-
#is_type(input) ⇒ Object
————————————————————————– is_type:.
-
#json(input) ⇒ Object
————————————————————————– json:.
-
#merge(input, hash) ⇒ Object
————————————————————————– merge: merge two hashes (input <- hash).
-
#newline_to_nothing(input) ⇒ Object
————————————————————————– newline_to_space: Replace all newlines by space.
-
#newline_to_space(input) ⇒ Object
————————————————————————– newline_to_space: Replace all newlines by space.
-
#rand(input) ⇒ Object
————————————————————————– rand:.
-
#read_index(input) ⇒ Object
————————————————————————– read_index:.
-
#regex_replace(input, regex, replacement = NOTHING) ⇒ Object
————————————————————————– regex_replace:.
-
#regex_replace_first(input, regex, replacement = NOTHING) ⇒ Object
————————————————————————– regex_replace_first: replace the FIRST occurence.
-
#strip_adoc(input) ⇒ Object
————————————————————————– strip_adoc:.
-
#strip_all_spaces(input) ⇒ Object
————————————————————————– strip_all_spaces:.
-
#strip_comments(input) ⇒ Object
————————————————————————– strip_comments:.
-
#strip_empty_lines(input) ⇒ Object
————————————————————————– strip_empty_lines:.
-
#strip_html_comments(input) ⇒ Object
————————————————————————– strip_html_comments:.
-
#strip_js_comments(input) ⇒ Object
————————————————————————– strip_js_comments:.
-
#strip_liquid_tag(input) ⇒ Object
————————————————————————– strip_liquid_tag:.
-
#strip_multiple_spaces(input) ⇒ Object
————————————————————————– strip_multiple_spaces:.
-
#strip_my_html(input) ⇒ Object
————————————————————————– strip_my_html:.
-
#union(input, arr) ⇒ Object
def difference_both_unique(input, arr) if ( input.kind_of?(Array) ) input - arr | arr - input else [] end end.
Instance Method Details
#contain_substr(input, substr) ⇒ Object
contain_substr: check if a string contains a substring
Example:
112 113 114 |
# File 'lib/starter_web/_plugins/filters.rb', line 112 def contain_substr(input, substr) input.include?(substr) ? true : false end |
#contains(input, substr) ⇒ Object
contains: check if a string contains a substring
Example:
102 103 104 |
# File 'lib/starter_web/_plugins/filters.rb', line 102 def contains(input, substr) input.include?(substr) ? true : false end |
#difference(input, arr) ⇒ Object
Managing Arrays
input - arr: returns any elements from input that are NOT in arr
arr - input: returns any elements from arr that are not in a
arr | input: returns the unique set from input AND arr
arr & input: returns the intersection elements of input AND arr
pipe (|): returns the unique set of elements for BOTH arrays
Example|s:
{% assign input = '1,2,3,4,5' | split: ',' %}
{% assign arr = '1,2,3' | split: ',' %}
{% assign difference = input | difference: arr %}
{% assign union = input | union: arr %}
{% assign intersection = input | intersection: arr %}
NOTE
See: https://stackoverflow.com/questions/5678108/how-can-i-get-the-intersection-union-and-subset-of-arrays-in-ruby
157 158 159 160 161 162 163 |
# File 'lib/starter_web/_plugins/filters.rb', line 157 def difference(input, arr) if ( input.kind_of?(Array) ) input - arr else [] end end |
#intersection(input, arr) ⇒ Object
181 182 183 184 185 186 187 |
# File 'lib/starter_web/_plugins/filters.rb', line 181 def intersection(input, arr) if ( input.kind_of?(Array) ) input & arr else [] end end |
#is_array(input) ⇒ Object
342 343 344 345 |
# File 'lib/starter_web/_plugins/filters.rb', line 342 def is_array(input) input.kind_of?(Array) return type end |
#is_fixnum(input) ⇒ Object
333 334 335 |
# File 'lib/starter_web/_plugins/filters.rb', line 333 def is_fixnum(input) input.kind_of?(Fixnum) end |
#is_hash(input) ⇒ Object
347 348 349 |
# File 'lib/starter_web/_plugins/filters.rb', line 347 def is_hash(input) input.kind_of?(Hash) end |
#is_numeric(input) ⇒ Object
337 338 339 340 |
# File 'lib/starter_web/_plugins/filters.rb', line 337 def is_numeric(input) return true if input =~ /\A\d+\Z/ true if Float(input) rescue false end |
#is_string(input) ⇒ Object
is_XXXX:
"Duck typing" methods to determine the object (base) class
returns true|false
Example:
329 330 331 |
# File 'lib/starter_web/_plugins/filters.rb', line 329 def is_string(input) input.kind_of?(String) end |
#is_type(input) ⇒ Object
is_type:
Example:
316 317 318 |
# File 'lib/starter_web/_plugins/filters.rb', line 316 def is_type(input) "#{input.class}".to_s.strip.downcase end |
#json(input) ⇒ Object
json:
Example:
306 307 308 |
# File 'lib/starter_web/_plugins/filters.rb', line 306 def json(input) input.to_json end |
#merge(input, hash) ⇒ Object
merge: merge two hashes (input <- hash)
Example:
{% assign settings = options|merge:defaults %}
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/starter_web/_plugins/filters.rb', line 75 def merge(input, hash) unless input.respond_to?(:to_hash) # value = input == EMPTY ? 'empty' : input is_caller = caller[0][/`([^']*)'/, 1] raise ArgumentError.new('merge filter requires at least a hash for 1st arg, found caller|args: ' + "#{is_caller}|#{input}:#{hash}") end # if hash to merge is NOT a hash or empty return first hash (input) unless hash.respond_to?(:to_hash) input end if hash.nil? || hash.empty? input else merged = input.dup hash.each do |k, v| merged[k] = v end merged end end |
#newline_to_nothing(input) ⇒ Object
newline_to_space:
Replace all newlines by space
Example:
369 370 371 |
# File 'lib/starter_web/_plugins/filters.rb', line 369 def newline_to_nothing(input) input.to_s.gsub(/\n/, NOTHING) end |
#newline_to_space(input) ⇒ Object
newline_to_space:
Replace all newlines by space
Example:
358 359 360 |
# File 'lib/starter_web/_plugins/filters.rb', line 358 def newline_to_space(input) input.to_s.gsub(/\n/, SPACE) end |
#rand(input) ⇒ Object
rand:
Example:
295 296 297 298 |
# File 'lib/starter_web/_plugins/filters.rb', line 295 def rand(input) max = input.to_i Random.new.rand(1..max) end |
#read_index(input) ⇒ Object
read_index:
Example:
286 287 |
# File 'lib/starter_web/_plugins/filters.rb', line 286 def read_index(input) end |
#regex_replace(input, regex, replacement = NOTHING) ⇒ Object
regex_replace:
Example:
132 133 134 |
# File 'lib/starter_web/_plugins/filters.rb', line 132 def regex_replace(input, regex, replacement = NOTHING) input.to_s.gsub(Regexp.new(regex), replacement.to_s) end |
#regex_replace_first(input, regex, replacement = NOTHING) ⇒ Object
regex_replace_first: replace the FIRST occurence
Example:
122 123 124 |
# File 'lib/starter_web/_plugins/filters.rb', line 122 def regex_replace_first(input, regex, replacement = NOTHING) input.to_s.sub(Regexp.new(regex), replacement.to_s) end |
#strip_adoc(input) ⇒ Object
strip_adoc:
Example:
255 256 257 |
# File 'lib/starter_web/_plugins/filters.rb', line 255 def strip_adoc(input) input.to_s.gsub(ADOC_TAG_LINE, SPACE).gsub(ADOC_INLINE_COMMENT, SPACE).gsub(ADOC_HEAD_LINE, SPACE) end |
#strip_all_spaces(input) ⇒ Object
strip_all_spaces:
Example:
245 246 247 |
# File 'lib/starter_web/_plugins/filters.rb', line 245 def strip_all_spaces(input) input.to_s.gsub(Regexp.new(ALL_SPACES), SPACE) end |
#strip_comments(input) ⇒ Object
strip_comments:
Example:
205 206 207 |
# File 'lib/starter_web/_plugins/filters.rb', line 205 def strip_comments(input) input.to_s.gsub(Regexp.new(COMMENT_LINE), NOTHING) end |
#strip_empty_lines(input) ⇒ Object
strip_empty_lines:
Example:
195 196 197 |
# File 'lib/starter_web/_plugins/filters.rb', line 195 def strip_empty_lines(input) input.to_s.gsub(Regexp.new(EMPTY_LINE), NOTHING) end |
#strip_html_comments(input) ⇒ Object
strip_html_comments:
Example:
215 216 217 |
# File 'lib/starter_web/_plugins/filters.rb', line 215 def strip_html_comments(input) input.to_s.gsub(Regexp.new(HTML_COMMENT_LINE), NOTHING) end |
#strip_js_comments(input) ⇒ Object
strip_js_comments:
Example:
225 226 227 |
# File 'lib/starter_web/_plugins/filters.rb', line 225 def strip_js_comments(input) input.to_s.gsub(Regexp.new(JS_COMMENT_LINE), NOTHING) end |
#strip_liquid_tag(input) ⇒ Object
strip_liquid_tag:
Example:
265 266 267 |
# File 'lib/starter_web/_plugins/filters.rb', line 265 def strip_liquid_tag(input) input.to_s.gsub(LIQUID_TAG, SPACE) end |
#strip_multiple_spaces(input) ⇒ Object
strip_multiple_spaces:
Example:
235 236 237 |
# File 'lib/starter_web/_plugins/filters.rb', line 235 def strip_multiple_spaces(input) input.to_s.gsub(Regexp.new(MULTIPLE_SPACES), SPACE) end |
#strip_my_html(input) ⇒ Object
strip_my_html:
Example:
275 276 277 278 |
# File 'lib/starter_web/_plugins/filters.rb', line 275 def strip_my_html(input) space = ' '.freeze input.to_s.gsub(/<script.*?<\/script>/m, space).gsub(/<!--.*?-->/m, space).gsub(/<style.*?<\/style>/m, space).gsub(/<.*?>/m, space) end |
#union(input, arr) ⇒ Object
def difference_both_unique(input, arr)
if ( input.kind_of?(Array) )
input - arr | arr - input
else
[]
end
end
173 174 175 176 177 178 179 |
# File 'lib/starter_web/_plugins/filters.rb', line 173 def union(input, arr) if ( input.kind_of?(Array) ) input | arr else [] end end |