Sha256: 60ab6db61749cf3dfe53d058044522ac5cc5ad8ff4e888f240129f32db071076

Contents?: true

Size: 1.58 KB

Versions: 2

Compression:

Stored size: 1.58 KB

Contents

# -*- encoding: utf-8 -*-

require 'uri'

module Webgen::Tag

  # 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.
  class Relocatable

    include Webgen::Tag::Base

    # Return the relativized path for the path provided in the tag definition.
    def call(tag, body, context)
      path = param('tag.relocatable.path')
      result = ''
      begin
        result = (Webgen::Node.url(path, false).absolute? ? path : resolve_path(path, context))
      rescue URI::InvalidURIError => e
        log(:error) { "Error while parsing path for tag relocatable in <#{context.ref_node.alcn}>: #{e.message}" }
        context.dest_node.flag(:dirty)
      end
      result
    end

    #######
    private
    #######

    # Resolve the path +path+ using the reference node and return the correct relative path from the
    # destination node.
    def resolve_path(path, context)
      dest_node = context.ref_node.resolve(path, context.dest_node.lang)
      if dest_node
        context.dest_node.node_info[:used_meta_info_nodes] << dest_node.alcn
        context.dest_node.route_to(dest_node)
      else
        log(:error) { "Could not resolve path '#{path}' in <#{context.ref_node.alcn}>" }
        context.dest_node.flag(:dirty)
        ''
      end
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gettalong-webgen-0.5.9.20090620 lib/webgen/tag/relocatable.rb
gettalong-webgen-0.5.9.20090626 lib/webgen/tag/relocatable.rb