闭社主体 forked from https://github.com/tootsuite/mastodon
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.

64 lines
1.6 KiB

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