Skip to content

Commit

Permalink
Support imgclass, figclass, title parameters in thumbnail shortcode
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Warrick <kwpolska@gmail.com>
  • Loading branch information
Kwpolska committed Sep 20, 2017
1 parent e72038f commit f50459b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
13 changes: 10 additions & 3 deletions docs/manual.txt
Expand Up @@ -1231,15 +1231,22 @@ raw
of them being munged into shortcode **output**. I can't show an example because Inception.

thumbnail
Display image thumbnails, optionally with captions, alignment, and alt text. Examples:
Display image thumbnails, with optional captions. Examples:

.. code:: text

{{% raw %}}{{% thumbnail "/images/foo.png" %}}{{% /thumbnail %}}{{% /raw %}}
{{% raw %}}{{% thumbnail "/images/foo.png" alt="Foo Image" align="center" %}}{{% /thumbnail %}}{{% /raw %}}
{{% raw %}}{{% thumbnail "/images/foo.png" %}}<p>Image caption</p>{{% /thumbnail %}}{{% /raw %}}
{{% raw %}}{{% thumbnail "/images/foo.png" alt="Foo Image" align="right" %}}<p class="caption">Foo Image (right-aligned) caption</p>{{% /thumbnail %}}{{% /raw %}}
{{% raw %}}{{% thumbnail "/images/foo.png" imgclass="image-grayscale" figclass="figure-shadow" %}}<p>Image caption</p>{{% /thumbnail %}}{{% /raw %}}
{{% raw %}}{{% thumbnail "/images/foo.png" alt="Foo Image" title="Insert title-text joke here" align="right" %}}<p class="caption">Foo Image (right-aligned) caption</p>{{% /thumbnail %}}{{% /raw %}}

The following keyword arguments are supported:

* alt (alt text for image)
* align (image alignment, left/center/right)
* title (title text for image)
* imgclass (class for image)
* figclass (class for figure, used only if you provide a caption)

Looks similar to the reST thumbnail directive. Caption should be a HTML fragment.

Expand Down
26 changes: 17 additions & 9 deletions nikola/plugins/shortcode/thumbnail.py
Expand Up @@ -36,24 +36,32 @@ class ThumbnailShortcode(ShortcodePlugin):

name = "thumbnail"

def handler(self, uri, alt=None, align=None, target=None, site=None, data=None, lang=None, post=None):
def handler(self, uri, alt=None, align=None, title=None, imgclass=None, figclass=None, site=None, data=None, lang=None, post=None):
"""Create HTML for thumbnail."""
if uri.endswith('.svg'):
# the ? at the end makes docutil output an <img> instead of an object for the svg, which colorbox requires
src = '.thumbnail'.join(os.path.splitext(uri)) + '?'
else:
src = '.thumbnail'.join(os.path.splitext(uri))


if imgclass is None:
imgclass = ''
if figclass is None:
figclass = ''

if align and data:
figclass += ' align-{0}'.format(align)
elif align:
imgclass += ' align-{0}'.format(align)

output = '<a href="{0}" class="image-reference"><img src="{1}"'.format(uri, src)
if alt:
output += ' alt="{}"'.format(alt)
if align and not data:
output += ' class="align-{}"'.format(align)
for item, name in ((alt, 'alt'), (title, 'title'), (imgclass, 'class')):
if item:
output += ' {0}="{1}"'.format(name, item)
output += '></a>'

if data and align:
output = '<div class="figure align-{2}">{0}{1}</div>'.format(output, data, align)
elif data:
output = '<div class="figure">{0}{1}</div>'.format(output, data)
if data:
output = '<div class="figure {0}">{1}{2}</div>'.format(figclass, output, data)

return output, []

0 comments on commit f50459b

Please sign in to comment.