|
| 1 | +# Title: Dynamic directories for Jekyll |
| 2 | +# Author: Tommy Sullivan http://superawesometommy.com, Robert Park http://exolucere.ca |
| 3 | +# Description: The directory tag lets you iterate over files at a particular path. If files conform to the standard Jekyll format, YYYY-MM-DD-file-title, then those attributes will be populated on the yielded file object. The `forloop` object maintains [its usual context](http://wiki.shopify.com/UsingLiquid#For_loops). |
| 4 | +# |
| 5 | +# Syntax: |
| 6 | +# |
| 7 | +# {% directory path: path/from/source [reverse] [exclude] %} |
| 8 | +# {{ file.url }} |
| 9 | +# {{ file.name }} |
| 10 | +# {{ file.date }} |
| 11 | +# {{ file.slug }} |
| 12 | +# {{ file.title }} |
| 13 | +# {% enddirectory %} |
| 14 | +# |
| 15 | +# Options: |
| 16 | +# |
| 17 | +# - `reverse` - Defaults to 'false', ordering files the same way `ls` does: 0-9A-Za-z. |
| 18 | +# - `exclude` - Defaults to '.html$', a Regexp of files to skip. |
| 19 | +# |
| 20 | +# File Attributes: |
| 21 | +# |
| 22 | +# - `url` - The absolute path to the published file |
| 23 | +# - `name` - The basename |
| 24 | +# - `date` - The date extracted from the filename, otherwise the file's creation time |
| 25 | +# - `slug` - The basename with date and extension removed |
| 26 | +# - `title` - The titlecase'd slug |
| 27 | +# |
| 28 | + |
| 29 | +module Jekyll |
| 30 | + |
| 31 | + class DirectoryTag < Liquid::Block |
| 32 | + include Convertible |
| 33 | + |
| 34 | + MATCHER = /^(.+\/)*(\d+-\d+-\d+)-(.*)(\.[^.]+)$/ |
| 35 | + |
| 36 | + attr_accessor :content, :data |
| 37 | + |
| 38 | + def initialize(tag_name, markup, tokens) |
| 39 | + attributes = {} |
| 40 | + |
| 41 | + # Parse parameters |
| 42 | + markup.scan(Liquid::TagAttributes) do |key, value| |
| 43 | + attributes[key] = value |
| 44 | + end |
| 45 | + |
| 46 | + @path = attributes['path'] || '.' |
| 47 | + @exclude = Regexp.new(attributes['exclude'] || '.html$', Regexp::EXTENDED | Regexp::IGNORECASE) |
| 48 | + @rev = attributes['reverse'].nil? |
| 49 | + |
| 50 | + super |
| 51 | + end |
| 52 | + |
| 53 | + def render(context) |
| 54 | + context.registers[:directory] ||= Hash.new(0) |
| 55 | + |
| 56 | + source_dir = context.registers[:site].source |
| 57 | + directory_files = File.join(source_dir, @path, "*") |
| 58 | + |
| 59 | + files = Dir.glob(directory_files).reject{|f| f =~ @exclude } |
| 60 | + files.sort! {|x,y| @rev ? x <=> y : y <=> x } |
| 61 | + |
| 62 | + length = files.length |
| 63 | + result = [] |
| 64 | + |
| 65 | + context.stack do |
| 66 | + files.each_with_index do |filename, index| |
| 67 | + basename = File.basename(filename) |
| 68 | + |
| 69 | + filepath = [@path, basename] - ['.'] |
| 70 | + path = filepath.join '/' |
| 71 | + url = '/' + filepath.join('/') |
| 72 | + |
| 73 | + m, cats, date, slug, ext = *basename.match(MATCHER) |
| 74 | + |
| 75 | + if m |
| 76 | + date = Time.parse(date) |
| 77 | + ext = ext |
| 78 | + slug = slug |
| 79 | + else |
| 80 | + date = File.ctime(filename) |
| 81 | + ext = basename[/\.[a-z]+$/, 0] |
| 82 | + slug = basename.sub(ext, '') |
| 83 | + end |
| 84 | + |
| 85 | + context['file'] = { |
| 86 | + 'date' => date, |
| 87 | + 'name' => basename, |
| 88 | + 'slug' => slug, |
| 89 | + 'url' => url |
| 90 | + } |
| 91 | + |
| 92 | + context['forloop'] = { |
| 93 | + 'name' => 'directory', |
| 94 | + 'length' => length, |
| 95 | + 'index' => index + 1, |
| 96 | + 'index0' => index, |
| 97 | + 'rindex' => length - index, |
| 98 | + 'rindex0' => length - index - 1, |
| 99 | + 'first' => (index == 0), |
| 100 | + 'last' => (index == length - 1) |
| 101 | + } |
| 102 | + |
| 103 | + result << render_all(@nodelist, context) |
| 104 | + end |
| 105 | + end |
| 106 | + result |
| 107 | + end |
| 108 | + |
| 109 | + end |
| 110 | + |
| 111 | +end |
| 112 | + |
| 113 | +Liquid::Template.register_tag('directory', Jekyll::DirectoryTag) |
| 114 | + |
0 commit comments