Class: JekyllSupport::JekyllPluginHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/helper/jekyll_plugin_helper_class.rb,
lib/helper/jekyll_plugin_helper.rb,
lib/helper/jekyll_plugin_helper_attribution.rb

Overview

Attribution aspect of JekyllPluginHelper

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, logger, no_arg_parsing) ⇒ void

Parameters:

  • tag_name (String)

    the name of the tag, which we already know.

  • argument_string (String)

    the arguments from 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



22
23
24
25
26
27
28
29
30
# File 'lib/helper/jekyll_plugin_helper.rb', line 22

def initialize(tag_name, markup, logger, no_arg_parsing)
  @tag_name = tag_name
  @logger = logger
  @no_arg_parsing = no_arg_parsing
  @markup = markup
rescue StandardError => e
  e.shorten_backtrace
  @logger.error { e.message }
end

Instance Attribute Details

#argvObject (readonly)

Returns the value of attribute argv.



10
11
12
# File 'lib/helper/jekyll_plugin_helper.rb', line 10

def argv
  @argv
end

#argv_originalObject (readonly)

Returns the value of attribute argv_original.



10
11
12
# File 'lib/helper/jekyll_plugin_helper.rb', line 10

def argv_original
  @argv_original
end

#attributionObject (readonly)

Returns the value of attribute attribution.



10
11
12
# File 'lib/helper/jekyll_plugin_helper.rb', line 10

def attribution
  @attribution
end

#excerpt_callerObject (readonly)

Returns the value of attribute excerpt_caller.



10
11
12
# File 'lib/helper/jekyll_plugin_helper.rb', line 10

def excerpt_caller
  @excerpt_caller
end

#jpsh_subclass_callerObject (readonly)

Returns the value of attribute jpsh_subclass_caller.



10
11
12
# File 'lib/helper/jekyll_plugin_helper.rb', line 10

def jpsh_subclass_caller
  @jpsh_subclass_caller
end

#keys_valuesObject (readonly)

Returns the value of attribute keys_values.



10
11
12
# File 'lib/helper/jekyll_plugin_helper.rb', line 10

def keys_values
  @keys_values
end

#keys_values_originalObject (readonly)

Returns the value of attribute keys_values_original.



10
11
12
# File 'lib/helper/jekyll_plugin_helper.rb', line 10

def keys_values_original
  @keys_values_original
end

#liquid_contextObject

Returns the value of attribute liquid_context.



9
10
11
# File 'lib/helper/jekyll_plugin_helper.rb', line 9

def liquid_context
  @liquid_context
end

#loggerObject (readonly)

Returns the value of attribute logger.



10
11
12
# File 'lib/helper/jekyll_plugin_helper.rb', line 10

def logger
  @logger
end

#markupObject (readonly)

Returns the value of attribute markup.



10
11
12
# File 'lib/helper/jekyll_plugin_helper.rb', line 10

def markup
  @markup
end

#no_arg_parsingObject (readonly)

Returns the value of attribute no_arg_parsing.



10
11
12
# File 'lib/helper/jekyll_plugin_helper.rb', line 10

def no_arg_parsing
  @no_arg_parsing
end

#paramsObject (readonly)

Returns the value of attribute params.



10
11
12
# File 'lib/helper/jekyll_plugin_helper.rb', line 10

def params
  @params
end

#params_originalObject (readonly)

Returns the value of attribute params_original.



10
11
12
# File 'lib/helper/jekyll_plugin_helper.rb', line 10

def params_original
  @params_original
end

#tag_nameObject (readonly)

Returns the value of attribute tag_name.



10
11
12
# File 'lib/helper/jekyll_plugin_helper.rb', line 10

def tag_name
  @tag_name
end

Class Method Details

.current_spec(file) ⇒ Object

Parameters:

  • file

    must be a fully qualified file name that points to a file within a gem.

Returns:

  • Gem::Specification of gem that file points into, or nil if not called from a gem



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/helper/jekyll_plugin_helper_attribution.rb', line 7

