| 1 |
|
| 2 |
|
| 3 |
module Diva::Entity |
| 4 |
class BlankEntity |
| 5 |
REGEXP_EACH_CHARACTER = //u.freeze |
| 6 |
|
| 7 |
include Enumerable |
| 8 |
|
| 9 |
attr_reader :message |
| 10 |
|
| 11 |
def initialize(message) |
| 12 |
@message = message |
| 13 |
@generate_value = [] end |
| 14 |
|
| 15 |
def each(&block) |
| 16 |
to_a.each(&block) |
| 17 |
end |
| 18 |
|
| 19 |
def reverse_each(&block) |
| 20 |
to_a.reverse.each(&block) |
| 21 |
end |
| 22 |
|
| 23 |
字列, url:リンク先}, ...] の配列を返す |
| 24 |
される文字列。 |
| 25 |
にURLになるかはリンクの種類に依存する。 |
| 26 |
合はこの内容は "mikutter" になる。 |
| 27 |
def to_a |
| 28 |
generate_value end |
| 29 |
|
| 30 |
た状態のMessageの本文を返す |
| 31 |
def to_s |
| 32 |
segment_splitted.map{ |s| |
| 33 |
if s.is_a? Hash |
| 34 |
s[:face] |
| 35 |
else |
| 36 |
s end }.join end |
| 37 |
|
| 38 |
ンティティを書き換える。 |
| 39 |
換えられた場合、イベントで書き換えが通知される。 |
| 40 |
ィの範囲が被った場合それを削除する |
| 41 |
|
| 42 |
要素を持つ配列 |
| 43 |
ィの種類。:urls 等 |
| 44 |
リ(リンク先URL等) |
| 45 |
する文字列 |
| 46 |
き換える範囲 |
| 47 |
ge |
| 48 |
|
| 49 |
|
| 50 |
def add(addition) |
| 51 |
type_strict addition[:slug] => Symbol, addition[:url] => String, addition[:face] => String, addition[:range] => Range |
| 52 |
links = select{|link| |
| 53 |
(link[:range].to_a & addition[:range].to_a).empty? |
| 54 |
} |
| 55 |
links.push(addition) |
| 56 |
@generate_value = links.sort_by{ |r| r[:range].first }.freeze |
| 57 |
Plugin.call(:message_modified, message) |
| 58 |
end |
| 59 |
|
| 60 |
要素を返す。エンティティでなければnilを返す |
| 61 |
def segment_by_index(index) |
| 62 |
segment_text.each{ |segment| |
| 63 |
if segment.is_a? Integer |
| 64 |
index -= segment |
| 65 |
elsif segment.is_a? Hash |
| 66 |
index -= segment[:face].size |
| 67 |
end |
| 68 |
if index < 0 |
| 69 |
if segment.is_a? Hash |
| 70 |
return segment |
| 71 |
else |
| 72 |
return nil end end } |
| 73 |
nil end |
| 74 |
|
| 75 |
ツイートに対して、 |
| 76 |
ティのURLの値}] |
| 77 |
ティの情報を間に入れた配列にして返す。 |
| 78 |
def segment_splitted |
| 79 |
result = message.to_show.split(REGEXP_EACH_CHARACTER) |
| 80 |
reverse_each{ |segment| |
| 81 |
result[segment[:range]] = segment } |
| 82 |
result.freeze end |
| 83 |
|
| 84 |
def segment_text |
| 85 |
result = [] |
| 86 |
segment_splitted.each{ |segment| |
| 87 |
if segment.is_a? String |
| 88 |
if result.last.is_a? Integer |
| 89 |
result[-1] += 1 |
| 90 |
else |
| 91 |
result << 1 end |
| 92 |
elsif segment.is_a? Hash |
| 93 |
result << segment end } |
| 94 |
result.freeze end |
| 95 |
|
| 96 |
def generate_value |
| 97 |
@generate_value end |
| 98 |
|
| 99 |
def get_range_by_face(link) |
| 100 |
right = message.to_show.index(link[:url], link[:indices][0]) |
| 101 |
left = message.to_show.rindex(link[:url], link[:indices][1]) |
| 102 |
if right and left |
| 103 |
start = [right - link[:indices][0], left - link[:indices][0]].map(&:abs).min + link[:indices][0] |
| 104 |
start...(start + link[:url].size) |
| 105 |
elsif right or left |
| 106 |
start = right || left |
| 107 |
start...(start + link[:url].size) |
| 108 |
else |
| 109 |
indices_to_range(link[:indices]) end end |
| 110 |
|
| 111 |
def indices_to_range(indices) |
| 112 |
Range.new(self.class.index_to_escaped_index(message.to_show, indices[0]), |
| 113 |
self.class.index_to_escaped_index(message.to_show, indices[1]), true) end |
| 114 |
|
| 115 |
end |
| 116 |
end |