1 # Nick's web site: Ruby helpers for processing
3 # Copyright © 2018-2021 Nick Bowler
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.
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.
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/>.
21 use_helper Nanoc::Helpers::Blogging
22 use_helper Nanoc::Helpers::Breadcrumbs
23 use_helper Nanoc::Helpers::Rendering
26 'xhtml' => 'http://www.w3.org/1999/xhtml'
30 def to_xhtml(subpath = "", item = @item)
31 if item.identifier =~ '/index.*'
32 ret = "/" + subpath + "/index.xhtml"
34 ret = item.identifier.without_ext + "/" + subpath + "/index.xhtml"
37 return ret.gsub(/\/+/, "/")
40 def item_source(item = @item)
41 filename = "content" + item.identifier
42 filebase = filename.chomp(File.extname(filename))
44 [ if item.binary? then filebase + ".yaml" end,
45 filename].compact.find { |f| File.file? f }
48 def item_uri(item = @item, rep: :default)
49 return item.path(rep: rep).sub(%r{/index[.][^.]*$}, "/")
52 def rep_uri(rep = @rep)
53 return rep.path.sub(%r{/index[.][^.]*$}, "/")
56 def find_license(license)
57 matches = @items.find_all("/license/" + license + ".*")
59 raise("License not defined: " + license) if !matches.length
60 return matches.sort_by { |item| item.identifier } [0]
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) +
69 p = xml.xpath('//xhtml:p', Xmlns)
70 if p.empty? then nil else p[0].xpath('string(.)') end
73 def counter(name = :default, item = @item)
74 $counters[item] ||= {}
75 $counters[item][name] ||= 0
77 name.to_s.capitalize + " " + ($counters[item][name] += 1).to_s
80 def img_rep_fallback(item, rep)
81 return rep unless item.reps[rep].raw_path.nil?
85 def gallery_img(item, rep: :large, alt: nil, caption: nil)
86 return "[image not found]" unless item
90 caption = caption.strip
91 caption.gsub!(/\s+/, " ")
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)
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?
102 xml.small { xml << caption }
106 b.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::NO_DECLARATION)
109 def expand_copyright(copyright)
110 result = { :years => {} }
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
120 result[:years] = result[:years].keys.sort
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
137 def find_images(item = @item)
138 return [] if item.binary?
141 doc = Nokogiri::HTML(item.compiled_content(snapshot: :pre))
142 doc.xpath("//img/@src").each do |imgsrc|
143 rep = path_to_rep(imgsrc.value)
145 result[rep.item.identifier] ||= rep.item
151 def human_filesize(size)
152 units = ["B", "KiB", "MiB", "GiB"]
164 sprintf("%.*f %s", prec, size + 0.05, unit)
167 def license_shortname(item)
168 item.fetch(:shortname, File::basename(item.identifier.without_ext).upcase)