Module: JekyllHighlightCards::ExpressionEvaluator

Included in:
LinkcardTag, PolaroidTag
Defined in:
lib/jekyll-highlight-cards/expression_evaluator.rb

Overview

Evaluate Liquid expressions and handle string values

Provides utilities for evaluating Liquid variables (e.g., {{ page.title }}) and processing string values with quote stripping and fallback behavior.

Examples:

Evaluate a Liquid variable

result = evaluate_expression("{{ page.title }}", context)

Evaluate a quoted string

result = evaluate_expression('"My Title"', context)  #=> "My Title"

Instance Method Summary collapse

Instance Method Details

#evaluate_expression(token, context, allow_nil: true) ⇒ String?

Evaluate a token as a Liquid expression or literal string

Parameters:

  • token (String)

    the token to evaluate

  • context (Liquid::Context)

    the Liquid context for variable resolution

  • allow_nil (Boolean) (defaults to: true)

    whether to allow nil results

Returns:

  • (String, nil)

    evaluated value or nil if evaluation fails



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/jekyll-highlight-cards/expression_evaluator.rb', line 21

def evaluate_expression(token, context, allow_nil: true)
  # Strip outer quotes if present
  stripped = strip_outer_quotes(token)

  # If the original token was quoted, treat as literal string
  if !token.nil? && quote_wrapped?(token)
    return stripped if allow_nil

    return stripped.empty? ? nil : stripped
  end

  # Try to evaluate as Liquid expression
  if variable_lookup?(token)
    begin
      # Parse and evaluate the Liquid expression
      template = Liquid::Template.parse(token)
      result = template.render(context)
      return result if allow_nil

      return nil if result.empty?

      result
    rescue Liquid::SyntaxError, StandardError => e
      log_debug("Failed to evaluate '#{token}' as Liquid expression: #{e.class}: #{e}")
      token
    end
  else
    token
  end
end

#log_debug(message) ⇒ Object

Log debug message

Parameters:

  • message (String)

    message to log



87
88
89
# File 'lib/jekyll-highlight-cards/expression_evaluator.rb', line 87

def log_debug(message)
  Jekyll.logger.debug("HighlightCards:", message)
end

#log_error(message) ⇒ Object

Log error message

Parameters:

  • message (String)

    message to log



108
109
110
# File 'lib/jekyll-highlight-cards/expression_evaluator.rb', line 108

def log_error(message)
  Jekyll.logger.error("HighlightCards:", message)
end

#log_info(message) ⇒ Object

Log info message

Parameters:

  • message (String)

    message to log



94
95
96
# File 'lib/jekyll-highlight-cards/expression_evaluator.rb', line 94

def log_info(message)
  Jekyll.logger.info("HighlightCards:", message)
end

#log_warn(message) ⇒ Object

Log warning message

Parameters:

  • message (String)

    message to log



101
102
103
# File 'lib/jekyll-highlight-cards/expression_evaluator.rb', line 101

def log_warn(message)
  Jekyll.logger.warn("HighlightCards:", message)
end

#quote_wrapped?(value) ⇒ Boolean

Whether value has matching outer quotes and more than one character

Parameters:

  • value (String)

    the string to inspect

Returns:

  • (Boolean)


77
78
79
80
81
82
# File 'lib/jekyll-highlight-cards/expression_evaluator.rb', line 77

def quote_wrapped?(value)
  return false unless value.length >= 2

  (value.start_with?('"') && value.end_with?('"')) ||
    (value.start_with?("'") && value.end_with?("'"))
end

#strip_outer_quotes(value) ⇒ String

Strip outer quotes from a string (both single and double quotes)

Parameters:

  • value (String)

    the string to process

Returns:

  • (String)

    string with outer quotes removed if present



66
67
68
69
70
71
# File 'lib/jekyll-highlight-cards/expression_evaluator.rb', line 66

def strip_outer_quotes(value)
  return nil if value.nil?
  return value[1..-2] if quote_wrapped?(value)

  value
end

#variable_lookup?(expression) ⇒ Boolean

Check if an expression looks like a Liquid variable lookup

Parameters:

  • expression (String)

    the expression to check

Returns:

  • (Boolean)

    true if expression contains Liquid syntax



56
57
58
59
60
# File 'lib/jekyll-highlight-cards/expression_evaluator.rb', line 56

def variable_lookup?(expression)
  return false if expression.nil?

  expression.include?("{{") || expression.include?("{%")
end