Sha256: 6a38f935fe2f33afdc973e0ae3e9d40d57c852dcaf309324d866b9528d7bca71

Contents?: true

Size: 1.99 KB

Versions: 12

Compression:

Stored size: 1.99 KB

Contents

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

require 'uri'

module Webgen
  class Tag

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

      # Return the relativized path for the path provided in the tag definition.
      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 the path using the reference node and return the correct relative path from the
      # destination node.
      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

    end

  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
webgen-1.7.3 lib/webgen/tag/relocatable.rb
webgen-1.7.2 lib/webgen/tag/relocatable.rb
webgen-1.7.1 lib/webgen/tag/relocatable.rb
webgen-1.7.0 lib/webgen/tag/relocatable.rb
webgen-1.6.0 lib/webgen/tag/relocatable.rb
webgen-1.5.2 lib/webgen/tag/relocatable.rb
webgen-1.5.1 lib/webgen/tag/relocatable.rb
webgen-1.5.0 lib/webgen/tag/relocatable.rb
webgen-1.4.1 lib/webgen/tag/relocatable.rb
webgen-1.4.0 lib/webgen/tag/relocatable.rb
webgen-1.3.0 lib/webgen/tag/relocatable.rb
webgen-1.2.1 lib/webgen/tag/relocatable.rb