Class: JekyllSupport::JekyllBlock

Inherits:
Liquid::Block
  • Object
show all
Includes:
JekyllSupportError, ToString
Defined in:
lib/block/jekyll_plugin_support_block.rb,
lib/jekyll_plugin_support.rb

Overview

Base class for Jekyll block tags

Direct Known Subclasses

JekyllBlockNoArgParsing

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ToString

#to_s

Methods included from JekyllSupportError

#exit_without_stack_trace, #format_error_message, #maybe_reraise_error, #remove_ansi_color, #warn_short_trace

Constructor Details

#initialize(tag_name, markup, parse_context) ⇒ void



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/block/jekyll_plugin_support_block.rb', line 23

def initialize(tag_name, markup, parse_context)
  super
  @tag_name = tag_name
  raise JekyllPluginSupportError, "markup is a #{markup.class} with value '#{markup}'." unless markup.instance_of? String

  # Lookup variable names with values in markup in render because site and config are not available here
  @argument_string = markup # Replace variable names with values in markup in render because site and config are not available here

  @logger = PluginMetaLogger.instance.new_logger(self, PluginMetaLogger.instance.config)
  @logger.debug { "#{self.class}: respond_to?(:no_arg_parsing) #{respond_to?(:no_arg_parsing) ? 'yes' : 'no'}." }
  @helper = JekyllPluginHelper.new(tag_name, markup, @logger, no_arg_parsing: respond_to?(:no_arg_parsing))

  @error_name = "#{tag_name.camelcase(:upper)}Error"
  ::JekyllSupport::CustomError.factory @error_name
end

Instance Attribute Details

#argument_stringObject (readonly)

Returns the value of attribute argument_string.



4
5
6
# File 'lib/block/jekyll_plugin_support_block.rb', line 4

def argument_string
  @argument_string
end

#helperObject (readonly)

Returns the value of attribute helper.



4
5
6
# File 'lib/block/jekyll_plugin_support_block.rb', line 4

def helper
  @helper
end

#line_numberObject (readonly)

Returns the value of attribute line_number.



4
5
6
# File 'lib/block/jekyll_plugin_support_block.rb', line 4

def line_number
  @line_number
end

#loggerObject (readonly)

Returns the value of attribute logger.



4
5
6
# File 'lib/block/jekyll_plugin_support_block.rb', line 4

def logger
  @logger
end

#pageObject (readonly)

Returns the value of attribute page.



4
5
6
# File 'lib/block/jekyll_plugin_support_block.rb', line 4

def page
  @page
end

#siteObject (readonly)

Returns the value of attribute site.



4
5
6
# File 'lib/block/jekyll_plugin_support_block.rb', line 4

def site
  @site
end

#textObject (readonly)

Returns the value of attribute text.



4
5
6
# File 'lib/block/jekyll_plugin_support_block.rb', line 4

def text
  @text
end

Instance Method Details

#blank?Boolean

Liquid::Block subclasses do not render if there is no content within the tag This override fixes that



41
42
43
# File 'lib/block/jekyll_plugin_support_block.rb', line 41

def blank?
  false
end

#render(liquid_context) ⇒ String

Method prescribed by the Jekyll plugin lifecycle. Defines @config, @envs, @mode, @page and @site



48
49
50
51
52
53
54
55
56
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/block/jekyll_plugin_support_block.rb', line 48

def render(liquid_context) # rubocop: disable Metrics/AbcSize
  @helper.liquid_context = ::JekyllSupport.inject_config_vars liquid_context # modifies liquid_context
  text = super # Liquid variable values in content are looked up and substituted

  @envs      = liquid_context.environments.first
  @page      = liquid_context.registers[:page]
  @scopes    = liquid_context.scopes
  @site      = liquid_context.registers[:site]

  @config = @site.config
  @tag_config = @config[@tag_name]
  @jps = @config['jekyll_plugin_support']
  @pry_on_standard_error = @jps['pry_on_standard_error'] || false if @jps

  set_error_context

  # @envs.keys are :content, :highlighter_prefix, :highlighter_suffix, :jekyll, :layout, :page, :paginator, :site, :theme
  @layout    = @envs[:layout]
  @paginator = @envs[:paginator]
  @theme     = @envs[:theme]

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

  @argument_string = JekyllSupport.lookup_liquid_variables @logger, @helper.liquid_context, @argument_string.to_s.strip
  @helper.reinitialize @argument_string.to_s.strip

  @attribution = @helper.parameter_specified?('attribution') || false unless @no_arg_parsing
  @logger.debug { "@keys_values='#{@keys_values}'" }

  # @argument_string = JekyllSupport.lookup_liquid_variables @logger, liquid_context, @argument_string # Is this redundant?
  # @argument_string.strip! # Is this redundant?
  # @helper.reinitialize @argument_string # Is this redundant?

  render_impl(text)
rescue StandardError => e
  e.shorten_backtrace
  @logger.error e.full_message
  file_name = e.backtrace[0]&.split(':')&.first
  in_file_name = "in '#{file_name}' " if file_name
  of_page = "of '#{@page['path']}'" if @page
  @logger.error { "While processing line #{@line_number} #{of_page} for #{tag_name} #{in_file_name}- #{e.message}" }
  binding.pry if @pry_on_standard_error # rubocop:disable Lint/Debugger
  raise e if @die_on_standard_error

  "    <div class='standard_error'>\n      \#{e.class} on line \#{@line_number} of \#{e.backtrace[0].split(':').first} by \#{@tag_name}: \#{e.message}\n    </div>\n  END_MSG\nend\n"

#render_impl(text) ⇒ String

Jekyll plugins should override this method, not render, so they can be tested more easily. The following variables are predefined:

@argument_string, @config, @envs, @helper, @layout, @logger, @mode, @page, @paginator, @site, @tag_name and @theme


105
106
107
# File 'lib/block/jekyll_plugin_support_block.rb', line 105

def render_impl(text)
  text
end

#set_error_contextObject



109
110
111
112
113
114
115
116
117
# File 'lib/block/jekyll_plugin_support_block.rb', line 109

def set_error_context
  return unless Object.const_defined? @error_name

  error_class = Object.const_get @error_name
  error_class.class_variable_set(:@@argument_string, @argument_string)
  error_class.class_variable_set(:@@line_number, @line_number)
  error_class.class_variable_set(:@@path, @page['path'])
  error_class.class_variable_set(:@@tag_name, @tag_name)
end