Class: JekyllSupport::JekyllPluginHelper
- Inherits:
-
Object
- Object
- JekyllSupport::JekyllPluginHelper
- 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
-
#argv ⇒ Object
readonly
Returns the value of attribute argv.
-
#argv_original ⇒ Object
readonly
Returns the value of attribute argv_original.
-
#attribution ⇒ Object
readonly
Returns the value of attribute attribution.
-
#excerpt_caller ⇒ Object
readonly
Returns the value of attribute excerpt_caller.
-
#jpsh_subclass_caller ⇒ Object
readonly
Returns the value of attribute jpsh_subclass_caller.
-
#keys_values ⇒ Object
readonly
Returns the value of attribute keys_values.
-
#keys_values_original ⇒ Object
readonly
Returns the value of attribute keys_values_original.
-
#liquid_context ⇒ Object
Returns the value of attribute liquid_context.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#markup ⇒ Object
readonly
Returns the value of attribute markup.
-
#no_arg_parsing ⇒ Object
readonly
Returns the value of attribute no_arg_parsing.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#params_original ⇒ Object
readonly
Returns the value of attribute params_original.
-
#tag_name ⇒ Object
readonly
Returns the value of attribute tag_name.
Class Method Summary collapse
- .current_spec(file) ⇒ Object
-
.expand_env(str, logger = nil, die_if_undefined: false) ⇒ Object
Expand an environment variable reference.
- .generate_message(klass, tag_name, version) ⇒ Object
- .register(klass, tag_name) ⇒ Object
- .remove_html_tags(string) ⇒ Object
-
.remove_quotes(string) ⇒ Object
strip leading and trailing quotes if present.
Instance Method Summary collapse
- #attribute ⇒ Object
- #default_attribution ⇒ Object
- #delete_parameter(key) ⇒ Object
-
#gem_file(file) ⇒ Object
Sets @current_gem if file points at a uniquely named file within a gem.
- #initialize(tag_name, markup, logger, no_arg_parsing) ⇒ void constructor
-
#parameter_specified?(name, delete_param: true) ⇒ Boolean
Undefined if parameter was specified, removes it from the available tokens and returns value.
- #reinitialize(markup) ⇒ Object
-
#remaining_markup ⇒ Object
Call this method to return the remaining markup after ‘parameter_specified?` has been invoked.
- #remaining_markup_original ⇒ Object
- #warn_fetch(variable) ⇒ Object
- #warn_parse(meth) ⇒ Object
Constructor Details
#initialize(tag_name, markup, logger, no_arg_parsing) ⇒ void
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. } end |
Instance Attribute Details
#argv ⇒ Object (readonly)
Returns the value of attribute argv.
10 11 12 |
# File 'lib/helper/jekyll_plugin_helper.rb', line 10 def argv @argv end |
#argv_original ⇒ Object (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 |
#attribution ⇒ Object (readonly)
Returns the value of attribute attribution.
10 11 12 |
# File 'lib/helper/jekyll_plugin_helper.rb', line 10 def attribution @attribution end |
#excerpt_caller ⇒ Object (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_caller ⇒ Object (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_values ⇒ Object (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_original ⇒ Object (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_context ⇒ Object
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 |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
10 11 12 |
# File 'lib/helper/jekyll_plugin_helper.rb', line 10 def logger @logger end |
#markup ⇒ Object (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_parsing ⇒ Object (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 |
#params ⇒ Object (readonly)
Returns the value of attribute params.
10 11 12 |
# File 'lib/helper/jekyll_plugin_helper.rb', line 10 def params @params end |
#params_original ⇒ Object (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_name ⇒ Object (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
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.(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.(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 = (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.(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
#attribute ⇒ Object
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_attribution ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/helper/jekyll_plugin_helper_attribution.rb', line 36 def default_attribution = @authors&.join(', ') result = "Generated by the \#{@name} v\#{@version} Jekyll plugin" result << ", written by #{}" if 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.
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.
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_markup ⇒ Object
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_original ⇒ Object
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 |