Last active
December 23, 2015 03:09
-
-
Save seanredmond/6571611 to your computer and use it in GitHub Desktop.
Jekyll tag plugin for quickly making links to Twitter user timelines and hastag searches
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# TwitterLink Jekyll Plugin | |
# Copyright (C) 2013 Sean Redmond - MIT License | |
# https://gist.github.com/seanredmond/6571611 | |
# | |
# Quickly make links to people and hashtags on Twitter | |
# | |
# Usage: | |
# Link to a user's timeline: {% twlink @user %} | |
# Link to a hashtag search: {% twlink #hashtag %} | |
module Jekyll | |
class TwitterLink < Liquid::Tag | |
safe = true | |
priority = :normal | |
def initialize(tag_name, text, tokens) | |
super | |
@twitter_token = text.strip | |
end | |
# Return URL for a personal timeline | |
def person_link(person) | |
"https://twitter.com/#{person}" | |
end | |
# Return URL for a hastag search | |
def hash_link(hashtag) | |
"https://twitter.com/search?q=%23#{hashtag}" | |
end | |
# Return the proper kind of link | |
def make_link(match) | |
if match == nil | |
raise "TwitterLink plugin can't identify #{@twitter_token} as either a user handle (beginning with '@') or hashtag (beginning with '#')" | |
end | |
if match[1] == '@' | |
return person_link(match[2]) | |
end | |
return hash_link(match[2]) | |
end | |
def render(context) | |
href = make_link(/^([@#])(.\S+)$/.match(@twitter_token)) | |
%Q|<a href="#{href}">#{@twitter_token}</a>| | |
end | |
end | |
end | |
Liquid::Template.register_tag('twlink', Jekyll::TwitterLink) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment