]> git.draconx.ca Git - homepage.git/blob - Rules
Work around git-annex breakage.
[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 end
53
54 postprocess do
55     # Gzip all text items for nginx http_static
56     if !ENV['GZIP_SITE'].to_s.empty?
57         reps = @items.flat_map(&:reps).each do |rep|
58             file = rep.raw_path
59             next if !file or rep.binary?
60
61             if system("gzip", "-ck9", file, [:out]=>[file + ".gz.tmp", "wb"])
62                 system("touch", "-r", file, file + ".gz.tmp")
63                 File.rename(file + ".gz.tmp", file + ".gz")
64             else
65                 File.unlink(file + ".gz.tmp")
66             end
67         end
68     end
69
70     # Register URLs for git-annex keys
71     unless (uribase = ENV['ANNEX_URI_BASE'].to_s.chomp("/")).empty?
72         Open3.popen2("git", "-c", "core.bare=false", "annex", "registerurl") do
73         |stdin, stdout, result|
74             @items.each do |item|
75                 l = File.readlink(item.raw_filename)
76                 next unless l =~ %r{/annex/objects/}
77
78                 key = File.basename(l)
79
80                 # Find output reps corresponding to this key, if any
81                 item.reps.each do |rep|
82                     next unless
83                         FileUtils.identical?(item.raw_filename, rep.raw_path)
84
85                     loop do
86                         STDOUT.write(stdout.read_nonblock(100))
87                     rescue EOFError, IO::WaitReadable
88                         break
89                     end
90
91                     stdin.printf("%s %s%s\n", key, uribase, rep.path)
92                 end
93             rescue Errno::EINVAL
94             end
95
96             stdin.close
97             loop do
98                 STDOUT.write(stdout.readpartial(100))
99             rescue EOFError
100                 break
101             end
102
103             unless (rc = result.value).success?
104                 printf("git annex registerurl failed: %s\n", rc.to_s)
105             end
106         end
107     end
108 end
109
110 compile '/license/gpl*.md' do
111     filter :kramdown, auto_ids: false, header_offset: -1
112     layout '/default.xml'
113     layout '/gpl.xsl'
114     layout '/default.xsl'
115     filter :relativize_paths, type: :xml
116     filter :xhtml_compat
117     filter :remove_wj
118     write to_xhtml
119 end
120
121 compile '/**/*.md' do
122     filter :erb
123     filter :kramdown, auto_ids: false, header_offset: 1
124     snapshot :rawbody
125     layout '/default.xml'
126     layout '/default.xsl'
127     filter :relativize_paths, type: :xml
128     filter :xhtml_compat
129     filter :remove_wj
130     write to_xhtml
131 end
132
133 compile '/license/cc*.sgml' do
134     filter :sgml2xml
135     layout '/creativecommons.xsl'
136     layout '/default.xml'
137     layout '/default.xsl', "section-links": "yes"
138     filter :relativize_paths, type: :xml
139     filter :xhtml_compat
140     filter :remove_wj
141     write to_xhtml
142 end
143
144 compile '/license/cc*.xhtml' do
145     layout '/creativecommons.xsl'
146     layout '/default.xml'
147     layout '/default.xsl', "section-links": "yes"
148     filter :relativize_paths, type: :xml
149     filter :xhtml_compat
150     filter :remove_wj
151     write to_xhtml
152 end
153
154 compile '/images/*.jpg', rep: :large do
155     filename = item.identifier.without_ext + '-t1200.' + item.identifier.ext
156     filter :imgresize, width: 1200, height: 1200, cache: filename
157     write filename
158 end
159
160 compile '/images/*.jpg', rep: :info do
161     filter :imginfo
162     layout '/imginfo.xsl'
163     layout '/default.xml'
164     layout '/default.xsl'
165     filter :relativize_paths, type: :xml
166     filter :xhtml_compat
167     filter :remove_wj
168     write to_xhtml
169 end
170
171 compile '/**/*.scss' do
172     filter :sass, syntax: :scss
173     filter :css_source, uribase: \
174         "https://git.draconx.ca/gitweb/homepage.git/blob/" +
175         @item[:gitrev] + ":"
176     filter :css_clean_selectors, \
177         preserve_comments: true, \
178         preserve_hacks: true
179     write @item.identifier.without_ext + '.css'
180 end
181
182 compile '/**/*' do
183     filter :copybin if @item.binary?
184     write @item.identifier.to_s
185 end
186
187 layout '/**/*.xsl', :xsl
188 layout '/**/*', :erb