about summary refs log tree commit diff
path: root/app/lib
diff options
context:
space:
mode:
authorabcang <abcang1015@gmail.com>2017-05-20 03:19:14 +0900
committerEugen Rochko <eugen@zeonfederated.com>2017-05-19 20:19:14 +0200
commitd22cec81fb7c3222405cd49380b486d40323c5d4 (patch)
tree18509ab3b2e321195e846a7c5c07b9ae87fad311 /app/lib
parentd2e0edd721e202a0b32485e74b4dbf95148d171e (diff)
Unify the method of extracting tags (#3138)
Diffstat (limited to 'app/lib')
-rw-r--r--app/lib/extractor.rb26
1 files changed, 26 insertions, 0 deletions
diff --git a/app/lib/extractor.rb b/app/lib/extractor.rb
index 3d88b01cd..c582b5e4d 100644
--- a/app/lib/extractor.rb
+++ b/app/lib/extractor.rb
@@ -30,4 +30,30 @@ module Extractor
     end
     possible_entries
   end
+
+  def extract_hashtags_with_indices(text, _options = {})
+    return [] unless text =~ /#/
+
+    tags = []
+    text.scan(Tag::HASHTAG_RE) do |hash_text, _|
+      match_data = $LAST_MATCH_INFO
+      start_position = match_data.char_begin(1) - 1
+      end_position = match_data.char_end(1)
+      after = $'
+      if after =~ %r{\A://}
+        hash_text.match(/(.+)(https?\Z)/) do |matched|
+          hash_text = matched[1]
+          end_position -= matched[2].char_length
+        end
+      end
+
+      tags << {
+        hashtag: hash_text,
+        indices: [start_position, end_position],
+      }
+    end
+
+    tags.each { |tag| yield tag[:hashtag], tag[:indices].first, tag[:indices].last } if block_given?
+    tags
+  end
 end