Module: Card::Content::Chunk

Defined in:
card/lib/card/content/chunk.rb,
card/lib/card/content/chunk/abstract.rb,
card-mod-content/lib/card/content/chunk/uri.rb,
card-mod-content/lib/card/content/chunk/link.rb,
card-mod-content/lib/card/content/chunk/nest.rb,
card-mod-content/lib/card/content/chunk/reference.rb,
card-mod-content/lib/card/content/chunk/view_stub.rb,
card-mod-content/lib/card/content/chunk/escaped_literal.rb,
card-mod-content/lib/card/content/chunk/query_reference.rb,
card-mod-content/lib/card/content/chunk/keep_escaped_literal.rb

Overview

A chunk is a pattern of text that can be protected and interrogated by a format. Each Chunk class has a +pattern+ that states what sort of text it matches. Chunks are initalized by passing in the result of a match by its pattern.

Defined Under Namespace

Classes: Abstract, EmailUri, EscapedLiteral, HostUri, KeepEscapedLiteral, Link, Nest, QueryReference, Reference, Uri, ViewStub

Constant Summary collapse

@@raw_list =
{}
@@prefix_regexp_by_list =
{}
@@prefix_map_by_chunkname =
{}
@@prefix_map_by_list =
Hash.new { |h, k| h[k] = {} }

Class Method Summary collapse

Class Method Details

.build_prefix_regexp(chunk_list_key) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'card/lib/card/content/chunk.rb', line 64

def build_prefix_regexp chunk_list_key
  validate_chunk_list_key chunk_list_key

  prefix_res =
    raw_list[chunk_list_key].map do |chunkname|
      chunk_class = const_get chunkname
      chunk_class.config[:prefix_re]
    end
  /(?:#{prefix_res * '|'})/m
end

.find_class_by_prefix(prefix, chunk_list_key = :default) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'card/lib/card/content/chunk.rb', line 46

def find_class_by_prefix prefix, chunk_list_key=:default
  validate_chunk_list_key chunk_list_key

  prefix_map = prefix_map_by_list[chunk_list_key]
  config = prefix_map[prefix[0, 1]] ||
           prefix_map[prefix[-1, 1]] ||
           prefix_map[:default]
  # prefix identified by first character, last character, or default.
  # a little ugly...

  config[:class]
end

.prefix_regexp(chunk_list_key) ⇒ Object



59
60
61
62
# File 'card/lib/card/content/chunk.rb', line 59

def prefix_regexp chunk_list_key
  prefix_regexp_by_list[chunk_list_key] ||=
    build_prefix_regexp chunk_list_key
end

.register_class(klass, hash) ⇒ Object



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

def register_class klass, hash
  klass.config = hash.merge class: klass
  prefix_index = hash[:idx_char] || :default
  # ^ this is gross and needs to be moved out.

  klassname = klass.name.split("::").last.to_sym
  prefix_map_by_chunkname[klassname] = { prefix_index => klass.config }
  raw_list.each do |key, list|
    next unless list.include? klassname

    prefix_map_by_list[key].merge! prefix_map_by_chunkname[klassname]
  end
end

.register_list(key, list) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'card/lib/card/content/chunk.rb', line 36

def register_list key, list
  raw_list[key] = list
  prefix_map_by_list[key] =
    list.each_with_object({}) do |chunkname, h|
      next unless (p_map = prefix_map_by_chunkname[chunkname])

      h.merge! p_map
    end
end

.validate_chunk_list_key(chunk_list_key) ⇒ Object



75
76
77
78
79
# File 'card/lib/card/content/chunk.rb', line 75

def validate_chunk_list_key chunk_list_key
  unless raw_list.key? chunk_list_key
    raise ArgumentError, "invalid chunk list key: #{chunk_list_key}"
  end
end