]> git.draconx.ca Git - homepage.git/blob - Rules
Suggest keyserver (again) for retrievng public keys.
[homepage.git] / Rules
1 #!/usr/bin/env ruby
2 #
3 # Copyright © 2018-2022 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
72     create_wkd_items(@items["/pubring.gpg"])
73     create_hkp_items(@items["/pubring.gpg"])
74 end
75
76 postprocess do
77     # Gzip all text items for nginx http_static
78     if !ENV['GZIP_SITE'].to_s.empty?
79         reps = @items.flat_map(&:reps).each do |rep|
80             file = rep.raw_path
81             next if !file or rep.binary?
82
83             if system("gzip", "-ck9", file, [:out]=>[file + ".gz.tmp", "wb"])
84                 system("touch", "-r", file, file + ".gz.tmp")
85                 File.rename(file + ".gz.tmp", file + ".gz")
86             else
87                 File.unlink(file + ".gz.tmp")
88             end
89         end
90     end
91
92     # Register URLs for git-annex keys
93     unless (uribase = ENV['ANNEX_URI_BASE'].to_s.chomp("/")).empty?
94         Open3.popen2("git", "-c", "core.bare=false", "annex", "registerurl") do
95         |stdin, stdout, result|
96             @items.each do |item|
97                 next if item.raw_filename.nil?
98
99                 l = File.readlink(item.raw_filename)
100                 next unless l =~ %r{/annex/objects/}
101
102                 key = File.basename(l)
103
104                 # Find output reps corresponding to this key, if any
105                 item.reps.each do |rep|
106                     next if rep.raw_path.nil?
107                     next unless
108                         FileUtils.identical?(item.raw_filename, rep.raw_path)
109
110                     loop do
111                         STDOUT.write(stdout.read_nonblock(100))
112                     rescue EOFError, IO::WaitReadable
113                         break
114                     end
115
116                     stdin.printf("%s %s%s\n", key, uribase, rep.path)
117                 end
118             rescue Errno::EINVAL
119             end
120
121             stdin.close
122             loop do
123                 STDOUT.write(stdout.readpartial(100))
124             rescue EOFError
125                 break
126             end
127
128             unless (rc = result.value).success?
129                 printf("git annex registerurl failed: %s\n", rc.to_s)
130             end
131         end
132     end
133 end
134
135 compile '/**/index.lst' do
136     layout '/listing.erb'
137     layout '/default.xml'
138     layout '/default.xsl'
139     layout '/embed-svg.xsl'
140     filter :relativize_paths, type: :xml
141     filter :xhtml_compat, fix_doctype: true
142     write item.identifier.without_ext + ".xhtml"
143 end
144
145 compile '/license/gpl*.md' do
146     filter :kramdown, auto_ids: false, header_offset: -1
147     layout '/default.xml'
148     layout '/gpl.xsl'
149     layout '/default.xsl'
150     filter :relativize_paths, type: :xml
151     filter :xhtml_compat
152     write to_xhtml
153 end
154
155 compile '/**/*.md' do
156     filter :erb
157     filter :kramdown, auto_ids: false, header_offset: 1
158     snapshot :rawbody
159     layout '/default.xml'
160     layout '/default.xsl'
161     filter :relativize_paths, type: :xml
162     filter :xhtml_compat
163     write to_xhtml
164 end
165
166 compile '/license/cc*.sgml' do
167     filter :sgml2xml
168     layout '/creativecommons.xsl'
169     layout '/default.xml'
170     layout '/default.xsl', "section-links": "yes"
171     filter :relativize_paths, type: :xml
172     filter :xhtml_compat
173     write to_xhtml
174 end
175
176 compile '/license/cc*.xhtml' do
177     layout '/creativecommons.xsl'
178     layout '/default.xml'
179     layout '/default.xsl', "section-links": "yes"
180     filter :relativize_paths, type: :xml
181     filter :xhtml_compat
182     write to_xhtml
183 end
184
185 compile '/images/*.jpg', rep: :large do
186     w, h = FastImage.size(item.raw_filename)
187     filename = item.identifier.without_ext + '-t1200.' + item.identifier.ext
188     filter :imgresize, width: [w, 1200].min, height: [h, 1200].min, cache: filename
189     write filename
190 end
191
192 compile '/images/*.jpg', rep: :medium do
193     w, h = FastImage.size(item.raw_filename)
194     filename = item.identifier.without_ext + '-t800.' + item.identifier.ext
195     if w > 900 or h > 900
196         filter :imgresize, width: 800, height: 800, cache: filename
197         write filename
198     end
199 end
200
201 compile '/images/*.jpg', rep: :info do
202     filter :imginfo
203     layout '/imginfo.xsl'
204     layout '/default.xml'
205     layout '/default.xsl'
206     filter :relativize_paths, type: :xml
207     filter :xhtml_compat
208     write to_xhtml
209 end
210
211 compile '/**/*.scss' do
212     filter :sass, syntax: :scss
213     filter :css_source, uribase: \
214         "https://git.draconx.ca/gitweb/homepage.git/blob/" +
215         @item[:gitrev] + ":"
216     filter :css_clean_selectors, \
217         preserve_comments: true, \
218         preserve_hacks: true
219     write @item.identifier.without_ext + '.css'
220 end
221
222 compile '/gpg/*' do
223     filter :wkd_export_armor
224     write "/pubring/#{@item.identifier.components.last}.asc"
225     write "/pubring/#{@item[:id64]}.asc" if item[:id64]
226     write "/pubring/#{@item[:id32]}.asc" if item[:id32]
227 end
228
229 compile '/gpg/*', rep: :hu do
230     if @item[:wkd_hash]
231         filter :wkd_export
232         write "/pubring/wkd/" + @item[:wkd_hash]
233     end
234 end
235
236 compile '/*.gpg' do
237 end
238
239 compile '/**/*.svg' do
240     filter :scour, comment_stripping: true
241     write @item.identifier.to_s
242 end
243
244 compile '/icons/**/*.svg', rep: :icon32 do
245     filter :svg2png, width: 32, height: 32
246     write @item.identifier.without_ext + "-32.png"
247 end
248
249 compile '/**/*' do
250     filter :copybin if @item.binary?
251     write @item.identifier.to_s
252 end
253
254 layout '/**/*.xsl', :xsl
255 layout '/**/*', :erb