Class: Slamdown::Conversion::Normaliser::UrlRewriter

Inherits:
Object
  • Object
show all
Defined in:
lib/slamdown/conversion/normaliser/url_rewriter.rb

Overview

Applies the validated URL pattern to usable link and image destinations without changing unrelated domains or root paths.

Constant Summary collapse

ABSOLUTE_SCHEME =
/\A[A-Za-z][A-Za-z0-9+.-]*:\/\//.freeze

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ UrlRewriter

Returns a new instance of UrlRewriter.



11
12
13
14
15
16
# File 'lib/slamdown/conversion/normaliser/url_rewriter.rb', line 11

def initialize(options)
	@force = options.fetch(:force)
	@scheme = clean_scheme(options[:scheme])
	@host = clean_host(options[:host])
	@path = clean_path(options[:path])
end

Instance Method Details

#rewrite(value) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/slamdown/conversion/normaliser/url_rewriter.rb', line 18

def rewrite(value)
	return value unless active?

	uri = URI.parse(value)
	return rewrite_absolute(uri, value) if absolute?(uri, value)
	return rewrite_root_relative(value) if value.start_with?("/")
	return value if value.start_with?("?", "#")

	path = join_paths(@path, value)
	@force == :absolute ? absolute_url(path) : root_relative(path)
rescue URI::InvalidURIError
	value
end