diff options
author | Rhenium <rhenium@rhe.jp> | 2014-01-21 06:43:02 +0900 |
---|---|---|
committer | Rhenium <rhenium@rhe.jp> | 2014-01-21 06:43:02 +0900 |
commit | 174f0059afe8914554c454b6c04c80cf278a9d1e (patch) | |
tree | 60fe0664cd093d0b47385b70677574197304ece4 | |
parent | c2479259ae06d19999f08dd6e5f3a679725d0c27 (diff) | |
download | aclog-174f0059afe8914554c454b6c04c80cf278a9d1e.tar.gz |
use Twitter::Autolink to link usernames / urls / ...
-rw-r--r-- | app/helpers/application_helper.rb | 25 | ||||
-rw-r--r-- | app/helpers/tweets_helper.rb | 10 | ||||
-rw-r--r-- | app/views/tweets/_tweet.html.haml | 2 |
3 files changed, 10 insertions, 27 deletions
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 5700099..4c3e5a4 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,31 +1,10 @@ module ApplicationHelper + include Twitter::Autolink + def format_time(dt) dt.to_time.localtime("+09:00").strftime("%Y-%m-%d %H:%M:%S") end - def format_tweet_text(text) - ret = text.gsub(/<([a-z]+?)(?<!\\):(.+?)(?:(?<!\\):(.+?))?>/) do - case $1 - when "mention" - screen_name = CGI.unescape($2) - link_to("@#{screen_name}", "/#{screen_name}") - when "url" - n = [$3, $2.gsub(/(https?)%3A/, "\\1:")].map {|m| m.gsub("\\:", ":") } - link_to(*n) - when "hashtag" - hashtag = CGI.unescape($2) - link_to("##{hashtag}", "https://twitter.com/search?q=#{CGI.escape("##{hashtag}")}") - when "symbol" - symbol = CGI.unescape($2) - link_to("$#{symbol}", "https://twitter.com/search?q=#{CGI.escape("$#{symbol}")}") - else - $& - end - end - return ret - end - alias format_source_text format_tweet_text - def title(*args) content_for :title do (args.compact).join(" - ") diff --git a/app/helpers/tweets_helper.rb b/app/helpers/tweets_helper.rb index 332380b..704c28f 100644 --- a/app/helpers/tweets_helper.rb +++ b/app/helpers/tweets_helper.rb @@ -7,13 +7,17 @@ module TweetsHelper (favorites_truncate_count || Float::INFINITY) < [tweet.favorites_count, tweet.retweets_count].max end + def format_tweet_text(text) + text = sanitize(text) + text = auto_link(text, suppress_lists: true, username_include_symbol: true, username_url_base: "/") + text.gsub(/\r\n|\r|\n/, "<br />").html_safe + end + def link_to_source_text(source) if /^<a href="(.+?)" rel="nofollow">(.+?)<\/a>/ =~ source link_to $2, $1 - elsif /^<url:(.+?)(?<!\\):(.+?)?>$/ =~ source - link_to(*[$2, $1.gsub(/(https?)%3A/, "\\1:")].map {|m| m.gsub("\\:", ":") }) else - h source + source end end end diff --git a/app/views/tweets/_tweet.html.haml b/app/views/tweets/_tweet.html.haml index 3cf6f3f..a3b3146 100644 --- a/app/views/tweets/_tweet.html.haml +++ b/app/views/tweets/_tweet.html.haml @@ -42,7 +42,7 @@ - if tweet.in_reply_to %span.in_reply_to= link_to "in reply to @" + tweet.in_reply_to.user_screen_name, tweet_path(tweet.in_reply_to.id) .text - = simple_format(format_tweet_text(tweet.text)) + = format_tweet_text tweet.text .meta.clearfix %span.twitter_bird = link_to image_tag("bird_gray_16.png", alt: "Twitter"), tweet.twitter_url, target: "_blank" |