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.
Instance Method Summary collapse
-
#evaluate_expression(token, context, allow_nil: true) ⇒ String?
Evaluate a token as a Liquid expression or literal string.
-
#log_debug(message) ⇒ Object
Log debug message.
-
#log_error(message) ⇒ Object
Log error message.
-
#log_info(message) ⇒ Object
Log info message.
-
#log_warn(message) ⇒ Object
Log warning message.
-
#quote_wrapped?(value) ⇒ Boolean
Whether value has matching outer quotes and more than one character.
-
#strip_outer_quotes(value) ⇒ String
Strip outer quotes from a string (both single and double quotes).
-
#variable_lookup?(expression) ⇒ Boolean
Check if an expression looks like a Liquid variable lookup.
Instance Method Details
#evaluate_expression(token, context, allow_nil: true) ⇒ String?
Evaluate a token as a Liquid expression or literal string
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
87 88 89 |
# File 'lib/jekyll-highlight-cards/expression_evaluator.rb', line 87 def log_debug() Jekyll.logger.debug("HighlightCards:", ) end |
#log_error(message) ⇒ Object
Log error message
108 109 110 |
# File 'lib/jekyll-highlight-cards/expression_evaluator.rb', line 108 def log_error() Jekyll.logger.error("HighlightCards:", ) end |
#log_info(message) ⇒ Object
Log info message
94 95 96 |
# File 'lib/jekyll-highlight-cards/expression_evaluator.rb', line 94 def log_info() Jekyll.logger.info("HighlightCards:", ) end |
#log_warn(message) ⇒ Object
Log warning message
101 102 103 |
# File 'lib/jekyll-highlight-cards/expression_evaluator.rb', line 101 def log_warn() Jekyll.logger.warn("HighlightCards:", ) end |
#quote_wrapped?(value) ⇒ Boolean
Whether value has matching outer quotes and more than one character
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)
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
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 |