lib/wikilink/converter.rb in wikilink-converter-0.1.0 vs lib/wikilink/converter.rb in wikilink-converter-0.2.1
- old
+ new
@@ -23,25 +23,40 @@
class Converter
extend Forwardable
include ArgumentExtractor
CURRENT_SITE = ::Wikilink::Converter::Site::CURRENT_SITE_NAME
+ class << self
+ extend Forwardable
+ def instance
+ @instance ||= Converter.new
+ end
+ def config
+ yield instance
+ end
+ def_delegators(:instance,
+ :run, :execute,
+ :namespace, :default_namespace,
+ :site, :current_site,
+ :action)
+ end
+
# 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`.
#
# @param [Hash] options options for Wikilink::Converter::Page
def initialize(options = {})
@site_converts = {}
@action_handlers = {}
@options = options
- on_site(CURRENT_SITE, @options)
+ site(CURRENT_SITE, @options)
yield self if block_given?
end
- def run(text, current_page = nil)
+ def run(text, run_options = {})
text.gsub(/(^|.)\[\[(.*?[^:])\]\]/) do |match|
prefix, inner = $1, $2.strip
if prefix == '\\'
match[1..-1]
else
@@ -59,16 +74,17 @@
namespace = nil
end
end
if name.to_s.empty?
- name = resolve_name(inner, current_page)
+ name = resolve_name(inner, run_options)
end
# ignore malformed wikilink
if valid?(site, namespace, path)
- result = convert_link(colon, site, namespace, path, name, current_page)
+ run_options = run_options.merge(path: path, name: name, colon: colon)
+ result = convert_link(site, namespace, run_options)
result ? ($1 + result) : match
else
match
end
end
@@ -86,18 +102,14 @@
result ? ($1 + result) : match
end
end
end
- def_delegator :@current_site_converter, :on_namespace
- alias_method :on, :on_namespace
- alias_method :namespace, :on_namespace
+ def_delegator :@current_site_converter, :namespace
+ def_delegator :@current_site_converter, :default_namespace
- def_delegator :@current_site_converter, :on_default_namespace
- alias_method :default_namespace, :on_default_namespace
-
- def on_site(*args)
+ 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
@@ -109,24 +121,20 @@
yield converter if block_given?
set_site_converter site, converter
self
end
- alias_method :site, :on_site
- def on_current_site(*args, &block)
- on_site(CURRENT_SITE, *args, &block)
+ def current_site(*args, &block)
+ site(CURRENT_SITE, *args, &block)
end
- alias_method :current_site, :on_current_site
- def on_action(action, &block)
- @action_handlers[action.to_s.downcase] = block
+ def action(name, &block)
+ @action_handlers[name.to_s.downcase] = block
self
end
- alias_method :action, :on_action
-
private
def site_converter(site)
site = site.to_s.downcase
site == CURRENT_SITE ? @current_site_converter : @site_converts[site]
end
@@ -143,18 +151,18 @@
def convert_action(action, argument)
handler = @action_handlers[action.to_s.downcase]
handler.call(argument) if handler
end
- def convert_link(colon, site, namespace, path, name, current_page)
+ def convert_link(site, namespace, run_options)
converter = site_converter(site)
- converter.run(colon, namespace, path, name, current_page) if converter
+ converter.run(namespace, run_options) if converter
end
# TODO: relative
# TODO: ruby (computer) -> ruby
# TODO: Shanghai, China -> Shanghai
- def resolve_name(inner_text, current_path)
+ def resolve_name(inner_text, run_options)
if inner_text.end_with?('|')
inner_text.chop.chomp('/').split(%r{[:/]}, 2).last
else
inner_text
end