From 9a9f9bdb834acfb3e463d6c8b67c4c2256855208 Mon Sep 17 00:00:00 2001 From: Talha Mansoor Date: Mon, 20 Jan 2014 23:21:56 +0500 Subject: [PATCH] Add plugin to add share URLs to article --- share_post/README.md | 65 ++++++++++++++++++++++++++++++++++++++++ share_post/__init__.py | 1 + share_post/share_post.py | 58 +++++++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+) create mode 100644 share_post/README.md create mode 100644 share_post/__init__.py create mode 100644 share_post/share_post.py diff --git a/share_post/README.md b/share_post/README.md new file mode 100644 index 0000000..9646ef5 --- /dev/null +++ b/share_post/README.md @@ -0,0 +1,65 @@ +Share Post +========== + +A Pelican plugin to create share URLs of article + +Copyright (c) Talha Mansoor + +Author | Talha Mansoor +----------------|----- +Author Email | talha131@gmail.com +Author Homepage | http://onCrashReboot.com +Github Account | https://github.com/talha131 + +Why do you need it? +=================== + +Almost all website have share widgets to let readers share posts on social +networks. Most of these widgets are used by vendors for online tracking. These +widgets are also visual which quite often become a distraction and negatively +affect readers attention. + +`share_post` creates old school URLs for some popular sites which your theme +can use. These links do not have the ability to track the users. They can also +be unobtrusive depending on how Pelican theme uses them. + +Requirements +============ + +`share_post` requires BeautifulSoup + +```bash +pip install beautifulsoup4 +``` + +How to Use +========== + +`share_post` adds a dictionary attribute to `article` which can be accessed via +`article.share_post`. Keys of the dictionary are as follows, + +1. `facebook` +1. `google-plus` +1. `email` +1. `twitter` + +Template Example +================ + +```python +{% if article.share_post and article.status != 'draft' %} +
+

+ Share on: + Twitter + ❄ + Facebook + ❄ + Google+ + ❄ + Email +

+
+{% endif %} +``` + diff --git a/share_post/__init__.py b/share_post/__init__.py new file mode 100644 index 0000000..428e32a --- /dev/null +++ b/share_post/__init__.py @@ -0,0 +1 @@ +from .share_post import * diff --git a/share_post/share_post.py b/share_post/share_post.py new file mode 100644 index 0000000..f4fc7c3 --- /dev/null +++ b/share_post/share_post.py @@ -0,0 +1,58 @@ +""" +Share Post +========== + +This plugin adds share URL to article. These links are textual which means no +online tracking of your readers. +""" + +from bs4 import BeautifulSoup +try: + from urllib.parse import quote +except ImportError: + from urllib import quote +from pelican import signals, contents + + +def article_title(content): + main_title = BeautifulSoup(content.title, 'html.parser').prettify().strip() + sub_title = '' + if hasattr(content, 'subtitle'): + sub_title = BeautifulSoup(content.subtitle, 'html.parser').prettify().strip() + return quote(('%s %s' % (main_title, sub_title)).encode('utf-8')) + + +def article_url(content): + site_url = content.settings['SITEURL'] + return quote(('%s/%s' % (site_url, content.url)).encode('utf-8')) + + +def article_summary(content): + return quote(content.summary.encode('utf-8')) + + +def share_post(content): + if isinstance(content, contents.Static): + return + title = article_title(content) + url = article_url(content) + summary = article_summary(content) + + tweet = '%s %s' % (title, url) + facebook_link = 'http://www.facebook.com/sharer/sharer.php?s=100' \ + '&p[url]=%s&p[images][0]=&p[title]=%s&p[summary]=%s' \ + % (url, title, summary) + gplus_link = 'https://plus.google.com/share?url=%s' % url + twitter_link = 'http://twitter.com/home?status=%s' % tweet + mail_link = 'mailto:?subject=%s&body=%s' % (title, url) + + share_links = {'twitter': twitter_link, + 'facebook': facebook_link, + 'google-plus': gplus_link, + 'email': mail_link + } + content.share_post = share_links + + +def register(): + signals.content_object_init.connect(share_post)