Class: Wikilink::Converter

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
ArgumentExtractor
Defined in:
lib/wikilink/converter.rb,
lib/wikilink/converter/site.rb,
lib/wikilink/converter/utils.rb,
lib/wikilink/converter/namespace.rb,
lib/wikilink/converter/sites/wikipedia.rb,
lib/wikilink/converter/sites/ruby_china.rb

Overview

Convert [[Wikilink]] to HTML

The real work is handed over to registered handlers through #on.

The parsing rules

  • [[Wikilink]] should be in one line, otherwise it is ignored.
  • \[[Wikilink]] is escaped and converted to [[Wikilink]].
  • [[action:arg]] triggers handler registered on action. arg is passed as first argument to the handler. arg can contains colons.
  • [[:action:arg]] is the same with [[action:arg]], except that true is passed as the second argument to handler to indicate a prefix colon exists. It is useful for resources like image, where no colon version inserts the image and colon version inserts the link.
  • [[Wikilink]] is identical with [[page:Wikilink]], i.e., the default action is page.

Defined Under Namespace

Modules: ArgumentExtractor, HTMLAttributes, LinkHelper, Sites Classes: Namespace, Site

Constant Summary collapse

CURRENT_SITE =
::Wikilink::Converter::Site::CURRENT_SITE_NAME

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ArgumentExtractor

#extract_arguments

Constructor Details

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

Setup a converter. Handlers can be registered in block directly. If no handler is registered on page, a default handler Wikilink::Converter::Page is created with the given options.

Parameters:

  • options (Hash) (defaults to: {})

    options for Wikilink::Converter::Page

Yields:

  • (_self)

Yield Parameters:



48
49
50
51
52
53
54
55
# File 'lib/wikilink/converter.rb', line 48

def initialize(options = {})
  @site_converts = {}
  @action_handlers = {}
  @options = options

  site(CURRENT_SITE, @options)
  yield self if block_given?
end

Class Method Details

.config {|instance| ... } ⇒ Object

Yields:



33
34
35
# File 'lib/wikilink/converter.rb', line 33

def config
  yield instance
end

.instanceObject



30
31
32
# File 'lib/wikilink/converter.rb', line 30

def instance
  @instance ||= Converter.new
end

Instance Method Details

#action(name, &block) ⇒ Object



132
133
134
135
# File 'lib/wikilink/converter.rb', line 132

def action(name, &block)
  @action_handlers[name.to_s.downcase] = block
  self
end

#current_site(*args, &block) ⇒ Object



128
129
130
# File 'lib/wikilink/converter.rb', line 128

def current_site(*args, &block)
  site(CURRENT_SITE, *args, &block)
end

#execute(text) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/wikilink/converter.rb', line 95

def execute(text)
  text.gsub(/(^|.)\{\{(.*?[^:])\}\}/) do |match|
    prefix, inner = $1, $2.strip
    if prefix == '\\'
      match[1..-1]
    else
      action, arg = inner.split(':', 2)
      result = convert_action(action, arg)
      result ? ($1 + result) : match
    end
  end
end

#run(text, run_options = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/wikilink/converter.rb', line 57

def run(text, run_options = {})
  text.gsub(/(^|.)\[\[(.*?[^:])\]\]/) do |match|
    prefix, inner = $1, $2.strip
    if prefix == '\\'
      match[1..-1]
    else
      if inner.start_with?(':')
        colon = ':'
        inner = inner[1..-1]
      end
      inner = inner.gsub(///, '/')
      link, name = inner.split('|', 2)
      path, namespace, site = link.split(':', 3).reverse

      if site.to_s.empty? && !namespace.to_s.empty?
        # if namespace is a valid site name, use it as site
        if site_converter(namespace)
          site = namespace
          namespace = nil
        end
      end

      if name.to_s.empty?
        name = resolve_name(inner, run_options)
      end

      # ignore malformed wikilink
      if valid?(site, namespace, path)
        run_options = run_options.merge(path: path, name: name, colon: colon)
        result = convert_link(site, namespace, run_options)
        result ? (prefix + result) : match
      else
        match
      end
    end
  end
end

#site(*args) {|converter| ... } ⇒ Object

Yields:

  • (converter)


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/wikilink/converter.rb', line 111

def site(*args)
  site, converter, options = extract_arguments(*args)
  options = @options.merge(options)
  site = CURRENT_SITE if site.to_s.empty?

  converter ||= site_converter(site) || Wikilink::Converter::Site
  if converter.is_a?(Class)
    options[:name] ||= site
    converter = converter.new(options)
  end

  yield converter if block_given?

  set_site_converter site, converter
  self
end