Sha256: 0501f1e0c17cf61d697d17f1926487c69013e2233b56ecd3374ac6679e4c8b2c

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

require 'wikilink/converter/utils'
require 'uri'

module Wikilink
  class Converter
    # Namespace converter
    class Namespace
      include LinkHelper
      include HTMLAttributes

      DEFAULT_NAME = ''

      attr_reader :options

      def initialize(options = {})
        @options = options
      end

      def config(&block)
        @block = block
        self
      end

      def run(run_options)
        if @block
          instance_exec(run_options, &@block)
        end
      end

      class Default < Namespace
        def run(run_options)
          return super if @block

          path = run_options[:path].to_s
          path, fragment = path.split('#', 2)
          path, query = path.split('?', 2)

          fragment = '#' + fragment if fragment
          query = '?' + query if query

          url = to_url(path, fragment, query)

          link_to(run_options[:name], url, :class => html_class(run_options[:class]))
        end

        def to_url(path, fragment, query)
          if path.nil? || path.empty?
            [query, fragment].join
          else
            if options[:prefix]
              prefix = URI.parse(URI.escape(options[:prefix]))
              prefix.path = File.expand_path(URI.escape(path), prefix.path)
              path = prefix.to_s
            end
            [path, options[:suffix], query, fragment].join
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wikilink-converter-0.2.4 lib/wikilink/converter/namespace.rb