Module: Webgen::Tag::Relocatable

Defined in:
lib/webgen/tag/relocatable.rb

Overview

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.

Class Method Summary collapse

Class Method Details

.call(tag, body, context) ⇒ Object

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



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/webgen/tag/relocatable.rb', line 18

def self.call(tag, body, context)
  path = context[:config]['tag.relocatable.path']
  result = ''
  begin
    result = (Webgen::Path.absolute?(path) ? path : resolve_path(path, context))
  rescue URI::InvalidURIError => e
    context.website.logger.warn do
      ["Could not parse path '#{path}' for tag.relocatable in <#{context.ref_node}>",
       e.message]
    end
  end
  result
end

.resolve_path(path, context) ⇒ Object

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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/webgen/tag/relocatable.rb', line 34

def self.resolve_path(path, context)
  fragment = ''

  if context[:config]['tag.relocatable.ignore_unknown_fragment']
    file, *fragments = path.split('#')
    fragment = '#' << fragments.join('#') unless fragments.empty?
    dest_node = context.ref_node.resolve(file, context.dest_node.lang, true)
    context.website.logger.vinfo do
      "Ignoring unknown fragment part of path '#{path}' for tag.relocatable in <#{context.ref_node}>"
    end if dest_node && fragment.length > 0
  else
    dest_node = context.ref_node.resolve(path, context.dest_node.lang, true)
  end

  if dest_node
    context.website.ext.item_tracker.add(context.dest_node, :node_meta_info, dest_node)
    context.dest_node.route_to(dest_node) + fragment
  else
    ''
  end
end