Sha256: b8f90f960e285b15247b67774206760fe9440e81ee4c9fe74627a6ec3d66c4ce

Contents?: true

Size: 1.18 KB

Versions: 3

Compression:

Stored size: 1.18 KB

Contents

require 'wikilink/converter/utils'

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
            [options[:prefix], path, options[:suffix], query, fragment].join
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
wikilink-converter-0.2.3 lib/wikilink/converter/namespace.rb
wikilink-converter-0.2.2 lib/wikilink/converter/namespace.rb
wikilink-converter-0.2.1 lib/wikilink/converter/namespace.rb