+ p = xml.xpath('//xhtml:p', Xmlns)
+ if p.empty? then nil else p[0].xpath('string(.)') end
+end
+
+def counter(name = :default, item = @item)
+ $counters[item] ||= {}
+ $counters[item][name] ||= 0
+
+ name.to_s.capitalize + " " + ($counters[item][name] += 1).to_s
+end
+
+# Return a hash containing :src, :width and :height based on an image item rep.
+def img_rep_attrs(item, rep)
+ rep = :large if item.reps[rep].raw_path.nil?
+ attrs = {}
+
+ attrs[:src] ||= item_uri(item, rep: rep)
+ attrs[:width], attrs[:height] = FastImage.size(item.reps[rep].raw_path)
+
+ return attrs
+end
+
+def embed_img(item, rep: :large, caption: nil, block_attrs: {}, img_attrs: {})
+ return "[image not found]" unless item
+
+ img_attrs[:alt] ||= item[:title]
+ caption ||= img_attrs[:alt]
+ caption = caption.strip
+ caption.gsub!(/\s+/, " ")
+
+ img_attrs = img_rep_attrs(item, rep).merge(img_attrs)
+ block_attrs[:href] = item_uri(item, rep: :info)
+
+ b = Nokogiri::XML::Builder.new do |xml|
+ xml.a(block_attrs) {
+ xml.img(img_attrs)
+ unless caption.empty?
+ xml << " ⁠"
+ xml.small { xml << caption }
+ end
+ }