]> git.draconx.ca Git - homepage.git/blob - lib/helpers.rb
Solar Eclipse in Ottawa on 2021-06-10.
[homepage.git] / lib / helpers.rb
1 # Nick's web site: Ruby helpers for processing
2 #
3 # Copyright © 2018-2021 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 require 'nokogiri'
19 require 'fastimage'
20
21 use_helper Nanoc::Helpers::Blogging
22 use_helper Nanoc::Helpers::Breadcrumbs
23 use_helper Nanoc::Helpers::Rendering
24
25 Xmlns = {
26     'xhtml' => 'http://www.w3.org/1999/xhtml'
27 }.freeze
28 $counters = {}
29
30 def to_xhtml(subpath = "", item = @item)
31     if item.identifier =~ '/index.*'
32         ret =  "/" + subpath + "/index.xhtml"
33     else
34         ret = item.identifier.without_ext + "/" + subpath + "/index.xhtml"
35     end
36
37     return ret.gsub(/\/+/, "/")
38 end
39
40 def item_source(item = @item)
41     filename = "content" + item.identifier
42     filebase = filename.chomp(File.extname(filename))
43
44     [ if item.binary? then filebase + ".yaml" end,
45       filename].compact.find { |f| File.file? f }
46 end
47
48 def item_uri(item = @item, rep: :default)
49     return item.path(rep: rep).sub(%r{/index[.][^.]*$}, "/")
50 end
51
52 def rep_uri(rep = @rep)
53     return rep.path.sub(%r{/index[.][^.]*$}, "/")
54 end
55
56 def find_license(license)
57     matches = @items.find_all("/license/" + license + ".*")
58
59     raise("License not defined: " + license) if !matches.length
60     return matches.sort_by { |item| item.identifier } [0]
61 end
62
63 # Return the first paragraph of the given item as a string.
64 def item_longdesc(item)
65     xml = Nokogiri::XML("<body xmlns='" + Xmlns["xhtml"] + "'>" +
66                         item.compiled_content(snapshot: :rawbody) +
67                         "</body>")
68
69     p = xml.xpath('//xhtml:p', Xmlns)
70     if p.empty? then nil else p[0].xpath('string(.)') end
71 end
72
73 def counter(name = :default, item = @item)
74     $counters[item] ||= {}
75     $counters[item][name] ||= 0
76
77     name.to_s.capitalize + " " + ($counters[item][name] += 1).to_s
78 end
79
80 def img_rep_fallback(item, rep)
81     return rep unless item.reps[rep].raw_path.nil?
82     return :large
83 end
84
85 def item_to_img(item, rep: :large, alt: nil, caption: nil)
86     unless item
87         return "[image not found]"
88     end
89
90     alt ||= item[:title]
91     caption ||= alt
92     caption = caption.strip
93     caption.gsub!(/\s+/, " ")
94
95     rep = img_rep_fallback(item, rep)
96     attrs = { :src => item_uri(item, rep: rep), :alt => item[:title] }
97     attrs[:width], attrs[:height] = FastImage.size(item.reps[rep].raw_path)
98
99     b = Nokogiri::XML::Builder.new do |xml|
100         xml.a(:href => item_uri(item, rep: :info)) {
101             xml.img(attrs)
102             unless caption.empty?
103                 xml << " &#x2060;"
104                 xml.small {
105                     xml << caption
106                 }
107             end
108         }
109     end
110     b.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::NO_DECLARATION)
111 end
112
113 def expand_copyright(copyright)
114     result = { :years => {} }
115
116     /^([-–[:digit:][:space:],]*)(.*)$/.match(copyright) do |m|
117         m[1].split(/\s*,\s*/).each do |range|
118             lo, hi = range.split(/[^[:digit:]]+/)
119             for y in (lo..hi||lo)
120                 result[:years][y] = 1
121             end
122         end
123
124         result[:years] = result[:years].keys.sort
125         result[:name] = m[2]
126         return result
127     end
128
129     return nil
130 end
131
132 def path_to_rep(path)
133     @items.find_all(File::dirname(path) + "/*").each do |item|
134         item.reps.each do |rep|
135             return rep if rep.path == path
136         end
137     end
138     return nil
139 end
140
141 def find_images(item = @item)
142     return [] if item.binary?
143
144     result = {}
145     doc = Nokogiri::HTML(item.compiled_content(snapshot: :pre))
146     doc.xpath("//img/@src").each do |imgsrc|
147         rep = path_to_rep(imgsrc.value)
148         if rep
149             result[rep.item.identifier] ||= rep.item
150         end
151     end
152     return result.values
153 end
154
155 def human_filesize(size)
156     units = ["B", "KiB", "MiB", "GiB"]
157     prec = 0
158
159     for unit in units
160         if (size < 1024)
161             break
162         end
163
164         size /= 1024.0
165         prec = 1
166     end
167
168     sprintf("%.*f %s", prec, size + 0.05, unit)
169 end
170
171 def license_shortname(item)
172     item.fetch(:shortname, File::basename(item.identifier.without_ext).upcase)
173 end