]> git.draconx.ca Git - homepage.git/blob - lib/helpers.rb
M48T59Y battery replacement
[homepage.git] / lib / helpers.rb
1 # Nick's web site: Ruby helpers for processing
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 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 # Return a hash containing :src, :width and :height based on an image item rep.
81 def img_rep_attrs(item, rep)
82     rep = :large if item.reps[rep].raw_path.nil?
83     attrs = {}
84
85     attrs[:src] ||= item_uri(item, rep: rep)
86     attrs[:width], attrs[:height] = FastImage.size(item.reps[rep].raw_path)
87
88     return attrs
89 end
90
91 def embed_img(item, rep: :large, caption: nil, block_attrs: {}, img_attrs: {})
92     return "[image not found]" unless item
93
94     img_attrs[:alt] ||= item[:title]
95     caption ||= img_attrs[:alt]
96     caption = caption.strip
97     caption.gsub!(/\s+/, " ")
98
99     img_attrs = img_rep_attrs(item, rep).merge(img_attrs)
100     block_attrs[:href] = item_uri(item, rep: :info)
101
102     b = Nokogiri::XML::Builder.new do |xml|
103         xml.a(block_attrs) {
104             xml.img(img_attrs)
105             unless caption.empty?
106                 xml << " &#x2060;"
107                 xml.small { xml << caption }
108             end
109         }
110     end
111     b.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::NO_DECLARATION)
112 end
113
114 def gallery_img(item, rep: :medium, caption: nil, alt: nil)
115     attrs = { alt: alt, "generate-gallery" => "generate-gallery" }
116     embed_img(item, rep: rep, caption: caption, img_attrs: attrs)
117 end
118
119 def floating_img(item, rep: :medium, caption: nil, alt: nil, left: nil)
120     battrs = { class: if left then "left" else "right" end }
121     attrs = { alt: alt }
122
123     embed_img(item, rep: rep, caption: caption,
124                     block_attrs: battrs, img_attrs: attrs)
125 end
126
127 def expand_copyright(copyright)
128     result = { :years => {} }
129
130     /^([-–[:digit:][:space:],]*)(.*)$/.match(copyright) do |m|
131         m[1].split(/\s*,\s*/).each do |range|
132             lo, hi = range.split(/[^[:digit:]]+/)
133             for y in (lo..hi||lo)
134                 result[:years][y] = 1
135             end
136         end
137
138         result[:years] = result[:years].keys.sort
139         result[:name] = m[2]
140         return result
141     end
142
143     return nil
144 end
145
146 def path_to_rep(path)
147     @items.find_all(File::dirname(path) + "/*").each do |item|
148         item.reps.each do |rep|
149             return rep if rep.path == path
150         end
151     end
152     return nil
153 end
154
155 def find_images(item = @item)
156     return [] if item.binary?
157
158     result = {}
159     doc = Nokogiri::HTML(item.compiled_content(snapshot: :pre))
160     doc.xpath("//img/@src").each do |imgsrc|
161         rep = path_to_rep(imgsrc.value)
162         if rep
163             result[rep.item.identifier] ||= rep.item
164         end
165     end
166     return result.values
167 end
168
169 def human_filesize(size)
170     units = ["B", "KiB", "MiB", "GiB"]
171     prec = 0
172
173     for unit in units
174         if (size < 1024)
175             break
176         end
177
178         size /= 1024.0
179         prec = 1
180     end
181
182     sprintf("%.*f %s", prec, size + 0.05, unit)
183 end
184
185 def license_shortname(item)
186     item.fetch(:shortname, File::basename(item.identifier.without_ext).upcase)
187 end