Class: Card::Content::Chunk::Include

Inherits:
Reference
  • Object
show all
Defined in:
mod/core/chunk/include.rb

Overview

Handler for nest chunks: {example}

Constant Summary collapse

DEFAULT_OPTION =

a value without a key is interpreted as view

:view
@@options =
::Set.new [
  :inc_name,   # name as used in nest
  :inc_syntax, # full nest syntax
  :items,      # handles pipe-based recursion
   # _conventional options_
  :view, :type, :title, :params, :variant,
  :size,        # images only
  :hide, :show, # affects optional rendering
  :structure    # override raw_content
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Reference

delete_if_referer_missing, map_referees, mass_insert, recreate_all, #referee, #referer, repair_all, unmap_if_referee_missing, unmap_referees

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



22
23
24
# File 'mod/core/chunk/include.rb', line 22

def options
  @options
end

Instance Method Details

#explicit_view=(view) ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'mod/core/chunk/include.rb', line 106

def explicit_view= view
  return if @options[:view]
  # could check to make sure it's not already the default...
  if @text =~ /\|/
    @text.sub! "|", "|#{view};"
  else
    @text.sub! "}}", "|#{view}}}"
  end
end

#inspectObject



88
89
90
# File 'mod/core/chunk/include.rb', line 88

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

#interpret(match, _content) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'mod/core/chunk/include.rb', line 29

def interpret match, _content
  in_brackets = strip_tags match[1]
  name, @opt_lists = in_brackets.split "|", 2
  name = name.to_s.strip
  if name =~ /^\#/
    @process_chunk = name =~ /^\#\#/ ? "" : visible_comment(in_brackets)
  else
    @options = interpret_options.merge inc_name: name,
                                       inc_syntax: in_brackets
    @name = name
  end
end

#interpret_optionsObject



52
53
54
55
56
57
# File 'mod/core/chunk/include.rb', line 52

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



59
60
61
62
63
64
65
# File 'mod/core/chunk/include.rb', line 59

def interpret_piped_options list_string, items
  options_hash = items.nil? ? {} : { items: items }
  style_hash = {}
  option_string_to_hash list_string, options_hash, style_hash
  style_hash_to_string options_hash, style_hash
  options_hash
end

#option_string_to_hash(list_string, options_hash, style_hash) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'mod/core/chunk/include.rb', line 67

def option_string_to_hash list_string, options_hash, style_hash
  each_option(list_string) do |key, value|
    key = key.to_sym
    if key == :item
      options_hash[:items] ||= {}
      options_hash[:items][:view] = value
    elsif @@options.include? key
      options_hash[key] = value
    else
      style_hash[key] = value
    end
  end
end

#process_chunkObject



92
93
94
95
96
97
98
# File 'mod/core/chunk/include.rb', line 92

def process_chunk
  return @process_chunk if @process_chunk

  referee_name
  @processed = yield @options
  # this is not necessarily text, sometimes objects for json
end

#replace_reference(old_name, new_name) ⇒ Object



100
101
102
103
104
# File 'mod/core/chunk/include.rb', line 100

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



42
43
44
45
46
# File 'mod/core/chunk/include.rb', line 42

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

#style_hash_to_string(options_hash, style_hash) ⇒ Object



81
82
83
84
85
86
# File 'mod/core/chunk/include.rb', line 81

def style_hash_to_string options_hash, style_hash
  return if style_hash.empty?
  options_hash[:style] = style_hash.map do |key, value|
    CGI.escapeHTML "#{key}:#{value};"
  end * ""
end

#visible_comment(message) ⇒ Object



48
49
50
# File 'mod/core/chunk/include.rb', line 48

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