pelican-themes icon indicating copy to clipboard operation
pelican-themes copied to clipboard

Reduce code duplication in Disqus code blocks

Open adamatan opened this issue 10 years ago • 3 comments

Disqus

Disqus is a popular commenting plugin, used by the following themes:

Just-Read, SoMA, SoMA2, aboutwilson, backdrop, bootlex, bootstrap, bootstrap2, bootstrap2-dark, bricks, brownstone, built-texts, cebong, dev-random, dev-random2, foundation-default-colours, franticworld, gum, lightweight, mnmlist, monospace, new-bootstrap2, nmnlist, notebook, notmyidea-cms, notmyidea-cms-fr, pelican-bootstrap3, photowall, simple-bootstrap, sneakyidea, subtle, syte, tuxlite_tbs, tuxlite_zf, waterspill, waterspill-en, zurb-F5-basic

Code duplication

Almost all themes just duplicate the same piece of Disqus code block. It's the exact same code block everywhere, which looks like:

{% if DISQUS_SITENAME %}
    <same div code in every theme, uses the {{ DISQUS_SITENAME }} once>
    ...
    ...
    ...
{% endif %}
  • https://github.com/getpelican/pelican-themes/blob/master/syte/templates/article.html#L39
  • https://github.com/getpelican/pelican-themes/blob/master/gum/templates/article.html#L35
  • https://github.com/getpelican/pelican-themes/blob/master/new-bootstrap2/templates/article.html#L30

What's the problem?

  1. Themes are fatter due to boilerplate code.
  2. Theme creators need to dive into the Disqus site and understand internal Jargon in order to find the code to embed (not a completely trivial task unless you know Disqus).
  3. Changes are hard: The Disqus code block seem to have changed recently (e.g. instead of dsq.src= they use s.src=). Updating nearly 40 themes whenever Disqus improves their code is tedious.

My solution

  • Pelican should declare a new variable - {{ DISQUS }}. It will be set to the entire code block if {{ DISQUS_SITENAME }} is defined, or an empty string otherwise. It can also be minified for efficiency.
  • Pelican themes will just add {{ DISQUS }}, saving all the work and code duplication. Themes will be shorter and more elegant this way.

I will be happy to implement - looking forward for your thoughts on the design.

adamatan avatar Feb 19 '16 22:02 adamatan

Why not simply {% include 'disqus.html' %} here the include path is relative to the theme directory and the included file lives in the parent directory ?

atodorov avatar Apr 28 '16 11:04 atodorov

@atodorov I don't understand where do you suggest to place disqus.html so it can be shared among all themes.

adamatan avatar Apr 28 '16 11:04 adamatan

@adamatan if you place it in the main directory of the repository it can be used by all themes which are subdirectories. Of course this will not work for all the rest which are sub-modules.

atodorov avatar Apr 28 '16 12:04 atodorov