aboutsummaryrefslogtreecommitdiff
path: root/_plugins/iterator.rb
blob: da0b5f0a4d667f7718aa8d48f8ac728296a69fd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
##
## Author: Jose Gonzalez - https://github.com/josegonzalez
## Source URL: https://github.com/josegonzalez/josediazgonzalez.com/blob/master/_plugins/iterator.rb
##

#module Jekyll
  #class Site
    #alias_method :orig_site_payload, :site_payload

    ## Constuct an array of hashes that will allow the user, using Liquid, to
    ## iterate through the keys of _kv_hash_ and be able to iterate through the
    ## elements under each key.
    ##
    ## Example:
    ##   categories = { 'Ruby' => [<Post>, <Post>] }
    ##   make_iterable(categories, :index => 'name', :items => 'posts')
    ## Will allow the user to iterate through all categories and then iterate
    ## though each post in the current category like so:
    ##   {% for category in site.categories %}
    ##     h1. {{ category.name }}
    ##     <ul>
    ##       {% for post in category.posts %}
    ##         <li>{{ post.title }}</li>
    ##       {% endfor %}
    ##       </ul>
    ##   {% endfor %}
    ##
    ## Returns [ {<index> => <kv_hash_key>, <items> => kv_hash[<kv_hash_key>]}, ... ]

    #def make_iterable(kv_hash, options)
      #options = {:index => 'name', :items => 'items'}.merge(options)
      #result = []
      #kv_hash.sort.each do |key, value|
        #result << { options[:index] => key, options[:items] => value }
      #end
      #result
    #end

    #def site_payload
      #payload = orig_site_payload
      #payload['site']['iterable'].merge!({
        #'categories'  => make_iterable(self.categories, :index => 'name', :items => 'posts'),
        #'tags'        => make_iterable(self.tags, :index => 'name', :items => 'posts')
      #})
      #payload
    #end

  #end
#end