module Twitter::TwitterText::Rewriter
A module provides base methods to rewrite usernames, lists, hashtags and URLs.
Public Instance Methods
rewrite(text, options = {})
click to toggle source
# File lib/twitter-text/rewriter.rb, line 33 def rewrite(text, options = {}) [:hashtags, :urls, :usernames_or_lists].inject(text) do |key| options[key] ? send(:"rewrite_#{key}", text, &options[key]) : text end end
rewrite_entities(text, entities) { |entity, codepoints| ... }
click to toggle source
# File lib/twitter-text/rewriter.rb, line 9 def rewrite_entities(text, entities) codepoints = text.to_s.to_codepoint_a # sort by start index entities = entities.sort_by do |entity| indices = entity.respond_to?(:indices) ? entity.indices : entity[:indices] indices.first end result = [] last_index = entities.inject(0) do |index, entity| indices = entity.respond_to?(:indices) ? entity.indices : entity[:indices] result << codepoints[index...indices.first] result << yield(entity, codepoints) indices.last end result << codepoints[last_index..-1] result.flatten.join end
rewrite_urls(text) { |entity| ... }
click to toggle source
# File lib/twitter-text/rewriter.rb, line 60 def rewrite_urls(text) entities = Extractor.extract_urls_with_indices(text, :extract_url_without_protocol => false) rewrite_entities(text, entities) do |entity, codepoints| yield(entity[:url]) end end
rewrite_usernames_or_lists(text) { |at, entity, list_slug| ... }
click to toggle source
# File lib/twitter-text/rewriter.rb, line 40 def rewrite_usernames_or_lists(text) entities = Extractor.extract_mentions_or_lists_with_indices(text) rewrite_entities(text, entities) do |entity, codepoints| at = codepoints[entity[:indices].first] list_slug = entity[:list_slug] list_slug = nil if list_slug.empty? yield(at, entity[:screen_name], list_slug) end end