class Card::Content::Chunk::Nest

Handler for nest chunks: {{example}}

Constants

DEFAULT_OPTION

Attributes

options[R]

Public Class Methods

gsub(string) { |last_match| ... } click to toggle source
# File lib/card/content/chunk/nest.rb, line 103
def self.gsub string
  string.gsub(/\{\{([^}]*)\}\}/) do |_match|
    yield(Regexp.last_match[1])
  end
end

Public Instance Methods

explicit_view=(view) click to toggle source
# File lib/card/content/chunk/nest.rb, line 84
def explicit_view= view
  return if @options[:view]

  # could check to make sure it's not already the default...
  if @text.match?(/\|/)
    @text.sub! "|", "|#{view};"
  else
    @text.sub! "}}", "|#{view}}}"
  end
end
inspect() click to toggle source
# File lib/card/content/chunk/nest.rb, line 66
def inspect
  "<##{self.class}:n[#{@name}] p[#{@process_chunk}] txt:#{@text}>"
end
interpret(match, _content) click to toggle source
# File lib/card/content/chunk/nest.rb, line 17
def interpret match, _content
  in_brackets = strip_tags match[1]
  name, @opt_lists = in_brackets.split "|", 2
  name = name.to_s.strip
  if name.match?(/^\#/)
    @process_chunk = name.match?(/^\#\#/) ? "" : visible_comment(in_brackets)
  else
    @options = interpret_options.merge nest_name: name, nest_syntax: in_brackets
    @name = name
  end
end
interpret_options() click to toggle source
# File lib/card/content/chunk/nest.rb, line 39
def interpret_options
  raw_options = @opt_lists.to_s.split("|").reverse
  raw_options.inject(nil) do |prev_level, level_options|
    interpret_piped_options level_options, prev_level
  end || {}
end
interpret_piped_options(list_string, items) click to toggle source
# File lib/card/content/chunk/nest.rb, line 46
def interpret_piped_options list_string, items
  options_hash = items.nil? ? {} : { items: items }
  option_string_to_hash list_string, options_hash
  options_hash
end
main?() click to toggle source
# File lib/card/content/chunk/nest.rb, line 95
def main?
  nest_name == "_main"
end
nest_name() click to toggle source
# File lib/card/content/chunk/nest.rb, line 99
def nest_name
  options&.dig :nest_name
end
option_string_to_hash(list_string, options_hash) click to toggle source
# File lib/card/content/chunk/nest.rb, line 52
def option_string_to_hash list_string, options_hash
  each_option(list_string) do |key, value|
    key = key.to_sym
    if key == :item
      options_hash[:items] ||= {}
      options_hash[:items][:view] = value
    elsif Card::View::Options.shark_keys.include? key
      options_hash[key] = value
      # else
      # handle other keys
    end
  end
end
process_chunk() click to toggle source
# File lib/card/content/chunk/nest.rb, line 70
def process_chunk
  return @process_chunk if @process_chunk

  referee_name
  @processed = format.content_nest(@options)
  # this is not necessarily text, sometimes objects for json
end
raw_options() click to toggle source
# File lib/card/content/chunk/nest.rb, line 109
def raw_options
  @opt_lists
end
replace_reference(old_name, new_name) click to toggle source
# File lib/card/content/chunk/nest.rb, line 78
def replace_reference old_name, new_name
  replace_name_reference old_name, new_name
  nest_body = [@name.to_s, @opt_lists].compact * "|"
  @text = "{{#{nest_body}}}"
end
strip_tags(string) click to toggle source
# File lib/card/content/chunk/nest.rb, line 29
def strip_tags string
  # NOTE: not using ActionView's strip_tags here
  # because this needs to be super fast.
  string.gsub(/<[^>]*>/, "")
end
visible_comment(message) click to toggle source
# File lib/card/content/chunk/nest.rb, line 35
def visible_comment message
  "<!-- #{CGI.escapeHTML message} -->"
end

Private Instance Methods

each_option(attr_string) { |strip, strip| ... } click to toggle source
# File lib/card/content/chunk/nest.rb, line 115
def each_option attr_string
  return if attr_string.blank?

  attr_string.strip.split(";").each do |pair|
    # key is optional for view option
    value, key = pair.split(":", 2).reverse
    key ||= self.class::DEFAULT_OPTION.to_s
    yield key.strip, value.strip
  end
end