Module: Jekyll::UJPowertools::VariableResolver

Instance Method Summary collapse

Instance Method Details

#is_quoted?(input) ⇒ Boolean

Check if input was originally quoted

Returns:

  • (Boolean)


110
111
112
# File 'lib/helpers/variable_resolver.rb', line 110

def is_quoted?(input)
  !!(input && input.match(/^["']/))
end

#parse_arguments(markup) ⇒ Object

Parse comma-separated arguments (preserving quotes)



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/helpers/variable_resolver.rb', line 57

def parse_arguments(markup)
  args = []
  current_arg = ''
  in_quotes = false
  quote_char = nil

  markup.each_char do |char|
    if !in_quotes && (char == '"' || char == "'")
      in_quotes = true
      quote_char = char
      current_arg += char
    elsif in_quotes && char == quote_char
      in_quotes = false
      quote_char = nil
      current_arg += char
    elsif !in_quotes && char == ','
      args << current_arg.strip
      current_arg = ''
    else
      current_arg += char
    end
  end

  args << current_arg.strip if current_arg.strip.length > 0
  args
end

#parse_options(args, context = nil) ⇒ Object

Parse key=value options from arguments



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/helpers/variable_resolver.rb', line 85

def parse_options(args, context = nil)
  options = {}
  
  args.each do |arg|
    if arg.include?('=')
      key, value = arg.split('=', 2)
      key = key.strip
      value = value.strip
      
      # If context provided, resolve variables in values
      if context
        value = resolve_input(context, value)
      else
        # Just strip quotes if no context
        value = value.gsub(/^['"]|['"]$/, '')
      end
      
      options[key] = value
    end
  end
  
  options
end

#resolve_input(context, input, prefer_literal = false) ⇒ Object

Resolve a variable or string literal from context If prefer_literal is true, unquoted strings are treated as literals unless they’re clearly variables



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/helpers/variable_resolver.rb', line 7

def resolve_input(context, input, prefer_literal = false)
  return nil if input.nil? || input.empty?
  
  # Check if the input is a quoted string literal
  if input.match(/^["'](.*)["']$/)
    # It's a string literal - extract the value between quotes
    $1
  elsif prefer_literal
    # In prefer_literal mode, only treat as variable if it has dots or exists in context
    if input.include?('.') || context[input]
      resolve_variable(context, input)
    else
      # Treat as literal string
      input
    end
  else
    # Default behavior - try to resolve as variable, return nil if not found
    resolve_variable(context, input)
  end
end

#resolve_variable(context, variable_name) ⇒ Object

Resolve a variable path through Jekyll’s context



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/helpers/variable_resolver.rb', line 29

def resolve_variable(context, variable_name)
  return nil if variable_name.nil? || variable_name.empty?
  
  # Handle nested variable access like page.resolved.post.id
  parts = variable_name.split('.')
  
  # Start with the first part
  current = context[parts.first]
  
  # Navigate through nested properties
  parts[1..-1].each do |part|
    break if current.nil?
    
    # Handle different types of objects
    if current.respond_to?(:[])
      current = current[part]
    elsif current.respond_to?(:data) && current.data.respond_to?(:[])
      # Handle Jekyll Drop objects
      current = current.data[part]
    else
      current = nil
    end
  end
  
  current
end