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.3.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
83
84
85
86
87
88
89
90
91
# File 'lib/middleman/redirects.rb', line 54

def self.read_redirects
  @mtime = self.redirects_file_path.mtime
  @redirects = {}

  self.redirects_file_path.each_line.map do |line|
    next if line =~ /^#/ # ignore comments
    next if line.empty?
    next if BLANK_RE === line

    parts = line.split
    source = URI.parse(parts[0])
    destination = URI.parse(parts[1])
    redirect_code = if parts[2] =~ /permanent/
                      301
                    else
                      302
                    end

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

    @redirects[source.to_s] = [destination.to_s, redirect_code]
  end

  autogenerated_redirects = {}

  # duplicate redirect so it also matches URIs with a trailing slash
  if self.match_trailing_slashes
    @redirects.each do |source, destination_and_code|
      next if source[-1] == '/'
      autogenerated_redirects["#{source}/"] = destination_and_code
    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