Permalink
Browse files

Add auto_link helper to parse twitter usernames

  • Loading branch information...
adamdawkins committed Jan 22, 2019
1 parent 6993d63 commit fe6dcd7c91e00f2130674e3c95e714f7a5ef19ea
Showing with 38 additions and 11 deletions.
  1. +20 −0 app/helpers/posts_helper.rb
  2. +18 −11 spec/helpers/posts_helper_spec.rb
@@ -1,2 +1,22 @@
module PostsHelper
def auto_link_regex
# Taken from https://github.com/kylewm/cassis-autolink-py/blob/master/cassis.py#L19

Regexp.new('(?:\\@[_a-zA-Z0-9]{1,17})|(?:(?:(?:(?:http|https|irc)?:\\/\\/(?:(?:[!$&-.0-9;=?A-Z_a-z]|(?:\\%[a-fA-F0-9]{2}))+(?:\\:(?:[!$&-.0-9;=?A-Z_a-z]|(?:\\%[a-fA-F0-9]{2}))+)?\\@)?)?(?:(?:(?:[a-zA-Z0-9][-a-zA-Z0-9]*\\.)+(?:(?:aero|arpa|asia|a[cdefgilmnoqrstuwxz])|(?:biz|b[abdefghijmnorstvwyz])|(?:cat|com|coop|c[acdfghiklmnoruvxyz])|d[ejkmoz]|(?:edu|e[cegrstu])|f[ijkmor]|(?:gov|g[abdefghilmnpqrstuwy])|h[kmnrtu]|(?:info|int|i[delmnoqrst])|j[emop]|k[eghimnrwyz]|l[abcikrstuvy]|(?:mil|museum|m[acdeghklmnopqrstuvwxyz])|(?:name|net|n[acefgilopruz])|(?:org|om)|(?:pro|p[aefghklmnrstwy])|qa|r[eouw]|s[abcdeghijklmnortuvyz]|(?:tel|travel|t[cdfghjklmnoprtvwz])|u[agkmsyz]|v[aceginu]|w[fs]|y[etu]|z[amw]))|(?:(?:25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9])\\.(?:25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[0-9])\\.(?:25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[0-9])\\.(?:25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[0-9])))(?:\\:\\d{1,5})?)(?:\\/(?:(?:[!#&-;=?-Z_a-z~])|(?:\\%[a-fA-F0-9]{2}))*)?)(?=\\b|\\s|$)', 'gi')
end

def auto_link(content)
links = content.scan(auto_link_regex)
links.each do |link|
if link.start_with?("@")
# Twitter
content.gsub!(link, twitter_link(link))
end
end
content
end

def twitter_link(username)
content_tag :a, username, class: "auto-link h-x-username", href: "https://twitter.com/#{username[1..-1]}"
end
end
@@ -1,15 +1,22 @@
require 'rails_helper'

# Specs in this file have access to a helper object that includes
# the PostsHelper. For example:
#
# describe PostsHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# expect(helper.concat_strings("this","that")).to eq("this that")
# end
# end
# end
RSpec.describe PostsHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
# tests copied from https://github.com/kylewm/cassis-autolink-py/blob/master/cassistest.py
describe "auto_link" do
context "Twitter usernames" do
it "adds links to @usernames" do
expect(helper.auto_link("@adamdawkins")).to eq '<a class="auto-link h-x-username" href="https://twitter.com/adamdawkins">@adamdawkins</a>'
end
it "preserves whitespace around username" do
expect(helper.auto_link(" @adamdawkins ")).to eq ' <a class="auto-link h-x-username" href="https://twitter.com/adamdawkins">@adamdawkins</a> '
end
it "links next to another word" do
expect(helper.auto_link("hi @adamdawkins")).to eq 'hi <a class="auto-link h-x-username" href="https://twitter.com/adamdawkins">@adamdawkins</a>'
end

it "skips no name" do
expect(helper.auto_link("@")).to eq '@'
end
end
end
end

0 comments on commit fe6dcd7

Please sign in to comment.