From d22cec81fb7c3222405cd49380b486d40323c5d4 Mon Sep 17 00:00:00 2001 From: abcang Date: Sat, 20 May 2017 03:19:14 +0900 Subject: [PATCH] Unify the method of extracting tags (#3138) --- app/lib/extractor.rb | 26 ++++++++++++++++++++++++ app/services/process_hashtags_service.rb | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/app/lib/extractor.rb b/app/lib/extractor.rb index 3d88b01cd2a..c582b5e4dec 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 diff --git a/app/services/process_hashtags_service.rb b/app/services/process_hashtags_service.rb index 617a381590e..990e01a4b34 100644 --- a/app/services/process_hashtags_service.rb +++ b/app/services/process_hashtags_service.rb @@ -2,7 +2,7 @@ class ProcessHashtagsService < BaseService def call(status, tags = []) - tags = status.text.scan(Tag::HASHTAG_RE).map(&:first) if status.local? + tags = Extractor.extract_hashtags(status.text) if status.local? tags.map { |str| str.mb_chars.downcase }.uniq(&:to_s).each do |tag| status.tags << Tag.where(name: tag).first_or_initialize(name: tag)