share_post.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. """
  2. Share Post
  3. ==========
  4. This plugin adds share URL to article. These links are textual which means no
  5. online tracking of your readers.
  6. """
  7. from bs4 import BeautifulSoup
  8. try:
  9. from urllib.parse import quote
  10. except ImportError:
  11. from urllib import quote
  12. from pelican import signals, contents
  13. def article_title(content):
  14. main_title = BeautifulSoup(content.title, 'html.parser').prettify().strip()
  15. sub_title = ''
  16. if hasattr(content, 'subtitle'):
  17. sub_title = BeautifulSoup(content.subtitle, 'html.parser').prettify().strip()
  18. return quote(('%s %s' % (main_title, sub_title)).encode('utf-8'))
  19. def article_url(content):
  20. site_url = content.settings['SITEURL']
  21. return quote(('%s/%s' % (site_url, content.url)).encode('utf-8'))
  22. def article_summary(content):
  23. return quote(content.summary.encode('utf-8'))
  24. def share_post(content):
  25. if isinstance(content, contents.Static):
  26. return
  27. title = article_title(content)
  28. url = article_url(content)
  29. summary = article_summary(content)
  30. tweet = '%s %s' % (title, url)
  31. facebook_link = 'http://www.facebook.com/sharer/sharer.php?s=100' \
  32. '&p[url]=%s&p[images][0]=&p[title]=%s&p[summary]=%s' \
  33. % (url, title, summary)
  34. gplus_link = 'https://plus.google.com/share?url=%s' % url
  35. twitter_link = 'http://twitter.com/home?status=%s' % tweet
  36. mail_link = 'mailto:?subject=%s&body=%s' % (title, url)
  37. share_links = {'twitter': twitter_link,
  38. 'facebook': facebook_link,
  39. 'google-plus': gplus_link,
  40. 'email': mail_link
  41. }
  42. content.share_post = share_links
  43. def register():
  44. signals.content_object_init.connect(share_post)