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