utopia 0.9.39 → 0.9.40

Sign up to get free protection for your applications and to get access to all the features.
@@ -15,6 +15,8 @@ module Utopia
15
15
  def initialize(kind, path, info = nil)
16
16
  path = Path.create(path)
17
17
 
18
+ @info = info ? info.symbolize_keys : {}
19
+ @locale = @info.delete(:locale) || path.locale(XNODE_EXT)
18
20
  @kind = kind
19
21
 
20
22
  case @kind
@@ -26,19 +28,11 @@ module Utopia
26
28
  @path = path
27
29
  when :virtual
28
30
  @name = path.to_s
29
- @path = nil
31
+ @path = @info[:path] ? Path.create(@info[:path]) : nil
30
32
  end
31
33
 
32
34
  @components = @name.split(".")
33
- @locale = path.locale(XNODE_EXT)
34
-
35
35
  @title = components[0]
36
-
37
- if info
38
- @info = info.symbolize_keys
39
- else
40
- @info = {}
41
- end
42
36
  end
43
37
 
44
38
  attr :kind
@@ -103,6 +97,10 @@ module Utopia
103
97
  def == other
104
98
  return other && kind == other.kind && name == other.name && path == other.path
105
99
  end
100
+
101
+ def default_locale
102
+ @locale == ''
103
+ end
106
104
  end
107
105
 
108
106
  module Links
@@ -143,6 +141,8 @@ module Utopia
143
141
  options = DEFAULT_OPTIONS.merge(options)
144
142
  path = File.join(root, top.components)
145
143
  metadata = Links.metadata(path)
144
+
145
+ virtual_metadata = metadata.dup
146
146
 
147
147
  links = []
148
148
 
@@ -154,8 +154,8 @@ module Utopia
154
154
  if File.directory?(fullpath) && options[:directories]
155
155
  name = filename
156
156
  indices_metadata = Links.metadata(fullpath)
157
-
158
157
  directory_metadata = metadata.delete(name) || {}
158
+
159
159
  indices = 0
160
160
  Links.indices(fullpath) do |index|
161
161
  index_name = File.basename(index, ".xnode")
@@ -195,6 +195,16 @@ module Utopia
195
195
 
196
196
  if options[:virtual]
197
197
  metadata.each do |name, details|
198
+ # Given a virtual named such as "welcome.cn", merge it with metadata
199
+ # from "welcome" if it exists.
200
+ basename, locale = name.split(".", 2)
201
+
202
+ if virtual_metadata[basename]
203
+ details = virtual_metadata[basename].merge(details || {})
204
+ name = basename
205
+ details[:locale] = locale
206
+ end
207
+
198
208
  links << Link.new(:virtual, name, details)
199
209
  end
200
210
  end
@@ -216,15 +226,18 @@ module Utopia
216
226
  reduced = []
217
227
 
218
228
  links.group_by(&:name).each do |name, links|
219
- default = nil
220
-
221
- link = links.select{|link|
222
- link.locale == options[:locale] || link.locale == ''
223
- }.sort_by{|link| link.locale.size}.last
224
-
225
- if link
226
- reduced << link
229
+ specific = nil
230
+
231
+ links.each do |link|
232
+ if link.locale == options[:locale]
233
+ specific = link
234
+ break
235
+ elsif link.default_locale
236
+ specific ||= link
237
+ end
227
238
  end
239
+
240
+ reduced << specific if specific
228
241
  end
229
242
 
230
243
  links = reduced
@@ -2,12 +2,6 @@
2
2
  # Copyright 2010 Samuel Williams. All rights reserved.
3
3
  # See <utopia.rb> for licensing details.
4
4
  require 'utopia/middleware'
5
- require 'utopia/path'
6
-
7
- require 'time'
8
-
9
- require 'digest/sha1'
10
- require 'mime/types'
11
5
 
12
6
  module Utopia
13
7
  module Middleware
@@ -33,42 +33,68 @@ module Utopia
33
33
  end
34
34
 
35
35
  def named_locale(resource_name)
