New Liquid tag for doing simple image galleries. #295

Closed
wants to merge 56 commits into from

6 participants

@ghost

Here's a basic usage:

 {% gallery name:your_gallery_name %}
 <img src="{{ file.path }}" />
 {% endgallery %}

If you want to arrange the images into a grid, you can do this:

 <table><tr>
 {% gallery name:your_gallery_name %}
 <td><img src="{{ file.path }}" alt="{{ file.title }}"/>
 <p>Taken on {{ file.date | date: "%F" }}</p></td>
 {% cycle '', '', '', '</tr><tr>' %}
 {% endgallery %}
 </tr></table>

It provides this data to the templates:

file.url        # absolute path to file
file.path       # relative path to file
file.htmlurl    # absolute path to html file
file.htmlpath   # relative path to html file
file.name       # File.basename(filename)
file.date       # date extracted from beginning of filename, if present (optional)
file.slug       # basename with the date and file extension stripped off
file.title      # file.slug with hyphens converted to spaces, and put into Title Case

As well as the familiar forloop variable.

Also, none of my code is actually specific to image files. This plugin could easily be used to glob anything out of the filesystem, including a list of available downloads:

 <ul>
 {% gallery name:downloads_page dir:downloads format:tar.gz reverse:no %}
 <li><a href="{{ file.path }}">{{ file.title }}</a></li>
 {% endgallery %}
 </ul>

The above example assumes that it's located inside /downloads_page/index.html and that your .tar.gz files are in /downloads_page/downloads/*.tar.gz. I've added some input checking that prevents the user passing in any periods or slashes to prevent them being able to glob outside of the jekyll directory.

heuripedes and others added some commits Dec 15, 2010
@heuripedes heuripedes updated/fixed wordpress.com migration script 9e0eb75
@MattHall MattHall Adding Posterous Importer c1f0e07
@MattHall MattHall Updating CLI for importing 84c1a72
Aaron Beckerman fix typo in history: site.ports -> site.posts 033333f
Ben Chatelain Add Jekyll::WordPress.TABLE_PREFIX and inclusion in QUERY 42f63f9
Ben Chatelain Change TABLE_PREFIX back to default e902bb9
Ben Chatelain Change TABLE_PREFIX from class member to 4th parameter of process met…
…hod (now lowercase)

Move QUERY into process method
bc3771a
Ben Chatelain Fix compile error by making QUERY lowercase (local instead of const) d61c1e9
@elia elia The Wordpress.com migrator now works and gathers categories as tags. f68bbcb
@elia elia Merged wordpress.com migrator fix from 'heuripedes/jekyll' ca48ea9
@elia elia Take permalink name directly from worpress export file. c70dac3
@elia elia Remove double directory creation. 034b064
@zenspider zenspider Cleaned up unnecessary string munging bd01e64
Robert Bruce Park Added liquid tag for doing simple image galleries. 787bb67
Robert Bruce Park Allow formats other than just ".jpg", and provide 'title' to the temp…
…lates.

'title' is just the image filename with the extension removed, hyphens and underscores converted to spaces, and the remaining words converted to Title Case.
fc8940d
Robert Bruce Park Less regex for better readability. 3cab419
Robert Bruce Park Add option to let the user reverse the sort. b18ccdd
Robert Bruce Park Simplify Title Casing by adding a method to String class. 69597bf
Robert Bruce Park Provide more data (including dates) to the templates. 8e8497f
Robert Bruce Park Do some input sanity checking. f996dbe
Robert Bruce Park Better input sanitation.
Should be pretty solid now. Nothing nasty is going to make it from user-supplied template data into the glob method now.
e8d3102
Robert Bruce Park Simplify titleize. eeb18e4
Robert Bruce Park Underscores too! D'oh! d07a9df
@tmm1 tmm1 Gemfile to help install the dependencies a04c270
@tmm1 tmm1 fix "4.2.1" versioned dev dependencies, and cleanup syntax 16ea326
@tmm1 tmm1 use the new albino gem 08725eb
@tmm1 tmm1 open4 is not required 4b5a4e8
@tmm1 tmm1 work around cucumber issue (closes #296) 8cc7f06
@tmm1 tmm1 rdiscount is a dev dependency 9da714d
@tmm1 tmm1 make kramdown a runtime dep so people can use it instead of maruku edef025
@tmm1 tmm1 speed up cleanup be8b771
@tmm1 tmm1 sanitize urls and ignore symlinks 13cc44f
@mojombo mojombo Update history. f82c51d
@mojombo mojombo Merge remote-tracking branch 'zenspider/master' into devel 5f4dfe3
@mojombo mojombo Move require to jekyll.rb and update history. a31780a
@mojombo mojombo Merge remote-tracking branch 'ab9/master' into devel 38844cd
@mojombo mojombo Merge remote-tracking branch 'MattHall/posterous' into devel f58a821
@mojombo mojombo Merge remote-tracking branch 'elia/master' into devel d2814cf
@mojombo mojombo Merge remote-tracking branch 'phatblat/master' into test 01a9090
@mojombo mojombo Merge remote-tracking branch 'MattHall/cli' into test 68eaadd
@mojombo mojombo TomDoc convertible.rb. 6c94db1
@mojombo

This looks really nice. Could you hook up a few test cases and cucumber features for it?

@ghost

This isn't the first time I've ever written a test case, but it's the first time I've written one in Ruby (all my previous experience is in python), and i've never used Cucumber before. Please let me know if these are satisfactory.

Thanks!

Robert Bruce... added some commits Mar 11, 2011
Robert Bruce Park More consistent absolute/relative path generation, and better cucumbe…
…r test of this.
cad5dde
Robert Bruce Park Provide contents of YAML front matter of iterated files to parent tem…
…plate.

As per discussion at jekyll/jekyll#299

Since images contain no YAML, this is harmlessly ignored by image files, but does make the plugin more useful to people using gallery for lists of files other than images.
11c86f4
Robert Bruce Park Add Cucumber Scenario for new ability to read YAML from globbed files. d96f226
Robert Bruce Park Make name attribute default to '.' if left unspecified.
This allows /index.html to contain a gallery of files at the top level instead of forcing everything to be one subdirectory below the sourcedir.
80aa862
@ghost Unknown commented on the diff Mar 13, 2011
lib/jekyll/tags/gallery.rb
+
+
+module Jekyll
+ class GalleryTag < Liquid::Block
+ include Convertible
+ attr_accessor :content, :data
+
+ def initialize(tag_name, markup, tokens)
+ attributes = {}
+
+ # Parse parameters
+ markup.scan(Liquid::TagAttributes) do |key, value|
+ attributes[key] = value
+ end
+
+ @name = attributes['name'] || '.'
@ghost
ghost added a note Mar 13, 2011

i've changed this so that if name is not specified, it defaults to a period, which is equivalent to saying "look in the root of the sourcedir for the gallery files". This is slightly nicer than it was before, but I'm still looking for a way to say "the gallery should be located in the same directory as the file that contained {% gallery %}."

Any thoughts on this, Tom?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Robert Bruce... added some commits Mar 12, 2011
Robert Bruce Park Change cucumber feature to show the simplest possible use case of gal…
…lery tag.
3ebcc45
Robert Bruce Park Code style cleanup.
Moved the String.titleize method definition into core_ext.rb. Also deleted the copious usage documentation comments because a) it was a bit out of date and b) that belongs on the wiki anyway.
5e87d9c
Robert Bruce Park Add file.htmlpath to make it easier to link to htmlized files. 16d8590
Robert Bruce Park Add file.htmlurl to compliment file.htmlpath 2b03d21
Robert Bruce Park Create gallery.feature, moving the three existing Cucumber Scenarios …
…there, and add two more.
ae5ba76
Robert Bruce Park Minor expansion of YAML use in Cucumber test. 592e4f6
Robert Bruce Park Merge remote-tracking branch 'mojombo/devel' 227fc4f
Robert Bruce Park Merge branch 'master' of github.com:robru/jekyll 967106a
@ghost

Hmmm, I seem to have made a mess of things by merging. Should I start over with a new fork and a new pull request, or is this ok as is? Sorry, github newbie ;-)

@ghost

Yeah, starting this one over. Derp de derp derp...

@ghost ghost closed this Mar 19, 2011
@parkr parkr referenced this pull request Sep 15, 2014
Closed

List files in directory #2927

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment