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