1 # Nick's web site: Ruby helpers for processing
3 # Copyright © 2018-2022 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 # 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?
85 attrs[:src] ||= item_uri(item, rep: rep)
86 attrs[:width], attrs[:height] = FastImage.size(item.reps[rep].raw_path)
91 def embed_img(item, rep: :large, caption: nil, block_attrs: {}, img_attrs: {})
92 return "[image not found]" unless item
94 img_attrs[:alt] ||= item[:title]
95 caption ||= img_attrs[:alt]
96 caption = caption.strip
97 caption.gsub!(/\s+/, " ")
99 img_attrs = img_rep_attrs(item, rep).merge(img_attrs)
100 block_attrs[:href] = item_uri(item, rep: :info)
102 b = Nokogiri::XML::Builder.new do |xml|
105 unless caption.empty?
107 xml.small { xml << caption }
111 b.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::NO_DECLARATION)
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)
119 def floating_img(item, rep: :medium, caption: nil, alt: nil, left: nil)
120 battrs = { class: if left then "left" else "right" end }
123 embed_img(item, rep: rep, caption: caption,
124 block_attrs: battrs, img_attrs: attrs)
127 def expand_copyright(copyright)
128 result = { :years => {} }
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
138 result[:years] = result[:years].keys.sort
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
155 def find_images(item = @item)
156 return [] if item.binary?
159 doc = Nokogiri::HTML(item.compiled_content(snapshot: :pre))
160 doc.xpath("//img/@src").each do |imgsrc|
161 rep = path_to_rep(imgsrc.value)
163 result[rep.item.identifier] ||= rep.item
169 def human_filesize(size)
170 units = ["B", "KiB", "MiB", "GiB"]
182 sprintf("%.*f %s", prec, size + 0.05, unit)
185 def license_shortname(item)
186 item.fetch(:shortname, File::basename(item.identifier.without_ext).upcase)