36
- Name.extract_locale(resource_name, @all_locales)
37
- end
38
-
39
- def current_locale(env, resource_name)
40
- Rack::Request.new(env).GET["locale"] || named_locale(resource_name) || @default_locale
36
+ if resource_name
37
+ Name.extract_locale(resource_name, @all_locales)
38
+ else
39
+ nil
40
+ end
41
41
  end
42
42
 
43
43
  attr :all_locales
44
44
  attr :default_locale
45
+
46
+ def check_resource(resource_name, resource_locale, env)
47
+ localized_name = Name.localized(resource_name, resource_locale, @all_locales).join(".")
48
+ localized_path = Path.create(env["PATH_INFO"]).dirname + localized_name
45
49
 
46
- def call(env)
47
- path = Path.create(env["PATH_INFO"])
50
+ localization_probe = env.dup
51
+ localization_probe["REQUEST_METHOD"] = "HEAD"
52
+ localization_probe["PATH_INFO"] = localized_path.to_s
48
53
 
49
- request_locale = current_locale(env, path.basename)
50
- resource_name = Name.nonlocalized(path.basename, @all_locales).join(".")
54
+ # Find out if a resource exists for the requested localization
55
+ return [localized_path, @app.call(localization_probe)]
56
+ end
51
57
 
52
- env["utopia.current_locale"] = request_locale
58
+ def call(env)
59
+ path = Path.create(env["PATH_INFO"])
53
60
  env["utopia.localization"] = self
54
61
 
55
- localized_name = Name.localized(resource_name, request_locale, @all_locales).join(".")
56
-
57
- localization_probe = env.dup
58
- localization_probe["REQUEST_METHOD"] = "HEAD"
59
- localization_probe["PATH_INFO"] = (path.dirname + localized_name).to_s
62
+ referer_locale = named_locale(env['HTTP_REFERER'])
63
+ request_locale = named_locale(path.basename)
64
+ resource_name = Name.nonlocalized(path.basename, @all_locales).join(".")
60
65
 
61
- response = @app.call(localization_probe)
66
+ response = nil
67
+ if request_locale
68
+ env["utopia.current_locale"] = request_locale
69
+ resource_path, response = check_resource(resource_name, request_locale, env)
70
+ elsif referer_locale
71
+ env["utopia.current_locale"] = referer_locale
72
+ resource_path, response = check_resource(resource_name, referer_locale, env)
73
+ end
74
+
75
+ # If the previous checks failed, i.e. there was no request/referer locale
76
+ # or the response was 404 (i.e. no localised resource), we check for the
77
+ # @default_locale
78
+ if response == nil || response[0] >= 400
79
+ env["utopia.current_locale"] = @default_locale
80
+ resource_path, response = check_resource(resource_name, @default_locale, env)
81
+ end
62
82
 
83
+ # If the response is 2xx, we have a localised resource
63
84
  if response[0] < 300
64
- if path.basename == localized_name
85
+ # If the original request was the same as the localized request,
86
+ if path.basename == resource_path.basename
87
+ # The resource URI is correct.
65
88
  return @app.call(env)
66
89
  else
67
- return [307, {"Location" => localization_probe["PATH_INFO"]}, []]
90
+ # Redirect to the correct resource URI.
91
+ return [307, {"Location" => resource_path.to_s}, []]
68
92
  end
69
93
  elsif response[0] < 400
94
+ # We have encountered a redirect while accessing the localized resource
70
95
  return response
71
96
  else
97
+ # A localized resource was not found, return the unlocalised resource path,
72
98
  if path.basename == resource_name
73
99
  return @app.call(env)
74
100
  else
@@ -6,7 +6,7 @@ module Utopia
6
6
  module VERSION
7
7
  MAJOR = 0
8
8
  MINOR = 9
9
- TINY = 39
9
+ TINY = 40
10
10
 
11
11
  STRING = [MAJOR, MINOR, TINY].join('.')
12
12
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: utopia
3
3
  version: !ruby/object:Gem::Version
4
- hash: 117
4
+ hash: 107
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 9
9
- - 39
10
- version: 0.9.39
9
+ - 40
10
+ version: 0.9.40
11
11
  platform: ruby
12
12
  authors:
13
13
  - Samuel Williams
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-10-22 00:00:00 +13:00
18
+ date: 2010-11-03 00:00:00 +13:00
19
19
  default_executable: utopia
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency