Class: Webgen::Tag::Relocatable

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/webgen/tag/relocatable.rb

Overview

Makes a path relative. This is very useful for templates. For example, you normally include a stylesheet in a template. If you specify the filename 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.

Instance Method Summary collapse

Methods included from Base

#create_tag_params, #param, #set_params

Methods included from WebsiteAccess

included, website

Methods included from Loggable

#log, #puts

Instance 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
31
32
33
34
35
36
37
38
39
# File 'lib/webgen/tag/relocatable.rb', line 18

def call(tag, body, context)
  uri_string = param('tag.relocatable.path')
  result = ''
  unless uri_string.nil?
    begin
      uri = URI.parse(uri_string)
      if uri.absolute?
        result = uri_string
      else
        result = resolve_path(uri_string, context)
      end
      if result.empty?
        log(:error) { "Could not resolve path '#{uri_string}' in <#{context.ref_node.absolute_lcn}>" }
        context.dest_node.flag(:dirty)
      end
    rescue URI::InvalidURIError => e
      log(:error) { "Error while parsing path for tag relocatable in <#{context.ref_node.absolute_lcn}>: #{e.message}" }
      context.dest_node.flag(:dirty)
    end
  end
  result
end