Class: Wikilink::Converter::Site

Inherits:
Object
  • Object
show all
Includes:
ArgumentExtractor
Defined in:
lib/wikilink/converter/site.rb

Overview

Site converter

Constant Summary collapse

CURRENT_SITE_NAME =
''
DEFAULT_NAMESPACE =
::Wikilink::Converter::Namespace::DEFAULT_NAME

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ArgumentExtractor

#extract_arguments

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ Site

Returns a new instance of Site.

Yields:

  • (_self)

Yield Parameters:



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/wikilink/converter/site.rb', line 14

def initialize(options = {})
  @options = options
  is_current_site = @options[:name].to_s == CURRENT_SITE_NAME
  @options[:external] = !is_current_site unless @options.key?(:external)
  @options[:prefix] ||= '/' if is_current_site
  @namespace_converters = {}

  namespace(DEFAULT_NAMESPACE)
  
  yield self if block_given?
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/wikilink/converter/site.rb', line 12

def options
  @options
end

Instance Method Details

#default_namespace(*args, &block) ⇒ Object



54
55
56
# File 'lib/wikilink/converter/site.rb', line 54

def default_namespace(*args, &block)
  namespace(DEFAULT_NAMESPACE, *args, &block)
end

#namespace(*args, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/wikilink/converter/site.rb', line 26

def namespace(*args, &block)
  namespace, converter, options = extract_arguments(*args)
  namespace = DEFAULT_NAMESPACE if namespace.to_s.empty?
  
  converter ||= namespace_converter(namespace)
  if converter.nil?
    # do not add new handler if there's a instance method can handle it,
    # except that user passes a block to override it
    if block || instance_method_converter(namespace).nil?
      converter = ::Wikilink::Converter::Namespace
      converter = ::Wikilink::Converter::Namespace::Default if !block && namespace == DEFAULT_NAMESPACE
    end
  end

  if converter.is_a?(Class)
    options = default_options_for_namespace.merge(options)
    options[:name] ||= namespace
    converter = converter.new(options)
  end

  if converter.respond_to?(:config) && block
    converter.config(&block)
  end

  set_namespace_converter namespace, converter if converter
  self
end

#run(namespace, run_options) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/wikilink/converter/site.rb', line 58

def run(namespace, run_options)
  if converter = namespace_converter(namespace)
    converter.run(run_options)
  elsif converter = instance_method_converter(namespace)
    converter.call(run_options)
  end
end