share_post.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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').get_text().strip()
  15. sub_title = ''
  16. if hasattr(content, 'subtitle'):
  17. sub_title = ' ' + BeautifulSoup(content.subtitle, 'html.parser').get_text().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(BeautifulSoup(content.summary, 'html.parser').get_text().strip().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%s' % (title, quote(' '), url)).encode('utf-8')
  31. diaspora_link = 'https://sharetodiaspora.github.io/?title=%s&url=%s' % (title, url)
  32. facebook_link = 'http://www.facebook.com/sharer/sharer.php?s=100&p%%5Burl%%5D=%s' % url
  33. gplus_link = 'https://plus.google.com/share?url=%s' % url
  34. twitter_link = 'http://twitter.com/home?status=%s' % tweet
  35. linkedin_link = 'https://www.linkedin.com/shareArticle?mini=true&url=%s&title=%s&summary=%s&source=%s' % (
  36. url, title, summary, url
  37. )
  38. mail_link = 'mailto:?subject=%s&body=%s' % (title, url)
  39. share_links = {
  40. 'diaspora': diaspora_link,
  41. 'twitter': twitter_link,
  42. 'facebook': facebook_link,
  43. 'google-plus': gplus_link,
  44. 'linkedin': linkedin_link,
  45. 'email': mail_link
  46. }
  47. content.share_post = share_links
  48. def register():
  49. signals.content_object_init.connect(share_post)