A new Riff-radio.org site with a static approach.

social_buttons.rst 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. .. title: Using Alternative Social Buttons with Nikola
  2. .. slug: social_buttons
  3. .. date: 2013-08-19 23:00:00 UTC-03:00
  4. .. tags:
  5. .. link:
  6. .. description:
  7. .. author: The Nikola Team
  8. :Version: 8.2.3
  9. .. class:: alert alert-primary float-md-right
  10. .. contents::
  11. The Default
  12. -----------
  13. By Default, the themes provided with Nikola will add to your pages a "slide in" widget at
  14. the bottom right of the page, provided by Addthis. This is the HTML code for that:
  15. .. code-block:: html
  16. <!-- Social buttons -->
  17. <div id="addthisbox" class="addthis_toolbox addthis_peekaboo_style
  18. addthis_default_style addthis_label_style addthis_32x32_style">
  19. <a class="addthis_button_more">Share</a>
  20. <ul><li><a class="addthis_button_facebook"></a>
  21. <li><a class="addthis_button_google_plusone_share"></a>
  22. <li><a class="addthis_button_linkedin"></a>
  23. <li><a class="addthis_button_twitter"></a>
  24. </ul>
  25. </div>
  26. <script src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-4f7088a56bb93798"></script>
  27. <!-- End of social buttons -->
  28. """
  29. You can change that using the ``SOCIAL_BUTTONS_CODE`` option in your conf.py. In some cases, just
  30. doing that will be enough but in others, it won't. This document tries to describe all the bits
  31. involved in making this work correctly.
  32. Part 1: ``SOCIAL_BUTTONS_CODE``
  33. Social sharing services like addthis and others will provide you a HTML snippet.
  34. If it is self-contained, then just setting ``SOCIAL_BUTTONS_CODE`` may be enough.
  35. Try :-)
  36. Part 2: The theme
  37. The ``SOCIAL_BUTTONS_CODE`` HTML fragment will be embedded *somewhere* by the theme. Whether that
  38. is the correct place or not is not something the theme author can truly know, so it is possible that
  39. you may have to tweak the ``base.html`` template to make it look good.
  40. Part 3: ``BODY_END`` and ``EXTRA_HEAD_DATA``
  41. Some social sharing code requires JS execution that depends on JQuery being available
  42. (example: `SocialSharePrivacy <https://github.com/panzi/SocialSharePrivacy>`__). It's good
  43. practice (and often, the only way that will work) to put those at the end of ``<BODY>``,
  44. and one easy way to do that is to put them in ``BODY_END``
  45. On the other hand, it's possible that it requires you to load some CSS files.
  46. The right place for that is in the document's ``<HEAD>`` so they should be added
  47. in ``EXTRA_HEAD_DATA``
  48. Part 4: assets
  49. For sharing code that doesn't rely on a social sharing service, you may need to add CSS, Image, or JS
  50. files to your site
  51. ShareNice
  52. ---------
  53. `ShareNice <https://graingert.co.uk/shareNice/>`__ is "written in order to provide social sharing features to
  54. web developers and website administrators who wish to maintain and protect their users' privacy"
  55. which sounds cool to me.
  56. Let's go step by step into integrating the hosted version of ShareNice into a Nikola site.
  57. For testing purposes, let's do it on a demo site::
  58. $ nikola init --demo sharenice_test
  59. A new site with example data has been created at sharenice_test.
  60. See README.txt in that folder for more information.
  61. $ cd sharenice_test/
  62. To see what's going on, let's start Nikola in "auto mode". This should build the
  63. site and open a web browser showing the default configuration, with the AddThis widget::
  64. $ nikola auto -b
  65. First, let's add the HTML snippet that will show the sharing options. In your conf.py, set this, which
  66. is the HTML code suggested by ShareNice:
  67. .. code-block:: python
  68. SOCIAL_BUTTONS_CODE = """<div id="shareNice" data-share-label="Share"
  69. data-color-scheme="black" data-icon-size="32" data-panel-bottom="plain"
  70. data-services="plus.google.com,facebook.com,digg.com,email,delicious.com,twitter.com"
  71. style="float:right"></div>"""
  72. BODY_END = """<script src="https://graingert.co.uk/shareNice/code.js"></script>"""
  73. And you should now see a sharing box at the bottom right of the page.
  74. Main problem remaining is that it doesn't really look good and integrated in the page layout.
  75. I suggest changing the code to this which looks nicer, but still has some placement issues:
  76. .. code-block:: python
  77. SOCIAL_BUTTONS_CODE = """<div id="shareNice" data-share-label="Share"
  78. data-color-scheme="black" data-icon-size="32" data-panel-bottom="plain"
  79. data-services="plus.google.com,facebook.com,email,twitter.com"
  80. style="position: absolute; left: 20px; top: 60px;"></div>"""
  81. If anyone comes up with a better idea of styling/placement, just let me know ;-)
  82. One bad bit of this so far is that you are now using a script from another site, and that
  83. doesn't let Nikola perform as many optimizations to your page as it could.
  84. So, if you really want to go the extra mile to save a few KB and round trips, you *could*
  85. install your own copy from the `github repo <https://github.com/mischat/shareNice>`_ and
  86. use that instead of the copy at `ShareNice <https://graingert.co.uk/shareNice>`_.
  87. Then, you can create your own theme inheriting from the one you are using and add the CSS
  88. and JS files from ShareNice into your ``bundles`` configuration so they are combined and
  89. minified.
  90. SocialSharePrivacy
  91. ------------------
  92. The Hard Way
  93. ~~~~~~~~~~~~
  94. `SocialSharePrivacy <https://github.com/panzi/SocialSharePrivacy>`__ is "a jQuery plugin that
  95. lets you add social share buttons to your website that don't allow the social sites to track
  96. your users." Nice!
  97. Let's go step-by-step into integrating SocialSharePrivacy into a Nikola site. To improve
  98. privacy, they recommend you not use the hosted service so we'll do it the hard way, by
  99. getting and distributing everything in our own site.
  100. https://github.com/panzi/SocialSharePrivacy
  101. For testing purposes, let's do it on a demo site::
  102. $ nikola init --demo ssp_test
  103. A new site with example data has been created at ssp_test.
  104. See README.txt in that folder for more information.
  105. $ cd ssp_test/
  106. To see what's going on, let's start Nikola in "auto mode". This should build the
  107. site and open a web browser showing the default configuration, with the AddThis widget::
  108. $ nikola auto -b
  109. Now, download `the current version <https://github.com/panzi/SocialSharePrivacy/archive/master.zip>`_
  110. and unzip it. You will have a ``SocialSharePrivacy-master`` folder with lots of stuff in it.
  111. First, we need to build it (this requires a working and modern uglifyjs, this may not be easy)::
  112. $ cd SocialSharePrivacy-master
  113. $ sh build.sh -m gplus,twitter,facebook,mail -s "/assets/css/socialshareprivacy.css" -a off
  114. You will now have several files in a ``build`` folder. We need to bring them into the site::
  115. $ cp -Rv SocialSharePrivacy-master/build/* files/
  116. $ cp -R SocialSharePrivacy-master/images/ files/assets/
  117. Edit your ``conf.py``:
  118. .. code-block:: python
  119. BODY_END = """
  120. <script src="/javascripts/jquery.socialshareprivacy.min.js"></script>
  121. <script>
  122. $(document).ready(function () {
  123. $('.share').socialSharePrivacy();
  124. });
  125. </script>
  126. """
  127. SOCIAL_BUTTONS_CODE = """<div class="share"></div>"""
  128. In my experience this produces a broken, duplicate, semi-working thing. YMMV and if you make it work correctly, let me know how :-)
  129. The Easy Way
  130. ~~~~~~~~~~~~
  131. Go to https://panzi.github.io/SocialSharePrivacy/ and use the provided form to get the code. Make sure you check "I already use JQuery"
  132. if you are using one of the themes that require it, like site or default, select the services you want, and use your disqus name if
  133. you have one.
  134. It will give you 3 code snippets:
  135. "Insert this once in the head of your page"
  136. Put it in ``BODY_END``
  137. "Insert this wherever you want a share widget displayed"
  138. Put it in ``SOCIAL_BUTTONS_CODE``
  139. "Insert this once anywhere after the other code"
  140. Put it in ``BODY_END``
  141. That should give you a working integration (not tested)