Browse Source

Merge pull request #824 from wamonite/photo_add_inline_lightbox

Photos plugin: Add lightbox option to inline photo replacement
Justin Mayer 7 years ago
parent
commit
6cd5a608b4
2 changed files with 104 additions and 20 deletions
  1. 3 1
      photos/README.md
  2. 101 19
      photos/photos.py

+ 3 - 1
photos/README.md

@@ -66,6 +66,7 @@ Maintain an organized library of high resolution photos somewhere on disk, using
 * To create a gallery of photos, add the metadata field `gallery: {photo}folder` to an article. To simplify the transition from the plug-in Gallery, the syntax `gallery: {filename}folder` is also accepted.
 * You can now have multiple galleries. The galleries need to be seperated by a comma in the metadata field. The syntax is gallery: `{photo}folder, {photo}folder2`. You can also add titles to your galleries. The syntax is: `{photo}folder, {photo}folder2{This is a title}`. Using the following example the first gallery would have the title of the folder location and the second would have the title `This is a tile.`
 * To use an image in the body of the text, just use the syntax `{photo}folder/image.jpg` instead of the usual `{filename}/images/image.jpg`.
+* To use an image in the body of the text, which can be used with [Lightbox](http://lokeshdhakar.com/projects/lightbox2/) just use the syntax `{lightbox}folder/image.jpg`. For use with other implementations, the gallery and caption attribute names can be set with `PHOTO_LIGHTBOX_GALLERY_ATTR` and `PHOTO_LIGHTBOX_CAPTION_ATTR`.
 * To associate an image with an article, add the metadata field `image: {photo}folder/image.jpg` to an article. Use associated images to improve navigation. For compatibility, the syntax `image: {filename}/images/image.jpg` is also accepted.
 
 ### Exif, Captions, and Blacklists
@@ -94,7 +95,7 @@ Folders of photos may optionally have three text files, where each line describe
 	this-one-will-be-also-skipped.jpg
 
 
-Here is an example Markdown article that shows the three use cases:
+Here is an example Markdown article that shows the four use cases:
 
 	title: My Article
 	gallery: {photo}favorite
@@ -102,6 +103,7 @@ Here is an example Markdown article that shows the three use cases:
 
 	Here are my best photos, taken with my favorite camera:
 	![]({photo}mybag/camera.jpg).
+	![]({lightbox}mybag/flash.jpg).
 
 The default behavior of the Photos plugin removes the exif information from the file. If you would like to keep the exif information, you can install the `piexif` library for python and add the following settings to keep some or all of the exif information. This feature is not a replacement for the `exif.txt` feature but in addition to that feature. This feature currently only works with jpeg input files.
 

+ 101 - 19
photos/photos.py

@@ -59,6 +59,8 @@ def initialized(pelican):
     DEFAULT_CONFIG.setdefault('PHOTO_EXIF_AUTOROTATE', True)
     DEFAULT_CONFIG.setdefault('PHOTO_EXIF_COPYRIGHT', False)
     DEFAULT_CONFIG.setdefault('PHOTO_EXIF_COPYRIGHT_AUTHOR', DEFAULT_CONFIG['SITENAME'])
+    DEFAULT_CONFIG.setdefault('PHOTO_LIGHTBOX_GALLERY_ATTR', 'data-lightbox')
+    DEFAULT_CONFIG.setdefault('PHOTO_LIGHTBOX_CAPTION_ATTR', 'data-title')
 
     DEFAULT_CONFIG['queue_resize'] = {}
     DEFAULT_CONFIG['created_galleries'] = {}
@@ -83,6 +85,8 @@ def initialized(pelican):
         pelican.settings.setdefault('PHOTO_EXIF_AUTOROTATE', True)
         pelican.settings.setdefault('PHOTO_EXIF_COPYRIGHT', False)
         pelican.settings.setdefault('PHOTO_EXIF_COPYRIGHT_AUTHOR', pelican.settings['AUTHOR'])
+        pelican.settings.setdefault('PHOTO_LIGHTBOX_GALLERY_ATTR', 'data-lightbox')
+        pelican.settings.setdefault('PHOTO_LIGHTBOX_CAPTION_ATTR', 'data-title')
 
 
 def read_notes(filename, msg=None):
@@ -311,37 +315,115 @@ def detect_content(content):
     def replacer(m):
         what = m.group('what')
         value = m.group('value')
-        origin = m.group('path')
+        tag = m.group('tag')
+        output = m.group(0)
 
-        if what == 'photo':
+        if what in ('photo', 'lightbox'):
             if value.startswith('/'):
                 value = value[1:]
+
             path = os.path.join(
                 os.path.expanduser(settings['PHOTO_LIBRARY']),
-                value)
-            if not os.path.isfile(path):
-                logger.error('photos: No photo %s', path)
+                value
+            )
+
+            if os.path.isfile(path):
+                photo_prefix = os.path.splitext(value)[0].lower()
+
+                if what == 'photo':
+                    photo_article = photo_prefix + 'a.jpg'
+                    enqueue_resize(
+                        path,
+                        os.path.join('photos', photo_article),
+                        settings['PHOTO_ARTICLE']
+                    )
+
+                    output = ''.join((
+                        '<',
+                        m.group('tag'),
+                        m.group('attrs_before'),
+                        m.group('src'),
+                        '=',
+                        m.group('quote'),
+                        os.path.join(settings['SITEURL'], 'photos', photo_article),
+                        m.group('quote'),
+                        m.group('attrs_after'),
+                    ))
+
+                elif what == 'lightbox' and tag == 'img':
+                    photo_gallery = photo_prefix + '.jpg'
+                    enqueue_resize(
+                        path,
+                        os.path.join('photos', photo_gallery),
+                        settings['PHOTO_GALLERY']
+                    )
+
+                    photo_thumb = photo_prefix + 't.jpg'
+                    enqueue_resize(
+                        path,
+                        os.path.join('photos', photo_thumb),
+                        settings['PHOTO_THUMB']
+                    )
+
+                    lightbox_attr_list = ['']
+
+                    gallery_name = value.split('/')[0]
+                    lightbox_attr_list.append('{}="{}"'.format(
+                        settings['PHOTO_LIGHTBOX_GALLERY_ATTR'],
+                        gallery_name
+                    ))
+
+                    captions = read_notes(
+                        os.path.join(os.path.dirname(path), 'captions.txt'),
+                        msg = 'photos: No captions for gallery'
+                    )
+                    caption = captions.get(os.path.basename(path)) if captions else None
+                    if caption:
+                        lightbox_attr_list.append('{}="{}"'.format(
+                            settings['PHOTO_LIGHTBOX_CAPTION_ATTR'],
+                            caption
+                        ))
+
+                    lightbox_attrs = ' '.join(lightbox_attr_list)
+
+                    output = ''.join((
+                        '<a href=',
+                        m.group('quote'),
+                        os.path.join(settings['SITEURL'], 'photos', photo_gallery),
+                        m.group('quote'),
+                        lightbox_attrs,
+                        '><img',
+                        m.group('attrs_before'),
+                        'src=',
+                        m.group('quote'),
+                        os.path.join(settings['SITEURL'], 'photos', photo_thumb),
+                        m.group('quote'),
+                        m.group('attrs_after'),
+                        '</a>'
+                    ))
+
             else:
