]> git.draconx.ca Git - homepage.git/blob - Rules
Release cdecl99-1.
[homepage.git] / Rules
1 #!/usr/bin/env ruby
2 #
3 # Copyright © 2018-2020 Nick Bowler
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
17
18 preprocess do
19     system("git", "submodule", "update", "--init")
20
21     # Remove dead annex keys from processing
22     @items.delete_if do |item|
23         l = File.readlink(item.raw_filename)
24         true if !File.exists?(item.raw_filename) and l =~ %r{/annex/objects/}
25     rescue Errno::EINVAL
26     end
27
28     commit = nil
29     Open3.popen2("git", "rev-parse", "HEAD") do |stdin, stdout, result|
30         stdin.close
31         val = stdout.gets
32         stdout.close
33         commit = val.chomp if result.value.success?
34     end
35
36     if commit then
37         @items.each do |item|
38             item[:gitrev] = commit if item_source(item)
39         end
40     end
41
42     @items.find_all('/weblog/*.md').each do |item|
43         item[:kind] ||= 'article'
44     end
45
46     @items.each do |item|
47         item[:created_at] ||=
48             item[:published] || File.stat(item.raw_filename).mtime
49         item[:updated_at] ||=
50             item[:updated] || File.stat(item.raw_filename).mtime
51     end
52
53     archivedirs = {}
54     @items.find_all('/archive/**/*').each do |item|
55         dir = File.dirname(item.identifier)
56         while dir != "/"
57             archivedirs[dir] = true
58             dir = File.dirname(dir)
59         end
60     end
61
62     archivedirs.keys.sort_by{ |s| -s.length }.each do |dir|
63         attrs = {
64             pattern: "#{dir}/*{,/index.lst}",
65             title: "Index of #{dir}",
66             gitrev: commit,
67         }
68
69         @items.create("", attrs, "#{dir}/index.lst")
70     end
71 end
72
73 postprocess do
74     # Gzip all text items for nginx http_static
75     if !ENV['GZIP_SITE'].to_s.empty?
76         reps = @items.flat_map(&:reps).each do |rep|
77             file = rep.raw_path
78             next if !file or rep.binary?
79
80             if system("gzip", "-ck9", file, [:out]=>[file + ".gz.tmp", "wb"])
81                 system("touch", "-r", file, file + ".gz.tmp")
82                 File.rename(file + ".gz.tmp", file + ".gz")
83             else
84                 File.unlink(file + ".gz.tmp")
85             end
86         end
87     end
88
89     # Register URLs for git-annex keys
90     unless (uribase = ENV['ANNEX_URI_BASE'].to_s.chomp("/")).empty?
91         Open3.popen2("git", "-c", "core.bare=false", "annex", "registerurl") do
92         |stdin, stdout, result|
93             @items.each do |item|
94                 next if item.raw_filename.nil?
95
96                 l = File.readlink(item.raw_filename)
97                 next unless l =~ %r{/annex/objects/}
98
99                 key = File.basename(l)
100
101                 # Find output reps corresponding to this key, if any
102                 item.reps.each do |rep|
103                     next unless
104                         FileUtils.identical?(item.raw_filename, rep.raw_path)
105
106                     loop do
107                         STDOUT.write(stdout.read_nonblock(100))
108                     rescue EOFError, IO::WaitReadable
109                         break
110                     end
111
112                     stdin.printf("%s %s%s\n", key, uribase, rep.path)
113                 end
114             rescue Errno::EINVAL
115             end
116
117             stdin.close
118             loop do
119                 STDOUT.write(stdout.readpartial(100))
120             rescue EOFError
121                 break
122             end
123
124             unless (rc = result.value).success?
125                 printf("git annex registerurl failed: %s\n", rc.to_s)
126             end
127         end
128     end
129 end
130
131 compile '/**/index.lst' do
132     layout '/listing.erb'
133     layout '/default.xml'
134     layout '/default.xsl'
135     layout '/embed-svg.xsl'
136     filter :relativize_paths, type: :xml
137     filter :xhtml_compat, fix_doctype: true
138     write item.identifier.without_ext + ".xhtml"
139 end
140
141 compile '/license/gpl*.md' do
142     filter :kramdown, auto_ids: false, header_offset: -1
143     layout '/default.xml'
144     layout '/gpl.xsl'
145     layout '/default.xsl'
146     filter :relativize_paths, type: :xml
147     filter :xhtml_compat
148     write to_xhtml
149 end
150
151 compile '/**/*.md' do
152     filter :erb
153     filter :kramdown, auto_ids: false, header_offset: 1
154     snapshot :rawbody
155     layout '/default.xml'
156     layout '/default.xsl'
157     filter :relativize_paths, type: :xml
158     filter :xhtml_compat
159     write to_xhtml
160 end
161
162 compile '/license/cc*.sgml' do
163     filter :sgml2xml
164     layout '/creativecommons.xsl'
165     layout '/default.xml'
166     layout '/default.xsl', "section-links": "yes"
167     filter :relativize_paths, type: :xml
168     filter :xhtml_compat
169     write to_xhtml
170 end
171
172 compile '/license/cc*.xhtml' do
173     layout '/creativecommons.xsl'
174     layout '/default.xml'
175     layout '/default.xsl', "section-links": "yes"
176     filter :relativize_paths, type: :xml
177     filter :xhtml_compat
178     write to_xhtml
179 end
180
181 compile '/images/*.jpg', rep: :large do
182     filename = item.identifier.without_ext + '-t1200.' + item.identifier.ext
183     filter :imgresize, width: 1200, height: 1200, cache: filename
184     write filename
185 end
186
187 compile '/images/*.jpg', rep: :info do
188     filter :imginfo
189     layout '/imginfo.xsl'
190     layout '/default.xml'
191     layout '/default.xsl'
192     filter :relativize_paths, type: :xml
193     filter :xhtml_compat
194     write to_xhtml
195 end
196
197 compile '/**/*.scss' do
198     filter :sass, syntax: :scss
199     filter :css_source, uribase: \
200         "https://git.draconx.ca/gitweb/homepage.git/blob/" +
201         @item[:gitrev] + ":"
202     filter :css_clean_selectors, \
203         preserve_comments: true, \
204         preserve_hacks: true
205     write @item.identifier.without_ext + '.css'
206 end
207
208 compile '/**/*.svg' do
209     filter :scour, comment_stripping: true
210     write @item.identifier.to_s
211 end
212
213 compile '/icons/**/*.svg', rep: :icon32 do
214     filter :svg2png, width: 32, height: 32
215     write @item.identifier.without_ext + "-32.png"
216 end
217
218 compile '/**/*' do
219     filter :copybin if @item.binary?
220     write @item.identifier.to_s
221 end
222
223 layout '/**/*.xsl', :xsl
224 layout '/**/*', :erb