Class: JekyllSupport::JekyllGenerator

Inherits:
Jekyll::Generator
  • Object
show all
Defined in:
lib/generator/jekyll_plugin_support_generator.rb

Overview

Base class for Jekyll generators. PluginMetaLogger.instance.config is populated with the contents of ‘_config.yml` before Jekyll::Generator instances run.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#helperObject (readonly)

Returns the value of attribute helper.



8
9
10
# File 'lib/generator/jekyll_plugin_support_generator.rb', line 8

def helper
  @helper
end

#line_numberObject (readonly)

Returns the value of attribute line_number.



8
9
10
# File 'lib/generator/jekyll_plugin_support_generator.rb', line 8

def line_number
  @line_number
end

#loggerObject (readonly)

Returns the value of attribute logger.



8
9
10
# File 'lib/generator/jekyll_plugin_support_generator.rb', line 8

def logger
  @logger
end

#siteObject (readonly)

Returns the value of attribute site.



8
9
10
# File 'lib/generator/jekyll_plugin_support_generator.rb', line 8

def site
  @site
end

Class Method Details

.register(klass) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/generator/jekyll_plugin_support_generator.rb', line 48

def self.register(klass)
  abort("Error: The #{klass.name} plugin does not define VERSION") \
    unless klass.const_defined?(:VERSION)

  version = klass.const_get(:VERSION)
  error_name_stub = klass.name.include?('::') ? klass.name.split('::')[1] : klass.name
  error_ruby_class_name = "#{error_name_stub.camelcase(:upper)}Error"
  error_css_class_name = error_ruby_class_name.split('::').last.snakecase
  msg = <<~END_MSG
    Loaded generator plugin #{klass.name} v#{version}. It has:
      Error class: #{@error_name}
      CSS class for error messages: #{error_css_class_name}
  END_MSG

  PluginMetaLogger.instance.info { msg }
end

Instance Method Details

#generate(site) ⇒ void

This method returns an undefined value.

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



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/generator/jekyll_plugin_support_generator.rb', line 13

def generate(site)
  @logger ||= PluginMetaLogger.instance.new_logger(self, PluginMetaLogger.instance.config)

  @error_name = "#{self.class.name}Error"
  # JekyllSupport::CustomError.factory @error_name

  @site   = site
  @config = @site.config
  @envs   = site.config['env']
  @theme  = @site.theme

  @mode = ENV['JEKYLL_ENV'] || 'development'

  # set_error_context(self.class)

  generate_impl
rescue StandardError => e
  e.shorten_backtrace
  @logger.error { "#{e.class} on line #{@line_number} of #{e.backtrace[0].split(':').first} by #{self.class.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 #{e.backtrace[0].split(':').first} by #{self.class.name}: #{e.message}
    </div>
  END_MSG
end

#generate_implvoid

This method returns an undefined value.

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

@config, @envs, @helper, @logger, @mode, @paginator, @site and @theme


46
# File 'lib/generator/jekyll_plugin_support_generator.rb', line 46

def generate_impl; end

#set_error_context(klass) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/generator/jekyll_plugin_support_generator.rb', line 65

def set_error_context(klass)
  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, klass.name)
end