You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.6 KiB

  1. # frozen_string_literal: true
  2. module Extractor
  3. extend Twitter::Extractor
  4. module_function
  5. def extract_mentions_or_lists_with_indices(text) # :yields: username, list_slug, start, end
  6. return [] unless text =~ Twitter::Regex[:at_signs]
  7. possible_entries = []
  8. text.to_s.scan(Account::MENTION_RE) do |screen_name, _|
  9. match_data = $LAST_MATCH_INFO
  10. after = $'
  11. unless after =~ Twitter::Regex[:end_mention_match]
  12. start_position = match_data.char_begin(1) - 1
  13. end_position = match_data.char_end(1)
  14. possible_entries << {
  15. screen_name: screen_name,
  16. indices: [start_position, end_position],
  17. }
  18. end
  19. end
  20. if block_given?
  21. possible_entries.each do |mention|
  22. yield mention[:screen_name], mention[:indices].first, mention[:indices].last
  23. end
  24. end
  25. possible_entries
  26. end
  27. def extract_hashtags_with_indices(text, _options = {})
  28. return [] unless text =~ /#/
  29. tags = []
  30. text.scan(Tag::HASHTAG_RE) do |hash_text, _|
  31. match_data = $LAST_MATCH_INFO
  32. start_position = match_data.char_begin(1) - 1
  33. end_position = match_data.char_end(1)
  34. after = $'
  35. if after =~ %r{\A://}
  36. hash_text.match(/(.+)(https?\Z)/) do |matched|
  37. hash_text = matched[1]
  38. end_position -= matched[2].char_length
  39. end
  40. end
  41. tags << {
  42. hashtag: hash_text,
  43. indices: [start_position, end_position],
  44. }
  45. end
  46. tags.each { |tag| yield tag[:hashtag], tag[:indices].first, tag[:indices].last } if block_given?
  47. tags
  48. end
  49. def extract_cashtags_with_indices(_text)
  50. [] # always returns empty array
  51. end
  52. end