def self.current_spec(file)
  abort 'JekyllPluginHelper::current_spec: file is nil' if file.nil?
  return nil unless File.exist?(file)

  searcher = if Gem::Specification.respond_to?(:find)
               Gem::Specification
             elsif Gem.respond_to?(:searcher)
               Gem.searcher.init_gemspecs
             end

  searcher&.find do |spec|
    file.start_with? spec.full_gem_path
  end
end

.expand_env(str, logger = nil, die_if_undefined: false) ⇒ Object

Expand an environment variable reference



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/helper/jekyll_plugin_helper_class.rb', line 9

def self.expand_env(str, logger = nil, die_if_undefined: false)
  str&.gsub(/\$([a-zA-Z_][a-zA-Z0-9_]*)|\${\g<1>}/) do
    envar = Regexp.last_match(1)
    unless ENV.key? envar
      msg = "jekyll_plugin_support error: environment variable #{envar} is undefined"
      raise JekyllPluginSupportError, msg.red, [] if die_if_undefined

      if logger
        logger.warn msg
      else
        puts msg.red
      end
    end
    ENV.fetch(envar, nil)
  end
end

.generate_message(klass, tag_name, version) ⇒ Object



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
51
52
53
54
55
56
# File 'lib/helper/jekyll_plugin_helper_class.rb', line 26

def self.generate_message(klass, tag_name, version)
  error_name_stub = klass.name.include?('::') ? klass.name.split('::')[1] : klass.name
  error_ruby_class_name = "#{error_name_stub.camelcase(:upper)}Error"
  config_die_key = "die_on_#{error_ruby_class_name.snakecase}"
  error_css_class_name = error_ruby_class_name.split('::').last.snakecase
  config = YAML.load_file('_config.yml')
  tag_config = config[tag_name]
  tag_config_msg = if tag_config.nil?
                     <<~END_MSG
                       _config.yml does not contain configuration information for this plugin.
                         You could add a section containing default values by specifying a section for the tag name,
                         and an entry whose name starts with `die_on_`, followed by a snake_case version of the error name.

                           #{tag_name}:
                             #{config_die_key}: false
                     END_MSG
                   else
                     <<~END_MSG
                       _config.yml contains the following configuration for this plugin:
                         #{tag_config}
                     END_MSG
                   end

  <<~END_MSG
    Loaded plugin #{tag_name} v#{version}. It has:
      Error class: #{error_ruby_class_name}
      CSS class for error messages: #{error_css_class_name}

      #{tag_config_msg}
  END_MSG
end

.register(klass, tag_name) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/helper/jekyll_plugin_helper_class.rb', line 58

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

  version = klass.const_get(:VERSION)

  abort("Error: The #{tag_name} plugin is not an instance of JekyllSupport::JekyllBlock or JekyllSupport::JekyllTag") \
    unless klass.instance_of?(Class) &&
           (klass.ancestors.include?(JekyllSupport::JekyllBlock) ||
            klass.ancestors.include?(JekyllSupport::JekyllTag))

  Liquid::Template.register_tag(tag_name, klass)
  msg = generate_message(klass, tag_name, version)
  PluginMetaLogger.instance.info { msg }
end

.remove_html_tags(string) ⇒ Object



74
75
76
# File 'lib/helper/jekyll_plugin_helper_class.rb', line 74

def self.remove_html_tags(string)
  string.gsub(/<[^>]*>/, '').strip
end

.remove_quotes(string) ⇒ Object

strip leading and trailing quotes if present



79
80
81
# File 'lib/helper/jekyll_plugin_helper_class.rb', line 79

