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

Add reddit_share_link tag for sharing on Reddit #9

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,35 @@ 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
```

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.

## Email sharing

Add convenient `mail:to` links which which helps readers
Expand Down
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 :Reddit, 'octopress-social/reddit'

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 @@ -62,6 +63,7 @@ def item(context, input)
Liquid::Template.register_tag('email_share_link', 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)

if defined? Octopress::Docs
Octopress::Docs.add({
Expand Down
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
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://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="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>

Comment links:
<a class="disqus-comments-link" title="View comments" href="http://example.com/blog/share-post/#disqus_thread">Comments</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://example.com/blog/share-page.html" 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-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="reddit-share-link" href="http://www.reddit.com/submit?title=Cool%20Page%20-%20http%3A%2F%2Fexample.com%2Fblog%2Fshare-page.html&url=http%3A%2F%2Fexample.com%2Fblog%2Fshare-page.html" title="Share on Reddit">Reddit</a>

Comment links:
<a class="disqus-comments-link" title="View comments" href="#disqus_thread">Comments</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://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="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>

Comment links:
<a class="disqus-comments-link" title="View comments" href="#disqus_thread">Comments</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,3 +10,4 @@
{% gplus_share_link %}
{% facebook_share_link %}
{% email_share_link %}
{% reddit_share_link %}
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://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="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>

Comment links:
<a class="disqus-comments-link" title="View comments" href="http://example.com/blog/share-post/#disqus_thread">Comments</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://example.com/blog/share-page.html" 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-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="reddit-share-link" href="http://www.reddit.com/submit?title=Cool%20Page%20-%20http%3A%2F%2Fexample.com%2Fblog%2Fshare-page.html&url=http%3A%2F%2Fexample.com%2Fblog%2Fshare-page.html" title="Share on Reddit">Reddit</a>

Comment links:
<a class="disqus-comments-link" title="View comments" href="#disqus_thread">Comments</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://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="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>

Comment links:
<a class="disqus-comments-link" title="View comments" href="#disqus_thread">Comments</a>
Expand Down