.. title: Alternative Social Buttons
.. slug: social_buttons
.. date: 2013-08-19 23:00:00 UTC-03:00
.. tags:
.. link:
.. description:
.. author: The Nikola Team
Using Alternative Social Buttons with Nikola
============================================
:Version: 7.8.6
.. class:: alert alert-info pull-right
.. contents::
The Default
-----------
By Default, the themes provided with Nikola will add to your pages a "slide in" widget at
the bottom right of the page, provided by Addthis. This is the HTML code for that:
.. code-block:: html
"""
You can change that using the ``SOCIAL_BUTTONS_CODE`` option in your conf.py. In some cases, just
doing that will be enough but in others, it won't. This document tries to describe all the bits
involved in making this work correctly.
Part 1: ``SOCIAL_BUTTONS_CODE``
Social sharing services like addthis and others will provide you a HTML snippet.
If it is self-contained, then just setting ``SOCIAL_BUTTONS_CODE`` may be enough.
Try :-)
Part 2: The theme
The ``SOCIAL_BUTTONS_CODE`` HTML fragment will be embedded *somewhere* by the theme. Whether that
is the correct place or not is not something the theme author can truly know, so it is possible that
you may have to tweak the ``base.html`` template to make it look good.
Part 3: ``BODY_END`` and ``EXTRA_HEAD_DATA``
Some social sharing code requires JS execution that depends on JQuery being available
(example: `SocialSharePrivacy `__). It's good
practice (and often, the only way that will work) to put those at the end of ````,
and one easy way to do that is to put them in ``BODY_END``
On the other hand, it's possible that it requires you to load some CSS files.
The right place for that is in the document's ```` so they should be added
in ``EXTRA_HEAD_DATA``
Part 4: assets
For sharing code that doesn't rely on a social sharing service, you may need to add CSS, Image, or JS
files to your site
ShareNice
---------
`Sharenice `__ is "written in order to provide social sharing features to
web developers and website administrators who wish to maintain and protect their users' privacy"
which sounds cool to me.
Let's go step by step into integrating the hosted version of ShareNice into a Nikola site.
For testing purposes, let's do it on a demo site::
$ nikola init --demo sharenice_test
A new site with example data has been created at sharenice_test.
See README.txt in that folder for more information.
$ cd sharenice_test/
To see what's going on, let's start Nikola in "auto mode". This should build the
site and open a web browser showing the default configuration, with the AddThis widget::
$ nikola auto -b
First, let's add the HTML snippet that will show the sharing options. In your conf.py, set this, which
is the HTML code suggested by ShareNice:
.. code-block:: python
SOCIAL_BUTTONS_CODE = """"""
BODY_END = """"""
And you should now see a sharing box at the bottom right of the page.
Main problem remaining is that it doesn't really look good and integrated in the page layout.
I suggest changing the code to this which looks nicer, but still has some placement issues:
.. code-block:: python
SOCIAL_BUTTONS_CODE = """"""
If anyone comes up with a better idea of styling/placement, just let me know ;-)
One bad bit of this so far is that you are now using a script from another site, and that
doesn't let Nikola perform as many optimizations to your page as it could.
So, if you really want to go the extra mile to save a few KB and round trips, you *could*
install your own copy from the `github repo `_ and
use that instead of the copy at sharenice.org.
Then, you can create your own theme inheriting from the one you are using and add the CSS
and JS files from ShareNice into your ``bundles`` configuration so they are combined and
minified.
SocialSharePrivacy
------------------
The Hard Way
~~~~~~~~~~~~
`SocialSharePrivacy `__ is "a jQuery plugin that
lets you add social share buttons to your website that don't allow the social sites to track
your users." Nice!
Let's go step-by-step into integrating SocialSharePrivacy into a Nikola site. To improve
privacy, they recommend you not use the hosted service so we'll do it the hard way, by
getting and distributing everything in our own site.
https://github.com/panzi/SocialSharePrivacy
For testing purposes, let's do it on a demo site::
$ nikola init --demo ssp_test
A new site with example data has been created at ssp_test.
See README.txt in that folder for more information.
$ cd ssp_test/
To see what's going on, let's start Nikola in "auto mode". This should build the
site and open a web browser showing the default configuration, with the AddThis widget::
$ nikola auto -b
Now, download `the current version `_
and unzip it. You will have a ``SocialSharePrivacy-master`` folder with lots of stuff in it.
First, we need to build it (this requires a working and modern uglifyjs, this may not be easy)::
$ cd SocialSharePrivacy-master
$ sh build.sh -m gplus,twitter,facebook,mail -s "/assets/css/socialshareprivacy.css" -a off
You will now have several files in a ``build`` folder. We need to bring them into the site::
$ cp -Rv SocialSharePrivacy-master/build/* files/
$ cp -R SocialSharePrivacy-master/images/ files/assets/
Edit your ``conf.py``:
.. code-block:: python
BODY_END = """
"""
SOCIAL_BUTTONS_CODE = """"""
In my experience this produces a broken, duplicate, semi-working thing. YMMV and if you make it work correctly, let me know how :-)
The Easy Way
~~~~~~~~~~~~
Go to http://panzi.github.io/SocialSharePrivacy/ and use the provided form to get the code. Make sure you check "I already use JQuery"
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
you have one.
It will give you 3 code snippets:
"Insert this once in the head of your page"
Put it in ``BODY_END``
"Insert this wherever you want a share widget displayed"
Put it in ``SOCIAL_BUTTONS_CODE``
"Insert this once anywhere after the other code"
Put it in ``BODY_END``
That should give you a working integration (not tested)