-                photo = os.path.splitext(value)[0].lower() + 'a.jpg'
-                origin = os.path.join(settings['SITEURL'], 'photos', photo)
-                enqueue_resize(
-                    path,
-                    os.path.join('photos', photo),
-                    settings['PHOTO_ARTICLE'])
-        return ''.join((m.group('markup'), m.group('quote'), origin,
-                        m.group('quote')))
+                logger.error('photos: No photo %s', path)
+
+        return output
 
     if hrefs is None:
         regex = r"""
-            (?P<markup><\s*[^\>]*  # match tag with src and href attr
-                (?:href|src)\s*=)
-
-            (?P<quote>["\'])      # require value to be quoted
+            <\s*
+            (?P<tag>[^\s\>]+)  # detect the tag
+            (?P<attrs_before>[^\>]*)
+            (?P<src>href|src)  # match tag with src and href attr
+            \s*=
+            (?P<quote>["\'])  # require value to be quoted
             (?P<path>{0}(?P<value>.*?))  # the url value
-            \2""".format(content.settings['INTRASITE_LINK_REGEX'])
+            (?P=quote)
+            (?P<attrs_after>[^\>]*>)
+        """.format(
+            content.settings['INTRASITE_LINK_REGEX']
+        )
         hrefs = re.compile(regex, re.X)
 
-    if content._content and '{photo}' in content._content:
+    if content._content and ('{photo}' in content._content or '{lightbox}' in content._content):
         settings = content.settings
         content._content = hrefs.sub(replacer, content._content)