Sha256: 51495fd4f2b049b780bce4dc5fc37218742b1180d5259743fc504eb95e4e64b2

Contents?: true

Size: 1.22 KB

Versions: 10

Compression:

Stored size: 1.22 KB

Contents

require 'jekyll_plugin_logger'
require 'rspec/match_ignoring_whitespace'
require_relative '../lib/jekyll_plugin_support'
require_relative '../lib/jekyll_plugin_support_spec_support'

class LiquidVariableParsing
  # @return copy of str with references to defined variables replaced by the values of the variables
  def variable_replace(str, scopes)
    result = str.clone
    match_data_list = str.to_enum(:scan, /{{[a-z_][a-zA-Z_0-9]*}}/).map { Regexp.last_match }.reverse
    match_data_list.each do |md|
      from = md.begin(0)
      to = md.end(0) - 1
      ref = str[from..to]
      name = ref[2..-3]
      scopes.each do |scope|
        value = scope.key?(name) ? scope[name] : ref
        # puts "str=#{str}; from=#{from}; to=#{to}; name=#{name} value=#{value}"
        result[from..to] = value
      end
    end
    result
  end

  RSpec.describe JekyllPluginHelper do
    it 'substitutes variable references for values without recursion' do
      scopes = [{ 'a' => '{{', 'b' => 'asdf', 'c' => '}}' }]
      str = '{{a}}{{b}}{{c}} This should be unchanged: {{d}}'
      new_str = variable_replace(str, scopes)
      expect(str).to start_with('{{a}}')
      expect(new_str).to be('{{asdf}} This should be unchanged: {{d}}')
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
jekyll_plugin_support-1.0.3 spec/liquid_variable_parsing_spec.rb
jekyll_plugin_support-1.0.2 spec/liquid_variable_parsing_spec.rb
jekyll_plugin_support-1.0.1 spec/liquid_variable_parsing_spec.rb
jekyll_plugin_support-1.0.0 spec/liquid_variable_parsing_spec.rb
jekyll_plugin_support-0.8.5 spec/liquid_variable_parsing_spec.rb
jekyll_plugin_support-0.8.4 spec/liquid_variable_parsing_spec.rb
jekyll_plugin_support-0.8.3 spec/liquid_variable_parsing_spec.rb
jekyll_plugin_support-0.8.2 spec/liquid_variable_parsing_spec.rb
jekyll_plugin_support-0.8.1 spec/liquid_variable_parsing_spec.rb
jekyll_plugin_support-0.8.0 spec/liquid_variable_parsing_spec.rb