Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose URL tags and add support for Reddit and Hacker News #13

Closed
wants to merge 10 commits into from
62 changes: 61 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ Sharing tags:
```
{% tweet_button %}
{% tweet_link %} # Tweet with a (no js) link
{% tweet_url %} # URL to tweet with a (no js) link. URL only, no additional HTML markup included.
```

The tweet button and tweet link will open a new page with a composed tweet in the format in your Twitter configuration, `:title by :username - :url :hashtags`.
Expand Down Expand Up @@ -176,6 +177,7 @@ Sharing tags:
{% facebook_like_button %}
{% facebook_send_button %} # For private sharing
{% facebook_share_link %} # share with a (no js) link
{% facebook_share_url %} # URL to share with a (no js) link. URL only, no additional HTML markup included.
```

Friend and Follow tags:
Expand Down Expand Up @@ -225,6 +227,7 @@ Sharing tags:
{% gplus_one_button %}
{% gplus_share_button %}
{% gplus_share_link %} # Share with a (no js) link
{% gplus_share_url %} # URL to share with a (no js) link. URL only, no additional HTML markup included.
```

Follow tags:
Expand All @@ -234,6 +237,62 @@ Follow tags:
{% gplus_profile_link %}
```

## Reddit

Configure this plugin in your site's `_config.yml`. No configurations are required. Here are the configuration defaults.

```yaml
reddit:
share_link_text: Reddit # Configure the link text
share_link_title: Share on Reddit # Title of the share link
share_title: :title - :url # The suggested title auto-filled in the reddit form
```

To include a custom auto-filled share title for a post or page, include a
`reddit_share_title` attribute in the frontmatter of the post/page like this:

```yaml
reddit_share_title: "Totally not click bait! :title - :url"
```

### Reddit Tags

Sharing tags:
```
{% reddit_share_link %} # Share with a (no js) link
{% reddit_share_url %} # URL to share with a (no js) link. URL only, no additional HTML markup included.
```

The share button will open a new page with a pre-filled Reddit submission form
with a title derived from the `share_title` or page/post specific
`reddit_share_title` configuration discussed above.

## Hacker News

Configure this plugin in your site's `_config.yml`. No configurations are required. Here are the configuration defaults.

```yaml
hacker_news:
share_link_text: Hacker News # Configure the link text
share_link_title: Share on Hacker News # Title of the share link
share_title: :title - :url # The suggested title auto-filled in the Hacker News form
```

To include a custom auto-filled share title for a post or page, include a
`hacker_news_share_title` attribute in the frontmatter of the post/page like this:

```yaml
hacker_news_share_title: "Totally not click bait! :title - :url"
```

### Hacker News Tags

Sharing tags:
```
{% hacker_news_share_link %} # Share with a (no js) link
{% hacker_news_share_url %} # URL to share with a (no js) link. URL only, no additional HTML markup included.
```

## Email sharing

Add convenient `mail:to` links which which helps readers
Expand All @@ -259,7 +318,8 @@ used to generate a subject and body for a sharing email link.

```
{% email_share_link %} # Share a post or page over email
{% email_contact_link %} # Contac the site's author
{% email_share_url %} # URL to share a post or page over email with a (no js) link. URL only, no additional HTML markup included.
{% email_contact_link %} # Contact the site's author
```

If you want, you may customize an email subject or message on
Expand Down
12 changes: 11 additions & 1 deletion lib/octopress-social.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ module Social
autoload :Disqus, 'octopress-social/disqus'
autoload :Email, 'octopress-social/email'
autoload :GitHub, 'octopress-social/github'

autoload :Reddit, 'octopress-social/reddit'
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 All @@ -40,17 +42,20 @@ def item(context, input)
Liquid::Template.register_tag('tweet', Octopress::Social::Twitter::Tweet)
Liquid::Template.register_tag('tweet_button', Octopress::Social::Twitter::Tag)
Liquid::Template.register_tag('tweet_link', Octopress::Social::Twitter::Tag)
Liquid::Template.register_tag('tweet_url', Octopress::Social::Twitter::Tag)
Liquid::Template.register_tag('twitter_script_tag', Octopress::Social::Twitter::Tag)
Liquid::Template.register_tag('twitter_follow_button', Octopress::Social::Twitter::Tag)
Liquid::Template.register_tag('twitter_profile_link', Octopress::Social::Twitter::Tag)
Liquid::Template.register_tag('gplus_share_button', Octopress::Social::GooglePlus::Tag)
Liquid::Template.register_tag('gplus_share_link', Octopress::Social::GooglePlus::Tag)
Liquid::Template.register_tag('gplus_share_url', Octopress::Social::GooglePlus::Tag)
Liquid::Template.register_tag('gplus_one_button', Octopress::Social::GooglePlus::Tag)
Liquid::Template.register_tag('gplus_follow_button', Octopress::Social::GooglePlus::Tag)
Liquid::Template.register_tag('gplus_profile_link', Octopress::Social::GooglePlus::Tag)
Liquid::Template.register_tag('gplus_script_tag', Octopress::Social::GooglePlus::Tag)
Liquid::Template.register_tag('facebook_like_button', Octopress::Social::Facebook::Tag)
Liquid::Template.register_tag('facebook_share_link', Octopress::Social::Facebook::Tag)
Liquid::Template.register_tag('facebook_share_url', Octopress::Social::Facebook::Tag)
Liquid::Template.register_tag('facebook_send_button', Octopress::Social::Facebook::Tag)
Liquid::Template.register_tag('facebook_follow_button', Octopress::Social::Facebook::Tag)
Liquid::Template.register_tag('facebook_profile_link', Octopress::Social::Facebook::Tag)
Expand All @@ -60,8 +65,13 @@ def item(context, input)
Liquid::Template.register_tag('disqus_comments', Octopress::Social::Disqus::Tag)
Liquid::Template.register_tag('disqus_comments_link', Octopress::Social::Disqus::Tag)
Liquid::Template.register_tag('email_share_link', Octopress::Social::Email::Tag)
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('reddit_share_link', Octopress::Social::Reddit::Tag)
Liquid::Template.register_tag('reddit_share_url', Octopress::Social::Reddit::Tag)
Liquid::Template.register_tag('hacker_news_share_link', Octopress::Social::HackerNews::Tag)
Liquid::Template.register_tag('hacker_news_share_url', Octopress::Social::HackerNews::Tag)

if defined? Octopress::Docs
Octopress::Docs.add({
Expand Down
6 changes: 5 additions & 1 deletion lib/octopress-social/email.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,14 @@ def message(site, item)

def email_share_link(site, item)
%Q{<a class="email-share-link"
href="mailto:?subject=#{subject(site, item)}&body=#{message(site, item)}"
href="#{email_share_url(site, item)}"
title="#{config['share_link_title']}">#{config['share_link_text']}</a>}
end

def email_share_url(site, item)
"mailto:?subject=#{subject(site, item)}&body=#{message(site, item)}"
end

def email_contact_link(site, item)
%Q{<a
class="email-contact-link"
Expand Down
19 changes: 11 additions & 8 deletions lib/octopress-social/facebook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,19 @@ def set_url(site, item)
end

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

def facebook_share_url(site, item)
encoded_url = ERB::Util.url_encode(url)
if config['app_id']
%Q{<a class="facebook-share-link" href="https://www.facebook.com/dialog/share?
app_id=#{config['app_id']}
&href=#{url}&redirect_uri=#{url}"
title="#{config['share_link_title']}">#{config['share_link_text']}</a>
}
"https://www.facebook.com/dialog/share?".
concat("app_id=#{config['app_id']}").
concat("&href=#{encoded_url}&redirect_uri=#{encoded_url}")
else
%Q{<a class="facebook-share-link"
href="https://www.facebook.com/sharer/sharer.php?u=#{url}"
title="#{config['share_link_title']}">#{config['share_link_text']}</a>}
"https://www.facebook.com/sharer/sharer.php?u=#{encoded_url}"
end
end

Expand Down
10 changes: 8 additions & 2 deletions lib/octopress-social/google-plus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,18 @@ def config(site=nil)
end

def gplus_share_link(site, item)
%Q{<a
%Q{<a
class="g-plus-share-link"
href="https://plus.google.com/share?url=#{Social.full_url(site, item)}"
href="#{gplus_share_url(site, item)}"
title="#{config['share_link_title']}">#{config['share_link_text']}</a>}
end

def gplus_share_url(site, item)
url = Social.full_url(site, item)
encoded_url = ERB::Util.url_encode(url)
"https://plus.google.com/share?url=#{encoded_url}"
end

def gplus_one_button(site, item)
%Q{<div class="g-plusone" data-href="#{Social.full_url(site, item)}" #{count('share')} #{width} #{size}></div>}
end
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
65 changes: 65 additions & 0 deletions lib/octopress-social/reddit.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# encoding: UTF-8
module Octopress
module Social
module Reddit
extend self

attr_accessor :url, :config

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

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

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

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

def reddit_share_url(site, item)
encoded_url = ERB::Util.url_encode(url)
encoded_share_title = ERB::Util.url_encode(share_title(site, item))
"http://www.reddit.com/submit".
concat("?title=#{encoded_share_title}").
concat("&url=#{encoded_url}")
end

def share_title(site, item)
(item['reddit_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::Reddit.set_config(site)
Octopress::Social::Reddit.set_url(site, item)
Octopress::Social::Reddit.send(@tag, site, item).gsub(/(\s{2,}|\n)/, ' ').strip
end
end
end
end
end
10 changes: 7 additions & 3 deletions lib/octopress-social/twitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,19 @@ def set_url(site, item)
end

def tweet_link(site, item)
%Q{<a
%Q{<a
class="twitter-share-link"
href="https://twitter.com/intent/tweet?&text=#{ERB::Util.url_encode(message(site, item)).strip}"
href="#{tweet_url(site, item)}"
title="#{config['tweet_link_title']}">#{config['tweet_link_text']}</a>}
end

def tweet_url(site, item)
"https://twitter.com/intent/tweet?text=#{ERB::Util.url_encode(message(site, item)).strip}"
end

def tweet_button(site, item)
%Q{
<a href="https://twitter.com/share" class="twitter-share-button"
<a href="https://twitter.com/share" class="twitter-share-button"
#{'data-size="large"' if config['size'] == 'large'}
#{'data-count="none"' if !config['tweet_count']}
data-url="#{url}"
Expand Down
16 changes: 13 additions & 3 deletions test/site/_expected/post-loop.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,20 @@ <h2>Share post</h2>
<div class="g-plus" data-action="share" data-href="http://example.com/blog/share-post/" data-annotation="none" data-width="" data-size="medium"></div>

Share Links:
<a class="twitter-share-link" href="https://twitter.com/intent/tweet?&text=Share%20post%20by%20%40octopress%20%23test%20-%20http%3A%2F%2Fexample.com%2Fblog%2Fshare-post%2F" title="Share on Twitter">Twitter</a>
<a class="g-plus-share-link" href="https://plus.google.com/share?url=http://example.com/blog/share-post/" title="Share on Google+">Google+</a>
<a class="facebook-share-link" href="https://www.facebook.com/sharer/sharer.php?u=http://example.com/blog/share-post/" title="Share on Facebook">Facebook</a>
<a class="twitter-share-link" href="https://twitter.com/intent/tweet?text=Share%20post%20by%20%40octopress%20%23test%20-%20http%3A%2F%2Fexample.com%2Fblog%2Fshare-post%2F" title="Share on Twitter">Twitter</a>
<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="reddit-share-link" href="http://www.reddit.com/submit?title=Share%20post%20-%20http%3A%2F%2Fexample.com%2Fblog%2Fshare-post%2F&url=http%3A%2F%2Fexample.com%2Fblog%2Fshare-post%2F" title="Share on Reddit">Reddit</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>
<a href="https://plus.google.com/share?url=http%3A%2F%2Fexample.com%2Fblog%2Fshare-post%2F">Google+</a>
<a href="https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fexample.com%2Fblog%2Fshare-post%2F">Facebook</a>
<a href="mailto:?subject=Share%20post%20by%20Guy%20McDude&body=Share%20post%20by%20Guy%20McDude%20-%20http%3A%2F%2Fexample.com%2Fblog%2Fshare-post%2F">Email</a>
<a href="http://www.reddit.com/submit?title=Share%20post%20-%20http%3A%2F%2Fexample.com%2Fblog%2Fshare-post%2F&url=http%3A%2F%2Fexample.com%2Fblog%2Fshare-post%2F">Reddit</a>
<a 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">Hacker News</a>

Comment links:
<a class="disqus-comments-link" title="View comments" href="http://example.com/blog/share-post/#disqus_thread">Comments</a>
Expand Down
Loading