Skip to content

Commit

Permalink
Add hacker_news_share_link tag for sharing on Hacker News
Browse files Browse the repository at this point in the history
  • Loading branch information
tdg5 committed Sep 10, 2015
1 parent e711b2f commit 8a6a353
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/octopress-social.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ module Social
autoload :Disqus, 'octopress-social/disqus'
autoload :Email, 'octopress-social/email'
autoload :GitHub, 'octopress-social/github'

autoload :HackerNews, 'octopress-social/hacker-news'

def full_url(site, item)
unless root = site['url']
abort "Site url not configured. Please set url: http://your-site.com in Jekyll configuration file."
Expand Down Expand Up @@ -66,6 +67,7 @@ def item(context, input)
Liquid::Template.register_tag('email_share_url', Octopress::Social::Email::Tag)
Liquid::Template.register_tag('email_contact_link', Octopress::Social::Email::Tag)
Liquid::Template.register_tag('github_profile_link', Octopress::Social::GitHub::Tag)
Liquid::Template.register_tag('hacker_news_share_link', Octopress::Social::HackerNews::Tag)

if defined? Octopress::Docs
Octopress::Docs.add({
Expand Down
65 changes: 65 additions & 0 deletions lib/octopress-social/hacker-news.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# encoding: UTF-8
module Octopress
module Social
module HackerNews
extend self

attr_accessor :url, :config

DEFAULTS = {
'share_link_text' => 'Hacker News',
'share_link_title' => 'Share on HackerNews',
'share_title' => ':title - :url',
}

def set_config(site)
@config ||= begin
config = site['octopress_social'] || site
DEFAULTS.merge(config['hacker_news'] || {})
end
end

def set_url(site, item)
@url = Social.full_url(site, item)
end

def hacker_news_share_link(site, item)
%Q{<a
class="hacker-news-share-link"
href="#{hacker_news_share_url(site, item)}"
title="#{config['share_link_title']}">#{config['share_link_text']}</a>}
end

def hacker_news_share_url(site, item)
encoded_url = ERB::Util.url_encode(url)
encoded_share_title = ERB::Util.url_encode(share_title(site, item))
"http://news.ycombinator.com/submitlink".
concat("?t=#{encoded_share_title}").
concat("&u=#{encoded_url}")
end

def share_title(site, item)
(item['hacker_news_share_title'] || config['share_title'])
.gsub(':title', item['title'] || '')
.gsub(':url', url)
.strip
end

class Tag < Liquid::Tag
def initialize(tag, input, tokens)
@tag = tag.strip
@input = input.strip
end

def render(context)
site = context['site']
item = Octopress::Social.item(context, @input)

Octopress::Social::HackerNews.set_config(site)
Octopress::Social::HackerNews.set_url(site, item)
Octopress::Social::HackerNews.send(@tag, site, item).gsub(/(\s{2,}|\n)/, ' ').strip
end
end
end
end
end
1 change: 1 addition & 0 deletions test/site/_expected/post-loop.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ <h2>Share post</h2>
<a class="g-plus-share-link" href="https://plus.google.com/share?url=http%3A%2F%2Fexample.com%2Fblog%2Fshare-post%2F" title="Share on Google+">Google+</a>
<a class="facebook-share-link" href="https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fexample.com%2Fblog%2Fshare-post%2F" title="Share on Facebook">Facebook</a>
<a class="email-share-link" href="mailto:?subject=Share%20post%20by%20Guy%20McDude&body=Share%20post%20by%20Guy%20McDude%20-%20http%3A%2F%2Fexample.com%2Fblog%2Fshare-post%2F" title="Share via email">Email</a>
<a class="hacker-news-share-link" href="http://news.ycombinator.com/submitlink?t=Share%20post%20-%20http%3A%2F%2Fexample.com%2Fblog%2Fshare-post%2F&u=http%3A%2F%2Fexample.com%2Fblog%2Fshare-post%2F" title="Share on HackerNews">Hacker News</a>

Share URLs:
<a href="https://twitter.com/intent/tweet?text=Share%20post%20by%20%40octopress%20%23test%20-%20http%3A%2F%2Fexample.com%2Fblog%2Fshare-post%2F">Twitter</a>
Expand Down
1 change: 1 addition & 0 deletions test/site/_expected/share-page.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<a class="g-plus-share-link" href="https://plus.google.com/share?url=http%3A%2F%2Fexample.com%2Fblog%2Fshare-page.html" title="Share on Google+">Google+</a>
<a class="facebook-share-link" href="https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fexample.com%2Fblog%2Fshare-page.html" title="Share on Facebook">Facebook</a>
<a class="email-share-link" href="mailto:?subject=Cool%20Page%20by%20Guy%20McDude&body=Cool%20Page%20by%20Guy%20McDude%20-%20http%3A%2F%2Fexample.com%2Fblog%2Fshare-page.html" title="Share via email">Email</a>
<a class="hacker-news-share-link" href="http://news.ycombinator.com/submitlink?t=Cool%20Page%20-%20http%3A%2F%2Fexample.com%2Fblog%2Fshare-page.html&u=http%3A%2F%2Fexample.com%2Fblog%2Fshare-page.html" title="Share on HackerNews">Hacker News</a>

Share URLs:
<a href="https://twitter.com/intent/tweet?text=Cool%20Page%20by%20%40imathis%20%23awesome%20%23stuff%20-%20http%3A%2F%2Fexample.com%2Fblog%2Fshare-page.html">Twitter</a>
Expand Down
1 change: 1 addition & 0 deletions test/site/_expected/share-post/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<a class="g-plus-share-link" href="https://plus.google.com/share?url=http%3A%2F%2Fexample.com%2Fblog%2Fshare-post%2F" title="Share on Google+">Google+</a>
<a class="facebook-share-link" href="https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fexample.com%2Fblog%2Fshare-post%2F" title="Share on Facebook">Facebook</a>
<a class="email-share-link" href="mailto:?subject=Share%20post%20by%20Guy%20McDude&body=Share%20post%20by%20Guy%20McDude%20-%20http%3A%2F%2Fexample.com%2Fblog%2Fshare-post%2F" title="Share via email">Email</a>
<a class="hacker-news-share-link" href="http://news.ycombinator.com/submitlink?t=Share%20post%20-%20http%3A%2F%2Fexample.com%2Fblog%2Fshare-post%2F&u=http%3A%2F%2Fexample.com%2Fblog%2Fshare-post%2F" title="Share on HackerNews">Hacker News</a>

Share URLs:
<a href="https://twitter.com/intent/tweet?text=Share%20post%20by%20%40octopress%20%23test%20-%20http%3A%2F%2Fexample.com%2Fblog%2Fshare-post%2F">Twitter</a>
Expand Down
1 change: 1 addition & 0 deletions test/site/_includes/share.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
{% gplus_share_link %}
{% facebook_share_link %}
{% email_share_link %}
{% hacker_news_share_link %}

Share URLs:
<a href="{% tweet_url post %}">Twitter</a>
Expand Down
1 change: 1 addition & 0 deletions test/site/_site/post-loop.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ <h2>Share post</h2>
<a class="g-plus-share-link" href="https://plus.google.com/share?url=http%3A%2F%2Fexample.com%2Fblog%2Fshare-post%2F" title="Share on Google+">Google+</a>
<a class="facebook-share-link" href="https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fexample.com%2Fblog%2Fshare-post%2F" title="Share on Facebook">Facebook</a>
<a class="email-share-link" href="mailto:?subject=Share%20post%20by%20Guy%20McDude&body=Share%20post%20by%20Guy%20McDude%20-%20http%3A%2F%2Fexample.com%2Fblog%2Fshare-post%2F" title="Share via email">Email</a>
<a class="hacker-news-share-link" href="http://news.ycombinator.com/submitlink?t=Share%20post%20-%20http%3A%2F%2Fexample.com%2Fblog%2Fshare-post%2F&u=http%3A%2F%2Fexample.com%2Fblog%2Fshare-post%2F" title="Share on HackerNews">Hacker News</a>

Share URLs:
<a href="https://twitter.com/intent/tweet?text=Share%20post%20by%20%40octopress%20%23test%20-%20http%3A%2F%2Fexample.com%2Fblog%2Fshare-post%2F">Twitter</a>
Expand Down
1 change: 1 addition & 0 deletions test/site/_site/share-page.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<a class="g-plus-share-link" href="https://plus.google.com/share?url=http%3A%2F%2Fexample.com%2Fblog%2Fshare-page.html" title="Share on Google+">Google+</a>
<a class="facebook-share-link" href="https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fexample.com%2Fblog%2Fshare-page.html" title="Share on Facebook">Facebook</a>
<a class="email-share-link" href="mailto:?subject=Cool%20Page%20by%20Guy%20McDude&body=Cool%20Page%20by%20Guy%20McDude%20-%20http%3A%2F%2Fexample.com%2Fblog%2Fshare-page.html" title="Share via email">Email</a>
<a class="hacker-news-share-link" href="http://news.ycombinator.com/submitlink?t=Cool%20Page%20-%20http%3A%2F%2Fexample.com%2Fblog%2Fshare-page.html&u=http%3A%2F%2Fexample.com%2Fblog%2Fshare-page.html" title="Share on HackerNews">Hacker News</a>

Share URLs:
<a href="https://twitter.com/intent/tweet?text=Cool%20Page%20by%20%40imathis%20%23awesome%20%23stuff%20-%20http%3A%2F%2Fexample.com%2Fblog%2Fshare-page.html">Twitter</a>
Expand Down
1 change: 1 addition & 0 deletions test/site/_site/share-post/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<a class="g-plus-share-link" href="https://plus.google.com/share?url=http%3A%2F%2Fexample.com%2Fblog%2Fshare-post%2F" title="Share on Google+">Google+</a>
<a class="facebook-share-link" href="https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fexample.com%2Fblog%2Fshare-post%2F" title="Share on Facebook">Facebook</a>
<a class="email-share-link" href="mailto:?subject=Share%20post%20by%20Guy%20McDude&body=Share%20post%20by%20Guy%20McDude%20-%20http%3A%2F%2Fexample.com%2Fblog%2Fshare-post%2F" title="Share via email">Email</a>
<a class="hacker-news-share-link" href="http://news.ycombinator.com/submitlink?t=Share%20post%20-%20http%3A%2F%2Fexample.com%2Fblog%2Fshare-post%2F&u=http%3A%2F%2Fexample.com%2Fblog%2Fshare-post%2F" title="Share on HackerNews">Hacker News</a>

Share URLs:
<a href="https://twitter.com/intent/tweet?text=Share%20post%20by%20%40octopress%20%23test%20-%20http%3A%2F%2Fexample.com%2Fblog%2Fshare-post%2F">Twitter</a>
Expand Down

0 comments on commit 8a6a353

Please sign in to comment.