Module: Middleman::Redirects

Defined in:
lib/middleman/redirects.rb,
lib/middleman/redirects/version.rb

Defined Under Namespace

Classes: Extension, Middleware

Constant Summary collapse

BLANK_RE =
/\A[[:space:]]*\z/
VERSION =
"0.2.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.match_trailing_slashesObject

Returns the value of attribute match_trailing_slashes.



35
36
37
# File 'lib/middleman/redirects.rb', line 35

def match_trailing_slashes
  @match_trailing_slashes
end

Class Method Details

.read_redirectsObject



54
55
56
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
# File 'lib/middleman/redirects.rb', line 54

def self.read_redirects
  @mtime = self.redirects_file_path.mtime
  @redirects = Hash[self.redirects_file_path.each_line.map do |line|
    next if line =~ /^#/ # ignore comments
    next if line.empty?
    next if BLANK_RE === line

    source, destination = line.split.map { |s| URI.parse(s) }

    # don't allow a single / or * as source
    next if source.path == '/'
    next if source.path == '.*'
    next if source.path == '*' # invalid

    [source.to_s, destination.to_s]
  end.compact]

  autogenerated_redirects = {}

  # duplicate redirect so it also matches URIs with a trailing slash
  if self.match_trailing_slashes
    @redirects.each do |source, destination|
      next if source[-1] == '/'
      autogenerated_redirects["#{source}/"] = destination
    end
  end

  @redirects.merge!(autogenerated_redirects)
end

.redirectsObject



46
47
48
49
50
51
52
# File 'lib/middleman/redirects.rb', line 46

def self.redirects
  if self.redirects_file_path.mtime != @mtime
    read_redirects
  else
    @redirects
  end
end

.redirects_file_pathObject



42
43
44
# File 'lib/middleman/redirects.rb', line 42

def self.redirects_file_path
  @redirects_file_path
end

.redirects_file_path=(v) ⇒ Object



38
39
40
# File 'lib/middleman/redirects.rb', line 38

def self.redirects_file_path=(v)
  @redirects_file_path = v
end