Module: FromToUntil

Defined in:
lib/jekyll_from_to_until.rb

Class Method Summary collapse

Class Method Details

.check_parameters(input_strings, regex) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/jekyll_from_to_until.rb', line 77

def check_parameters(input_strings, regex)
  if input_strings.nil? || input_strings.empty?
    @logger.warn { "Warning: Plugin 'from' received no input for regex #{regex}." }
    return false
  end

  regex = regex.to_s
  if regex.nil? || regex.empty?
    @logger.warn { "Warning: Plugin 'from' received no regex for input #{input_strings}." }
    return false
  end
  true
end

.from(input_strings, regex) ⇒ String

Filters a multiline string, returning the portion beginning with the line that satisfies a regex. The regex could be enclosed in single quotes, double quotes, or nothing.

Examples:

Returns remaining lines starting with the line containing the word ‘module`.

{{ flexible_include '/blog/2020/10/03/jekyll-plugins.html' | from 'module' }}

Parameters:

  • input_strings (String)

    The multi-line string to scan

  • regex (String)

    The regular expression to match against each line of ‘input_strings` until found

Returns:

  • (String)

    The remaining multi-line string



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/jekyll_from_to_until.rb', line 29

def from(input_strings, regex)
  return '' unless check_parameters(input_strings, regex)

  regex = remove_quotations(regex.to_s.strip)
  matched = false
  result = ''
  input_strings.each_line do |line|
    matched = true if !matched && line =~ %r!#{regex}!
    result += line if matched
  end
  result
end

.remove_quotations(str) ⇒ Object



91
92
93
94
95
# File 'lib/jekyll_from_to_until.rb', line 91

def remove_quotations(str)
  str = str.slice(1..-2) if (str.start_with?('"') && str.end_with?('"')) ||
                            (str.start_with?("'") && str.end_with?("'"))
  str
end

.to(input_strings, regex) ⇒ Object

Filters a multiline string, returning the portion from the beginning until and including the line that satisfies a regex. The regex could be enclosed in single quotes, double quotes, or nothing.

Examples:

Returns lines up to and including the line containing the word ‘module`.

{{ flexible_include '/blog/2020/10/03/jekyll-plugins.html' | to 'module' }}


46
47
48
49
50
51
52
53
54
55
56
# File 'lib/jekyll_from_to_until.rb', line 46

def to(input_strings, regex)
  return '' unless check_parameters(input_strings, regex)

  regex = remove_quotations(regex.to_s.strip)
  result = ''
  input_strings.each_line do |line|
    result += line
    return result if line.match?(%r!#{regex}!)
  end
  result
end

.until(input_strings, regex) ⇒ Object

Filters a multiline string, returning the portion from the beginning until but not including the line that satisfies a regex. The regex could be enclosed in single quotes, double quotes, or nothing.

Examples:

Returns lines up to but not including the line containing the word ‘module`.

{{ flexible_include '/blog/2020/10/03/jekyll-plugins.html' | until 'module' }}


62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/jekyll_from_to_until.rb', line 62

def until(input_strings, regex)
  return '' unless check_parameters(input_strings, regex)

  regex = remove_quotations(regex.to_s.strip)
  result = ''
  input_strings.each_line do |line|
    return result if line.match?(%r!#{regex}!)

    result += line
  end
  result
end