Module: Jekyll::J1_Filters
- Defined in:
- lib/starter_web/_plugins/filter/filters.rb
Constant Summary collapse
- EMPTY =
''- DOCTYPE_HTML =
'<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">'- EMPTY_LINE =
EMPTY_LINE = /^s*n/
/^\s*$\n/- SURRUNDING_QUOTES =
/^\h*\K"|"(?=\h*$)/- 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.
- #deep_clone(value) ⇒ Object
-
#deep_merge(input, *hashes) ⇒ Object
————————————————————————– deep_merge: merge MULTIPLE (nested) hashes (input <- hash*).
- #deep_merge_into!(target, source) ⇒ Object
-
#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 (nested) hashes (input <- hash).
- #merge_old(input, hash) ⇒ Object
-
#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_doctype_html(input) ⇒ Object
————————————————————————– strip_doctype_html:.
-
#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:.
-
#strip_surrounding_quotes(input) ⇒ Object
————————————————————————– strip_surrounding_quotes:.
-
#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:
195 196 197 |
# File 'lib/starter_web/_plugins/filter/filters.rb', line 195 def contain_substr(input, substr) input.include?(substr.to_s) ? true : false end |
#deep_clone(value) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/starter_web/_plugins/filter/filters.rb', line 168 def deep_clone(value) case value when Hash value.each_with_object({}) { |(k, v), h| h[k] = deep_clone(v) } when Array value.map { |v| deep_clone(v) } else # secure duplication for duplicable value types if value.respond_to?(:dup) && !value.is_a?(Symbol) && !value.is_a?(Numeric) && !value.is_a?(TrueClass) && !value.is_a?(FalseClass) && !value.nil? value.dup else value end end end |
#deep_merge(input, *hashes) ⇒ Object
deep_merge: merge MULTIPLE (nested) hashes (input <- hash*)
Example:
{% assign settings = defaults | deep_merge: prodA, prodB %}
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/starter_web/_plugins/filter/filters.rb', line 132 def deep_merge(input, *hashes) unless input.respond_to?(:to_hash) is_caller = caller[0][/`([^']*)'/, 1] raise ArgumentError.new( "deep_merge: requires at least a hash for the 1st arg, " \ "for #{is_caller}|#{input.inspect}" ) end # start by a deep copy of the defaults hash result = deep_clone(input) # merge all subsequent hashes one after the other # later values overwrite earlier values hashes.each do |hash| next if hash.nil? deep_merge_into!(result, hash) end result end |
#deep_merge_into!(target, source) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/starter_web/_plugins/filter/filters.rb', line 154 def deep_merge_into!(target, source) source.each do |key, value| if value.is_a?(Hash) && target[key].is_a?(Hash) # both are hashes - merge recursively deep_merge_into!(target[key], value) else # production value overrides default value target[key] = deep_clone(value) end end target 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
240 241 242 243 244 245 246 |
# File 'lib/starter_web/_plugins/filter/filters.rb', line 240 def difference(input, arr) if ( input.kind_of?(Array) ) input - arr else [] end end |
#intersection(input, arr) ⇒ Object
264 265 266 267 268 269 270 |
# File 'lib/starter_web/_plugins/filter/filters.rb', line 264 def intersection(input, arr) if ( input.kind_of?(Array) ) input & arr else [] end end |
#is_array(input) ⇒ Object
464 465 466 467 |
# File 'lib/starter_web/_plugins/filter/filters.rb', line 464 def is_array(input) input.kind_of?(Array) return type end |
#is_fixnum(input) ⇒ Object
455 456 457 |
# File 'lib/starter_web/_plugins/filter/filters.rb', line 455 def is_fixnum(input) input.kind_of?(Fixnum) end |
#is_hash(input) ⇒ Object
469 470 471 |
# File 'lib/starter_web/_plugins/filter/filters.rb', line 469 def is_hash(input) input.kind_of?(Hash) end |
#is_numeric(input) ⇒ Object
459 460 461 462 |
# File 'lib/starter_web/_plugins/filter/filters.rb', line 459 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:
451 452 453 |
# File 'lib/starter_web/_plugins/filter/filters.rb', line 451 def is_string(input) input.kind_of?(String) end |
#is_type(input) ⇒ Object
is_type:
Example:
438 439 440 |
# File 'lib/starter_web/_plugins/filter/filters.rb', line 438 def is_type(input) "#{input.class}".to_s.strip.downcase end |
#json(input) ⇒ Object
json:
Example:
409 410 411 |
# File 'lib/starter_web/_plugins/filter/filters.rb', line 409 def json(input) input.to_json end |
#merge(input, hash) ⇒ Object
merge: merge TWO (nested) hashes (input <- hash)
Example:
{% assign settings = options | merge: prodA %}
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/starter_web/_plugins/filter/filters.rb', line 78 def merge(input, hash) unless input.respond_to?(:to_hash) is_caller = caller[0][/`([^']*)'/, 1] raise ArgumentError.new( "merge: requires at least a hash for the 1st arg, " \ "for #{is_caller}|#{input.inspect}" ) end # early return on invalid hash return input unless hash.respond_to?(:to_hash) && !hash.nil? && !hash.empty? merged = input.dup hash.each do |k, v| if merged[k].respond_to?(:to_hash) && v.respond_to?(:to_hash) # Rekursiv mergen bei nested Hashes merged[k] = merge(merged[k], v) else merged[k] = v end end merged end |
#merge_old(input, hash) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/starter_web/_plugins/filter/filters.rb', line 102 def merge_old(input, hash) unless input.respond_to?(:to_hash) # value = input == EMPTY ? 'empty' : input is_caller = caller[0][/`([^']*)'/, 1] raise ArgumentError.new( "merge: requires at least a hash for the 1st arg, " \ "for #{is_caller}|#{input.inspect}" ) 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:
491 492 493 |
# File 'lib/starter_web/_plugins/filter/filters.rb', line 491 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:
480 481 482 |
# File 'lib/starter_web/_plugins/filter/filters.rb', line 480 def newline_to_space(input) input.to_s.gsub(/\n/, SPACE) end |
#rand(input) ⇒ Object
rand:
Example:
398 399 400 401 |
# File 'lib/starter_web/_plugins/filter/filters.rb', line 398 def rand(input) max = input.to_i Random.new.rand(1..max) end |
#read_index(input) ⇒ Object
read_index:
Example:
389 390 |
# File 'lib/starter_web/_plugins/filter/filters.rb', line 389 def read_index(input) end |
#regex_replace(input, regex, replacement = NOTHING) ⇒ Object
regex_replace:
Example:
215 216 217 |
# File 'lib/starter_web/_plugins/filter/filters.rb', line 215 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:
205 206 207 |
# File 'lib/starter_web/_plugins/filter/filters.rb', line 205 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:
358 359 360 |
# File 'lib/starter_web/_plugins/filter/filters.rb', line 358 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:
348 349 350 |
# File 'lib/starter_web/_plugins/filter/filters.rb', line 348 def strip_all_spaces(input) input.to_s.gsub(Regexp.new(ALL_SPACES), SPACE) end |
#strip_comments(input) ⇒ Object
strip_comments:
Example:
298 299 300 |
# File 'lib/starter_web/_plugins/filter/filters.rb', line 298 def strip_comments(input) input.to_s.gsub(Regexp.new(COMMENT_LINE), NOTHING) end |
#strip_doctype_html(input) ⇒ Object
strip_doctype_html:
Example: stribg_var | strip_doctype_html
308 309 310 |
# File 'lib/starter_web/_plugins/filter/filters.rb', line 308 def strip_doctype_html(input) input.to_s.gsub(Regexp.new(DOCTYPE_HTML), NOTHING) end |
#strip_empty_lines(input) ⇒ Object
strip_empty_lines:
Example:
288 289 290 |
# File 'lib/starter_web/_plugins/filter/filters.rb', line 288 def strip_empty_lines(input) input.to_s.gsub(Regexp.new(EMPTY_LINE), NOTHING) end |
#strip_html_comments(input) ⇒ Object
strip_html_comments:
Example:
318 319 320 |
# File 'lib/starter_web/_plugins/filter/filters.rb', line 318 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:
328 329 330 |
# File 'lib/starter_web/_plugins/filter/filters.rb', line 328 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:
368 369 370 |
# File 'lib/starter_web/_plugins/filter/filters.rb', line 368 def strip_liquid_tag(input) input.to_s.gsub(LIQUID_TAG, SPACE) end |
#strip_multiple_spaces(input) ⇒ Object
strip_multiple_spaces:
Example:
338 339 340 |
# File 'lib/starter_web/_plugins/filter/filters.rb', line 338 def strip_multiple_spaces(input) input.to_s.gsub(Regexp.new(MULTIPLE_SPACES), SPACE) end |
#strip_my_html(input) ⇒ Object
strip_my_html:
Example:
378 379 380 381 |
# File 'lib/starter_web/_plugins/filter/filters.rb', line 378 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 |
#strip_surrounding_quotes(input) ⇒ Object
strip_surrounding_quotes:
Example:
278 279 280 |
# File 'lib/starter_web/_plugins/filter/filters.rb', line 278 def strip_surrounding_quotes(input) input.to_s.gsub(Regexp.new(SURRUNDING_QUOTES), NOTHING) end |
#union(input, arr) ⇒ Object
def difference_both_unique(input, arr)
if ( input.kind_of?(Array) )
input - arr | arr - input
else
[]
end
end
256 257 258 259 260 261 262 |
# File 'lib/starter_web/_plugins/filter/filters.rb', line 256 def union(input, arr) if ( input.kind_of?(Array) ) input | arr else [] end end |