Class: JekyllSupport::JekyllTag

Inherits:
Liquid::Tag
  • Object
show all
Includes:
JekyllSupportError
Defined in:
lib/tag/jekyll_plugin_support_tag.rb,
lib/jekyll_plugin_support.rb

Overview

Base class for Jekyll tags

Direct Known Subclasses

JekyllTagNoArgParsing

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Parameters:

  • tag_name (String)

    the name of the tag, which we usually know.

  • argument_string (String)

    the arguments passed to the tag, as a single string.

  • parse_context (Liquid::ParseContext)

    hash that stores Liquid options. By default it has two keys: :locale and :line_numbers, the first is a Liquid::I18n object, and the second, a boolean parameter that determines if error messages should display the line number the error occurred. This argument is used mostly to display localized error messages on Liquid built-in Tags and Filters. See github.com/Shopify/liquid/wiki/Liquid-for-Programmers#create-your-own-tags

Raises:



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/tag/jekyll_plugin_support_tag.rb', line 18

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

  @argument_string = markup
  @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, @argument_string, @logger, 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.



7
8
9
# File 'lib/tag/jekyll_plugin_support_tag.rb', line 7

def argument_string
  @argument_string
end

#helperObject (readonly)

Returns the value of attribute helper.



7
8
9
# File 'lib/tag/jekyll_plugin_support_tag.rb', line 7

def helper
  @helper
end

#line_numberObject (readonly)

Returns the value of attribute line_number.



7
8
9
# File 'lib/tag/jekyll_plugin_support_tag.rb', line 7

def line_number
  @line_number
end

#loggerObject (readonly)

Returns the value of attribute logger.



7
8
9
# File 'lib/tag/jekyll_plugin_support_tag.rb', line 7

def logger
  @logger
end

#pageObject (readonly)

Returns the value of attribute page.



7
8
9
# File 'lib/tag/jekyll_plugin_support_tag.rb', line 7

def page
  @page
end

#siteObject (readonly)

Returns the value of attribute site.



7
8
9
# File 'lib/tag/jekyll_plugin_support_tag.rb', line 7

def site
  @site
end

Instance Method Details

#render(liquid_context) ⇒ Object

Method prescribed by the Jekyll plugin lifecycle.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
# File 'lib/tag/jekyll_plugin_support_tag.rb', line 33

def render(liquid_context)
  return if @helper.excerpt_caller

  @helper.liquid_context = JekyllSupport.inject_vars @logger, liquid_context

  @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'

  markup = JekyllSupport.lookup_liquid_variables liquid_context, @argument_string
  @helper.reinitialize markup.strip

  render_impl
rescue StandardError => e
  e.shorten_backtrace
  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 { "#{e.class} on line #{@line_number} #{of_page}while processing #{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

  <<~END_MSG
    <div class='standard_error'>
      #{e.class} on line #{@line_number} #{of_page}while processing #{tag_name} #{in_file_name} - #{e.message}
    </div>
  END_MSG
end

#render_implObject

Jekyll plugins must override this method, not render, so their plugin 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


81
82
83
# File 'lib/tag/jekyll_plugin_support_tag.rb', line 81

def render_impl
  abort "#{self.class}.render_impl for tag #{@tag_name} must be overridden, but it was not."
end

#set_error_contextObject



85
86
87
88
89
90
91
92
93
# File 'lib/tag/jekyll_plugin_support_tag.rb', line 85

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