Class: Card::Content::Chunk::Nest

Inherits:
Reference show all
Defined in:
mod/content/lib/card/content/chunk/nest.rb

Overview

Handler for nest chunks: {example}

Constant Summary collapse

DEFAULT_OPTION =

a value without a key is interpreted as view

:view

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Reference

clean, insert_in_slices, map_referees, recreate_all, #referee, #referer, repair_all, unmap_referees

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'mod/content/lib/card/content/chunk/nest.rb', line 8

def options
  @options
end

Class Method Details

.gsub(string) ⇒ Object



102
103
104
105
106
# File 'mod/content/lib/card/content/chunk/nest.rb', line 102

def self.gsub string
  string.gsub(/\{\{([^}]*)\}\}/) do |_match|
    yield(Regexp.last_match[1])
  end
end

Instance Method Details

#explicit_view=(view) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'mod/content/lib/card/content/chunk/nest.rb', line 83

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

#inspectObject



65
66
67
# File 'mod/content/lib/card/content/chunk/nest.rb', line 65

def inspect
  "<##{self.class}:n[#{@name}] p[#{@process_chunk}] txt:#{@text}>"
end

#interpret(match, _content) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'mod/content/lib/card/content/chunk/nest.rb', line 16

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_optionsObject



38
39
40
41
42
43
# File 'mod/content/lib/card/content/chunk/nest.rb', line 38

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) ⇒ Object



45
46
47
48
49
# File 'mod/content/lib/card/content/chunk/nest.rb', line 45

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?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'mod/content/lib/card/content/chunk/nest.rb', line 94

def main?
  nest_name == "_main"
end

#nest_nameObject



98
99
100
# File 'mod/content/lib/card/content/chunk/nest.rb', line 98

def nest_name
  options&.dig :nest_name
end

#option_string_to_hash(list_string, options_hash) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'mod/content/lib/card/content/chunk/nest.rb', line 51

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_chunkObject



69
70
71
72
73
74
75
# File 'mod/content/lib/card/content/chunk/nest.rb', line 69

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_optionsObject



108
109
110
# File 'mod/content/lib/card/content/chunk/nest.rb', line 108

def raw_options
  @opt_lists
end

#strip_tags(string) ⇒ Object



28
29
30
31
32
# File 'mod/content/lib/card/content/chunk/nest.rb', line 28

def strip_tags string
  # NOTE: not using ActionView's strip_tags here
  # because this needs to be super fast.
  string.gsub(/<[^>]*>/, "")
end

#swap_name(old_name, new_name) ⇒ Object



77
78
79
80
81
# File 'mod/content/lib/card/content/chunk/nest.rb', line 77

def swap_name old_name, new_name
  replace_name_reference old_name, new_name
  nest_body = [@name.to_s, @opt_lists].compact * "|"
  @text = "{{#{nest_body}}}"
end

#visible_comment(message) ⇒ Object



34
35
36
# File 'mod/content/lib/card/content/chunk/nest.rb', line 34

def visible_comment message
  "<!-- #{CGI.escapeHTML message} -->"
end