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