module Webgen::Tag::Relocatable

Makes a path relative.

For example, you normally include a stylesheet in a template. If you specify the path name of the stylesheet directly, the reference to the stylesheet in the output file of a page file that is not in the same directory as the template would be invalid.

By using the relocatable tag you ensure that the path stays valid.

Public Class Methods

call(tag, body, context) click to toggle source

Return the relativized path for the path provided in the tag definition.

   # File lib/webgen/tag/relocatable.rb
17 def self.call(tag, body, context)
18   path = context[:config]['tag.relocatable.path']
19   result = ''
20   begin
21     result = (Webgen::Path.absolute?(path) ? path : resolve_path(path, context))
22   rescue URI::InvalidURIError => e
23     context.website.logger.warn do
24       ["Could not parse path '#{path}' for tag.relocatable in <#{context.ref_node}>",
25        e.message]
26     end
27   end
28   result
29 end
resolve_path(path, context) click to toggle source

Resolve the path using the reference node and return the correct relative path from the destination node.

   # File lib/webgen/tag/relocatable.rb
33 def self.resolve_path(path, context)
34   fragment = ''
35 
36   if context[:config]['tag.relocatable.ignore_unknown_fragment']
37     file, *fragments = path.split('#')
38     fragment = '#' << fragments.join('#') unless fragments.empty?
39     dest_node = context.ref_node.resolve(file, context.dest_node.lang, true)
40     context.website.logger.vinfo do
41       "Ignoring unknown fragment part of path '#{path}' for tag.relocatable in <#{context.ref_node}>"
42     end if dest_node && fragment.length > 0
43   else
44     dest_node = context.ref_node.resolve(path, context.dest_node.lang, true)
45   end
46 
47   if dest_node
48     context.website.ext.item_tracker.add(context.dest_node, :node_meta_info, dest_node)
49     context.dest_node.route_to(dest_node) + fragment
50   else
51     ''
52   end
53 end