137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
# File 'lib/utopia/link.rb', line 137
def self.index(root, top, options = {})
options = DEFAULT_OPTIONS.merge(options)
path = File.join(root, top.components)
metadata = Links.metadata(path)
virtual_metadata = metadata.dup
links = []
Dir.entries(path).each do |filename|
next if filename.match(/^[\._]/)
fullpath = File.join(path, filename)
if File.directory?(fullpath) && options[:directories]
name = filename
indices_metadata = Links.metadata(fullpath)
directory_metadata = metadata.delete(name) || {}
indices = 0
Links.indices(fullpath) do |index|
index_name = File.basename(index, ".xnode")
index_metadata = directory_metadata.merge(indices_metadata[index_name] || {})
directory_link = Link.new(:directory, top + [filename, index_name], index_metadata)
if directory_link.locale
localized_metadata = metadata.delete(name + "." + directory_link.locale)
if localized_metadata
directory_link.info.update(localized_metadata.symbolize_keys)
end
end
links << directory_link
indices += 1
end
if indices == 0
links << Link.new(:directory, top + [filename, ""], {'uri' => nil}.merge(directory_metadata))
end
elsif filename.match(INDEX_XNODE_FILTER) && options[:indices] == false
name = $1
metadata.delete(name)
next
elsif filename.match(XNODE_FILTER) && options[:files]
name = $1
links << Link.new(:file, top + name, metadata.delete(name))
end
end
if options[:virtual]
metadata.each do |name, details|
basename, locale = name.split(".", 2)
if virtual_metadata[basename]
details = virtual_metadata[basename].merge(details || {})
name = basename
details[:locale] = locale
end
links << Link.new(:virtual, name, details)
end
end
if options[:display]
links = links.delete_if{|link| link[options[:display]] == false}
end
if options[:name]
case options[:name]
when Regexp
links.reject!{|link| !link.name.match(options[:name])}
when String
links.reject!{|link| link.name.index(options[:name]) != 0}
end
end
if options[:locale]
reduced = []
links.group_by(&:name).each do |name, links|
specific = nil
links.each do |link|
if link.locale == options[:locale]
specific = link
break
elsif link.default_locale
specific ||= link
end
end
reduced << specific if specific
end
links = reduced
end
if options[:sort]
sort_key = options[:sort]
sort_default = options[:sort_default] || 0
links = links.sort do |a, b|
result = nil
lhs = a[sort_key] || sort_default
rhs = b[sort_key] || sort_default
begin
result ||= lhs <=> rhs
rescue
end
if result == 0 || result == nil
a.title <=> b.title
else
result
end
end
end
return links
end
|