Class: EnchantedQuill::ElementCreator

Inherits:
Object
  • Object
show all
Defined in:
lib/enchanted_quill/element_creator.rb

Defined Under Namespace

Classes: Element

Class Method Summary collapse

Class Method Details

.create_category_elements(text, range) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/enchanted_quill/element_creator.rb', line 45

def self.create_category_elements(text, range)
  categories = EnchantedQuill::Parser.parse_elements_for(:category, text, range)
  elements = []

  categories.each do |category|
    if category.range.length > 2
      range = NSMakeRange(category.range.location, category.range.length)
      word  = text.substringWithRange(range).strip

      if word[0] == '{' && word[-1] == '}'
        word = word[1..-2]
      end

      elements << Element.new(:category, word, range)
    end
  end

  elements
end

.create_hashtag_elements(text, range) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/enchanted_quill/element_creator.rb', line 25

def self.create_hashtag_elements(text, range)
  hashtags = EnchantedQuill::Parser.parse_elements_for(:hashtag, text, range)
  elements = []

  hashtags.each do |hashtag|
    if hashtag.range.length > 2
      range = NSMakeRange(hashtag.range.location, hashtag.range.length)
      word  = text.substringWithRange(range).strip

      if word[0] == '#'
        word = word[1..-1]
      end

      elements << Element.new(:hashtag, word, range)
    end
  end

  elements
end

.create_mentions_elements(text, range) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/enchanted_quill/element_creator.rb', line 5

def self.create_mentions_elements(text, range)
  mentions = EnchantedQuill::Parser.parse_elements_for(:mention, text, range)
  elements = []

  mentions.each do |mention|
    if mention.range.length > 2
      range = NSMakeRange(mention.range.location, mention.range.length)
      word  = text.substringWithRange(range).strip

      if word[0] == '@'
        word = word[1..-1]
      end

      elements << Element.new(:mention, word, range)
    end
  end

  elements
end

.create_url_elements(text, range) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/enchanted_quill/element_creator.rb', line 65

def self.create_url_elements(text, range)
  urls     = EnchantedQuill::Parser.parse_elements_for(:url, text, range)
  elements = []

  urls.each do |url|
    if url.range.length > 2
      word  = text.substringWithRange(url.range)
        .stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())

      elements << Element.new(:url, word, url.range)
    end
  end

  elements
end