def self.remove_quotes(string)
  string.strip.gsub(/\A'|\A"|'\Z|"\Z/, '').strip if string
end

Instance Method Details

#attributeObject



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/helper/jekyll_plugin_helper_attribution.rb', line 22

def attribute
  return unless @current_gem

  <<~END_OUTPUT
    <div id="jps_attribute_#{rand(999_999)}" class="jps_attribute">
      <div>
        <a href="#{@homepage}" target="_blank" rel="nofollow">
          #{attribution_string}
        </a>
      </div>
    </div>
  END_OUTPUT
end

#default_attributionObject



36
37
38
39
40
41
42
43
# File 'lib/helper/jekyll_plugin_helper_attribution.rb', line 36

def default_attribution
  authors = @authors&.join(', ')
  result = "Generated by the \#{@name} v\#{@version} Jekyll plugin"
  result << ", written by #{authors}" if authors
  result << " \#{@published_date}" if @published_date
  result << '.'
  result
end

#delete_parameter(key) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/helper/jekyll_plugin_helper.rb', line 77

def delete_parameter(key)
  return if @keys_values.empty? || @params.nil?

  @params.delete(key)
  @argv.delete_if { |x| x == key or x.start_with?("#{key}=") }
  @keys_values.delete(key)

  @params_original.delete(key)
  @argv_original.delete_if { |x| x == key or x.start_with?("#{key}=") }
  @keys_values_original.delete(key)
end

#gem_file(file) ⇒ Object

Sets @current_gem if file points at a uniquely named file within a gem.

Parameters:

  • file

    must be a fully qualified file name in a gem, for example: __FILE__



47
48
49
50
51
# File 'lib/helper/jekyll_plugin_helper_attribution.rb', line 47

def gem_file(file)
  @current_gem = JekyllPluginHelper.current_spec file
  @logger.debug "No gem found for '#{file} was found." unless @current_gem
  annotate_globals if @attribution && @current_gem
end

#parameter_specified?(name, delete_param: true) ⇒ Boolean

Returns undefined if parameter was specified, removes it from the available tokens and returns value.

Returns:

  • (Boolean)

    undefined if parameter was specified, removes it from the available tokens and returns value



33
34
35
36
37
38
39
40
41
# File 'lib/helper/jekyll_plugin_helper.rb', line 33

def parameter_specified?(name, delete_param: true)
  return false if @keys_values.to_s.empty?

  key = name
  key = name.to_sym if @keys_values&.first&.first.instance_of?(Symbol)
  value = @keys_values[key]
  delete_parameter(name) if delete_param
  value
end

#reinitialize(markup) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/helper/jekyll_plugin_helper.rb', line 43

def reinitialize(markup)
  # @keys_values was a Hash[Symbol, String|Boolean] but now it is Hash[String, String|Boolean]
  @markup = markup
  if @no_arg_parsing
    define_singleton_method(:argv) { warn_fetch :argv }
    define_singleton_method(:keys_values) { warn_fetch :keys_values }
    define_singleton_method(:params) { warn_fetch :params }
    define_singleton_method(:parameter_specified?) { |_name| warn_parse(:parameter_specified?) }
    define_singleton_method(:delete_parameter) { |_name| warn_parse(:delete_parameter) }
    @attribution = false
  else
    parse markup
    @attribution = parameter_specified?('attribution') || false
    @logger.debug { "@keys_values='#{@keys_values}'" }
  end
end

#remaining_markupObject

Call this method to return the remaining markup after ‘parameter_specified?` has been invoked.



61
62
63
# File 'lib/helper/jekyll_plugin_helper.rb', line 61

def remaining_markup
  @argv&.join(' ')
end

#remaining_markup_originalObject



65
66
67
# File 'lib/helper/jekyll_plugin_helper.rb', line 65

def remaining_markup_original
  @argv_original&.join(' ')
end

#warn_fetch(variable) ⇒ Object



69
70
71
# File 'lib/helper/jekyll_plugin_helper.rb', line 69

def warn_fetch(variable)
  abort "Error: Argument parsing was suppressed, but an attempt to obtain the value of #{variable} was made"
end

#warn_parse(meth) ⇒ Object



73
74
75
# File 'lib/helper/jekyll_plugin_helper.rb', line 73

def warn_parse(meth)
  abort "Error: Argument parsing was suppressed, but an attempt to invoke #{meth} was made"
end