twitter-archive / twitter-text-rb

A library that does auto linking and extraction of usernames, lists and hashtags in tweets
611 stars 133 forks source link

HTML attrs for specific entity types #88

Open howardr opened 11 years ago

howardr commented 11 years ago

I am finding it difficult to use different html attributes for different entity types (i.e. url, screen name, entity, cashtag). I have ended up recreating the code here...

https://github.com/twitter/twitter-text-rb/blob/v1.6.1/lib/twitter-text/autolink.rb#L67

...to add type specific attrs. In my specific case, I could work around this by using class names, but was curious if you would accept a patch for type specific attributes? Another solution I was thinking would be passing in a block that gets called in #auto_link_entities to allow for specific attributes to be changed (very basic example below)

  Twitter::Rewriter.rewrite_entities(text, entities) do |entity, chars|
    if entity[:url]
      options = options.merge(block.call(:url) || {})
      link_to_url(entity, chars, options, &block)
    elsif entity[:hashtag]
      options = options.merge(block.call(:hashtag) || {})
      link_to_hashtag(entity, chars, options, &block)
    elsif entity[:screen_name]
      options = options.merge(block.call(:screen_name) || {})
      link_to_screen_name(entity, chars, options, &block)
    elsif entity[:cashtag]
      options = options.merge(block.call(:cashtag) || {})
      link_to_cashtag(entity, chars, options, &block)
    end
  end
jakl commented 11 years ago

Sure passing a block would be fine, we do that here (maybe using this existing block is even a solution): https://github.com/twitter/twitter-text-rb/blob/v1.6.1/lib/twitter-text/autolink.rb#L239

What attributes did you need? Can I have some context?