]> git.draconx.ca Git - homepage.git/blobdiff - Rules
Improve script hack compatibility.
[homepage.git] / Rules
diff --git a/Rules b/Rules
index 477d46dabc28c5c605813a4d700b70b14399a1a7..2460c14ba315339e7e4ca55479480101b97a6de1 100644 (file)
--- a/Rules
+++ b/Rules
@@ -1,6 +1,6 @@
 #!/usr/bin/env ruby
 #
-# Copyright © 2018 Nick Bowler
+# Copyright © 2018-2020 Nick Bowler
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
 
 preprocess do
+    system("git", "submodule", "update", "--init")
+
+    # Remove dead annex keys from processing
+    @items.delete_if do |item|
+        l = File.readlink(item.raw_filename)
+        true if !File.exists?(item.raw_filename) and l =~ %r{/annex/objects/}
+    rescue Errno::EINVAL
+    end
+
     commit = nil
     Open3.popen2("git", "rev-parse", "HEAD") do |stdin, stdout, result|
         stdin.close
         val = stdout.gets
         stdout.close
-        if result.value.success? then commit = val.chomp end
+        commit = val.chomp if result.value.success?
     end
 
     if commit then
         @items.each do |item|
-            if item_source(item) then
-                item[:gitrev] = commit
+            item[:gitrev] = commit if item_source(item)
+        end
+    end
+
+    @items.find_all('/weblog/*.md').each do |item|
+        item[:kind] ||= 'article'
+    end
+
+    @items.each do |item|
+        item[:created_at] ||=
+            item[:published] || File.stat(item.raw_filename).mtime
+        item[:updated_at] ||=
+            item[:updated] || File.stat(item.raw_filename).mtime
+    end
+
+    archivedirs = {}
+    @items.find_all('/archive/**/*').each do |item|
+        dir = File.dirname(item.identifier)
+        while dir != "/"
+            archivedirs[dir] = true
+            dir = File.dirname(dir)
+        end
+    end
+
+    archivedirs.keys.sort_by{ |s| -s.length }.each do |dir|
+        attrs = {
+            pattern: "#{dir}/*{,/index.lst}",
+            title: "Index of #{dir}",
+            gitrev: commit,
+        }
+
+        @items.create("", attrs, "#{dir}/index.lst")
+    end
+end
+
+postprocess do
+    # Gzip all text items for nginx http_static
+    if !ENV['GZIP_SITE'].to_s.empty?
+        reps = @items.flat_map(&:reps).each do |rep|
+            file = rep.raw_path
+            next if !file or rep.binary?
+
+            if system("gzip", "-ck9", file, [:out]=>[file + ".gz.tmp", "wb"])
+                system("touch", "-r", file, file + ".gz.tmp")
+                File.rename(file + ".gz.tmp", file + ".gz")
+            else
+                File.unlink(file + ".gz.tmp")
+            end
+        end
+    end
+
+    # Register URLs for git-annex keys
+    unless (uribase = ENV['ANNEX_URI_BASE'].to_s.chomp("/")).empty?
+        Open3.popen2("git", "-c", "core.bare=false", "annex", "registerurl") do
+        |stdin, stdout, result|
+            @items.each do |item|
+                next if item.raw_filename.nil?
+
+                l = File.readlink(item.raw_filename)
+                next unless l =~ %r{/annex/objects/}
+
+                key = File.basename(l)
+
+                # Find output reps corresponding to this key, if any
+                item.reps.each do |rep|
+                    next unless
+                        FileUtils.identical?(item.raw_filename, rep.raw_path)
+
+                    loop do
+                        STDOUT.write(stdout.read_nonblock(100))
+                    rescue EOFError, IO::WaitReadable
+                        break
+                    end
+
+                    stdin.printf("%s %s%s\n", key, uribase, rep.path)
+                end
+            rescue Errno::EINVAL
+            end
+
+            stdin.close
+            loop do
+                STDOUT.write(stdout.readpartial(100))
+            rescue EOFError
+                break
+            end
+
+            unless (rc = result.value).success?
+                printf("git annex registerurl failed: %s\n", rc.to_s)
             end
         end
     end
 end
 
+compile '/**/index.lst' do
+    layout '/listing.xhtml'
+    filter :erb
+    layout '/default.xml'
+    layout '/default.xsl'
+    filter :relativize_paths, type: :xml
+    filter :xhtml_compat
+    filter :remove_wj
+    write item.identifier.without_ext + ".xhtml"
+end
+
+compile '/license/gpl*.md' do
+    filter :kramdown, auto_ids: false, header_offset: -1
+    layout '/default.xml'
+    layout '/gpl.xsl'
+    layout '/default.xsl'
+    filter :relativize_paths, type: :xml
+    filter :xhtml_compat
+    filter :remove_wj
+    write to_xhtml
+end
+
 compile '/**/*.md' do
-    filter :kramdown, header_offset: 1
+    filter :erb
+    filter :kramdown, auto_ids: false, header_offset: 1
+    snapshot :rawbody
+    layout '/default.xml'
+    layout '/default.xsl'
+    filter :relativize_paths, type: :xml
+    filter :xhtml_compat
+    filter :remove_wj
+    write to_xhtml
+end
+
+compile '/license/cc*.sgml' do
+    filter :sgml2xml
+    layout '/creativecommons.xsl'
+    layout '/default.xml'
+    layout '/default.xsl', "section-links": "yes"
+    filter :relativize_paths, type: :xml
+    filter :xhtml_compat
+    filter :remove_wj
+    write to_xhtml
+end
+
+compile '/license/cc*.xhtml' do
+    layout '/creativecommons.xsl'
+    layout '/default.xml'
+    layout '/default.xsl', "section-links": "yes"
+    filter :relativize_paths, type: :xml
+    filter :xhtml_compat
+    filter :remove_wj
+    write to_xhtml
+end
+
+compile '/images/*.jpg', rep: :large do
+    filename = item.identifier.without_ext + '-t1200.' + item.identifier.ext
+    filter :imgresize, width: 1200, height: 1200, cache: filename
+    write filename
+end
+
+compile '/images/*.jpg', rep: :info do
+    filter :imginfo
+    layout '/imginfo.xsl'
     layout '/default.xml'
     layout '/default.xsl'
     filter :relativize_paths, type: :xml
+    filter :xhtml_compat
+    filter :remove_wj
     write to_xhtml
 end
 
-passthrough '/**/*'
+compile '/**/*.scss' do
+    filter :sass, syntax: :scss
+    filter :css_source, uribase: \
+        "https://git.draconx.ca/gitweb/homepage.git/blob/" +
+        @item[:gitrev] + ":"
+    filter :css_clean_selectors, \
+        preserve_comments: true, \
+        preserve_hacks: true
+    write @item.identifier.without_ext + '.css'
+end
+
+compile '/**/*' do
+    filter :copybin if @item.binary?
+    write @item.identifier.to_s
+end
+
 layout '/**/*.xsl', :xsl
 layout '/**/*', :erb