Module: Card::Content::Chunk

Defined in:
lib/card/content/chunk.rb,
mod/core/chunk/uri.rb,
mod/core/chunk/link.rb,
mod/core/chunk/nest.rb,
mod/core/chunk/literal.rb,
mod/core/chunk/reference.rb,
mod/core/chunk/view_stub.rb,
mod/core/chunk/query_reference.rb

Overview

This wiki chunk matches arbitrary URIs, using patterns from the Ruby URI modules. It parses out a variety of fields that could be used by formats to format the links in various ways (shortening domain names, hiding email addresses) It matches email addresses and host.com.au domains without schemes (http://) but adds these on as required.

The heuristic used to match a URI is designed to err on the side of caution. That is, it is more likely to not autolink a URI than it is to accidently autolink something that is not a URI. The reason behind this is it is easier to force a URI link by prefixing 'http://' to it than it is to escape and incorrectly marked up non-URI.

I'm using a part of the ISO 3166-1 Standard for country name suffixes. The generic names are from www.bnoack.com/data/countrycode2.html)

Defined Under Namespace

Classes: Abstract, EmailURI, EscapedLiteral, HostURI, 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

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



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

def find_class_by_prefix prefix, chunk_list_key=:default
  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

.get_prefix_regexp(chunk_list_key) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/card/content/chunk.rb', line 56

def get_prefix_regexp chunk_list_key
  prefix_regexp_by_list[chunk_list_key] ||= begin
    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
end

.register_class(klass, hash) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File '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



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

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
  prefix_map_by_list[key]
end