add v5.0.1
[qemu-web.git] / _plugins / monthly_archive_plugin.rb
1 # Jekyll Module to create monthly archive pages
2 #
3 # Shigeya Suzuki, November 2013
4 # Copyright notice (MIT License) attached at the end of this file
5 #
6
7 #
8 # This code is based on the following works:
9 # https://gist.github.com/ilkka/707909
10 # https://gist.github.com/ilkka/707020
11 # https://gist.github.com/nlindley/6409459
12 #
13
14 #
15 # Archive will be written as #{archive_path}/#{year}/#{month}/index.html
16 # archive_path can be configured in 'path' key in 'monthly_archive' of
17 # site configuration file. 'path' is default null.
18 #
19
20 module Jekyll
21
22 module MonthlyArchiveUtil
23 def self.archive_base(site)
24 site.config['monthly_archive'] && site.config['monthly_archive']['path'] || ''
25 end
26 end
27
28 # Generator class invoked from Jekyll
29 class MonthlyArchiveGenerator < Generator
30 def generate(site)
31 posts_group_by_year_and_month(site).each do |ym, list|
32 site.pages << MonthlyArchivePage.new(site, MonthlyArchiveUtil.archive_base(site),
33 ym[0], ym[1], list)
34 end
35 end
36
37 def posts_group_by_year_and_month(site)
38 site.posts.docs.each.group_by { |post| [post.date.year, post.date.month] }
39 end
40
41 end
42
43 # Actual page instances
44 class MonthlyArchivePage < Page
45
46 ATTRIBUTES_FOR_LIQUID = %w[
47 year,
48 month,
49 date,
50 content
51 ]
52
53 def initialize(site, dir, year, month, posts)
54 @site = site
55 @dir = dir
56 @year = year
57 @month = month
58 @archive_dir_name = '%04d/%02d' % [year, month]
59 @date = Date.new(@year, @month)
60 @layout = site.config['monthly_archive'] && site.config['monthly_archive']['layout'] || 'monthly_archive'
61 self.ext = '.html'
62 self.basename = 'index'
63 self.content = <<-EOS
64 {% for post in page.posts %}<li><a href="{{ post.url }}"><span>{{ post.title }}</span></a></li>
65 {% endfor %}
66 EOS
67 self.data = {
68 'layout' => @layout,
69 'type' => 'archive',
70 'title' => "Monthly archive for #{@year}/#{@month}",
71 'posts' => posts,
72 'url' => File.join('/',
73 MonthlyArchiveUtil.archive_base(site),
74 @archive_dir_name, 'index.html')
75 }
76 end
77
78 def render(layouts, site_payload)
79 payload = {
80 'page' => self.to_liquid,
81 'paginator' => pager.to_liquid
82 }.merge(site_payload)
83 do_layout(payload, layouts)
84 end
85
86 def to_liquid(attr = nil)
87 self.data.merge({
88 'content' => self.content,
89 'date' => @date,
90 'month' => @month,
91 'year' => @year
92 })
93 end
94
95 def destination(dest)
96 File.join('/', dest, @dir, @archive_dir_name, 'index.html')
97 end
98
99 end
100 end
101
102 # The MIT License (MIT)
103 #
104 # Copyright (c) 2013 Shigeya Suzuki
105 #
106 # Permission is hereby granted, free of charge, to any person obtaining a copy
107 # of this software and associated documentation files (the "Software"), to deal
108 # in the Software without restriction, including without limitation the rights
109 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
110 # copies of the Software, and to permit persons to whom the Software is
111 # furnished to do so, subject to the following conditions:
112 #
113 # The above copyright notice and this permission notice shall be included in all
114 # copies or substantial portions of the Software.
115 #
116 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
117 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
118 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
119 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
120 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
121 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
122 # SOFTWARE.