Module: JekyllSupport

Defined in:
lib/jekyll_plugin_support.rb,
lib/jekyll_plugin_support.rb,
lib/error/jekyll_custom_error.rb,
lib/helper/jekyll_plugin_helper.rb,
lib/tag/jekyll_plugin_support_tag.rb,
lib/block/jekyll_plugin_support_block.rb,
lib/helper/jekyll_plugin_helper_class.rb,
lib/tag/jekyll_plugin_support_tag_noarg.rb,
lib/block/jekyll_plugin_support_block_noarg.rb,
lib/helper/jekyll_plugin_helper_attribution.rb,
lib/generator/jekyll_plugin_support_generator.rb,
lib/jekyll_plugin_support/jekyll_plugin_support_class.rb

Defined Under Namespace

Classes: CustomError, JekyllBlock, JekyllBlockNoArgParsing, JekyllGenerator, JekyllPluginHelper, JekyllTag, JekyllTagNoArgParsing

Constant Summary collapse

DISPLAYED_CALLS =
8
JekyllPluginSupportError =
define_error

Class Method Summary collapse

Class Method Details

.define_errorObject

Returns a new StandardError subclass containing the shorten_backtrace method.

Returns:

  • a new StandardError subclass containing the shorten_backtrace method



24
25
26
# File 'lib/jekyll_plugin_support/jekyll_plugin_support_class.rb', line 24

def define_error
  Class.new JekyllSupport::CustomError
end

.dump_stack(stack, cycles, interval) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/jekyll_plugin_support/jekyll_plugin_support_class.rb', line 73

def self.dump_stack(stack, cycles, interval)
  stack_depth = stack.length
  puts "Stack depth is #{stack_depth}"
  num_entries = cycles * interval
  return unless stack_depth > interval * 5

  stack.last(num_entries).each_with_index do |x, i|
    msg = "  #{i}: #{x}"
    (i % interval).zero? ? puts(msg.yellow) : puts(msg)
  end
end

.dump_vars(_logger, liquid_context) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/jekyll_plugin_support/jekyll_plugin_support_class.rb', line 31

def self.dump_vars(_logger, liquid_context)
  page = liquid_context.registers[:page]
  vars = liquid_context.scopes.map do |scope|
    scope.map { |name, value| "  #{name} = #{value}" }.join("\n")
  end.join("\n")
  puts <<~END_MSG
    #{page['name']} variables after injecting any defined in _config.yml:
    #{vars}
  END_MSG
end

.error_short_trace(logger, error) ⇒ Object



17
18
19
20
21
# File 'lib/jekyll_plugin_support/jekyll_plugin_support_class.rb', line 17

def self.error_short_trace(logger, error)
  error.set_backtrace error.backtrace[0..DISPLAYED_CALLS]
  logger.error { error }
  error
end

.inject_vars(_logger, liquid_context) ⇒ Object

Add variable definitions from _config.yml to liquid_context Modifies liquid_context in the caller (call by object reference, see stackoverflow.com/a/1872159/553865) See README.md#configuration-variable-definitions See demo/variables.html

Returns:

  • modified liquid_context



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/jekyll_plugin_support/jekyll_plugin_support_class.rb', line 48

def self.inject_vars(_logger, liquid_context)
  # TODO: Modify a deep clone? Do I dare?
  site = liquid_context.registers[:site]

  plugin_variables = site.config['liquid_vars']
  return liquid_context unless plugin_variables

  scope = liquid_context.scopes.last

  env = site.config['env']
  mode = env&.key?('JEKYLL_ENV') ? env['JEKYLL_ENV'] : 'development'

  # Set default values
  plugin_variables&.each do |name, value|
    scope[name] = value if value.instance_of? String
  end

  # Override with environment-specific values
  plugin_variables[mode]&.each do |name, value|
    scope[name] = value if value.instance_of? String
  end

  liquid_context
end

.lookup_liquid_variables(liquid_context, markup_original) ⇒ Object

Modifies a clone of markup_original so variable references are replaced by their values

Parameters:

  • markup_original

    to be cloned

Returns:

  • modified markup_original



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/jekyll_plugin_support/jekyll_plugin_support_class.rb', line 88

def self.lookup_liquid_variables(liquid_context, markup_original)
  markup = markup_original.clone
  page = liquid_context.registers[:page]
  envs   = liquid_context.environments.first
  layout = envs[:layout]

  # process layout variables
  layout&.each do |name, value|
    markup.gsub!("{{layout.#{name}}}", value.to_s)
  end

  # process page variables
  # puts "\nStarting page variable processing of #{page['path']}; stack has #{caller.length} elements".green
  keys = page.keys
  %w[excerpt output].each { |key| keys.delete key }
  # puts "  Filtered keys: #{keys.join ' '}"
  # keys.each { |key| puts "  #{key}: #{page[key]}" }
  keys&.each do |key|
    markup.gsub!("{{page.#{key}}}", page[key].to_s)
  end

  # Process assigned, captured and injected variables
  liquid_context.scopes.each do |scope|
    scope&.each do |name, value|
      markup.gsub!("{{#{name}}}", value.to_s)
    end
  end
  markup
end

.redef_without_warning(const, value) ⇒ Object



12
13
14
15
# File 'lib/jekyll_plugin_support.rb', line 12

def self.redef_without_warning(const, value)
  send(:remove_const, const) if const_defined?(const)
  const_set const, value
end

.warn_short_trace(logger, error) ⇒ Object



118
119
120
121
122
123
124
125
# File 'lib/jekyll_plugin_support/jekyll_plugin_support_class.rb', line 118

def self.warn_short_trace(logger, error)
  remaining = error.backtrace.length - DISPLAYED_CALLS
  logger.warn do
    error.message + "\n" +
      error.backtrace.take(DISPLAYED_CALLS).join("\n") +
      "\n...Remaining #{remaining} call sites elided.\n"
  end
end