about summary refs log tree commit diff
path: root/app/lib/extractor.rb
diff options
context:
space:
mode:
authormasarakki <masaki182@gmail.com>2017-05-06 02:48:22 +0900
committerEugen Rochko <eugen@zeonfederated.com>2017-05-05 19:48:22 +0200
commitd08f1112d517788fb66d2683766cc168bac48315 (patch)
tree9ea8c15e12da05deae7552f76c18513c27d94fdd /app/lib/extractor.rb
parentbcfd9a2f8ecadd55e0a447eff37299f6cbd55a11 (diff)
use Twitter::Extractor for creating links (#2502)
Diffstat (limited to 'app/lib/extractor.rb')
-rw-r--r--app/lib/extractor.rb33
1 files changed, 33 insertions, 0 deletions
diff --git a/app/lib/extractor.rb b/app/lib/extractor.rb
new file mode 100644
index 000000000..3d88b01cd
--- /dev/null
+++ b/app/lib/extractor.rb
@@ -0,0 +1,33 @@
+# frozen_string_literal: true
+
+module Extractor
+  extend Twitter::Extractor
+
+  module_function
+
+  def extract_mentions_or_lists_with_indices(text) # :yields: username, list_slug, start, end
+    return [] unless text =~ Twitter::Regex[:at_signs]
+
+    possible_entries = []
+
+    text.to_s.scan(Account::MENTION_RE) do |screen_name, _|
+      match_data = $LAST_MATCH_INFO
+      after = $'
+      unless after =~ Twitter::Regex[:end_mention_match]
+        start_position = match_data.char_begin(1) - 1
+        end_position = match_data.char_end(1)
+        possible_entries << {
+          screen_name: screen_name,
+          indices: [start_position, end_position],
+        }
+      end
+    end
+
+    if block_given?
+      possible_entries.each do |mention|
+        yield mention[:screen_name], mention[:indices].first, mention[:indices].last
+      end
+    end
+    possible_entries
+  end
+end