Module: FromToUntil

Defined in:
from_to_until.rb

Overview

Jekyll filters for working with multiline strings.

Author:

  • Copyright 2020 Michael Slinn

License:

  • SPDX-License-Identifier: Apache-2.0

Instance Method Summary collapse

Instance Method Details

#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



14
15
16
17
18
19
20
21
22
23
24
25
# File 'from_to_until.rb', line 14

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 =~ /#{regex}/
    result += line if matched
  end
  result
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' }}


31
32
33
34
35
36
37
38
39
40
41
# File 'from_to_until.rb', line 31

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 =~ /#{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' }}


47
48
49
50
51
52
53
54
55
56
57
# File 'from_to_until.rb', line 47

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 =~ /#{regex}/
    result += line
  end
  result
end