Module: Utopia::Links

Defined in:
lib/utopia/link.rb

Constant Summary collapse

XNODE_FILTER =
/^(.+)\.xnode$/
INDEX_XNODE_FILTER =
/^(index(\..+)*)\.xnode$/
"links.yaml"
DEFAULT_OPTIONS =
{
	:directories => true,
	:files => true,
	:virtual => true,
	:indices => false,
	:sort => :order,
	:display => :display,
	:locale => nil
}

Class Method Summary collapse

Class Method Details

.index(root, top, options = {}) ⇒ Object



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
270
271
272
273
# File 'lib/utopia/link.rb', line 142

def self.index(root, top, options = {})
	options = DEFAULT_OPTIONS.merge(options)
	path = File.join(root, top.components)
	 = Links.(path)
	
	 = .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
			 = Links.(fullpath)
			 = .delete(name) || {}

			indices = 0
			Links.indices(fullpath) do |index|
				index_name = File.basename(index, ".xnode")
				 = .merge([index_name] || {})

				directory_link = Link.new(:directory, top + [filename, index_name], )

				# Check for localized directory metadata and update the link
				if directory_link.locale
					 = .delete(name + "." + directory_link.locale)

					if 
						directory_link.info.update(.symbolize_keys)
					end
				end

				links << directory_link

				indices += 1
			end

			if indices == 0
				links << Link.new(:directory, top + [filename, ""], .merge(:uri => "\#"))
			end
		elsif filename.match(INDEX_XNODE_FILTER) && options[:indices] == false
			name = $1
			.delete(name)

			# We don't include indices in the list of pages.
			next
		elsif filename.match(XNODE_FILTER) && options[:files]
			name = $1

			links << Link.new(:file, top + name, .delete(name))
		end
	end

	if options[:virtual]
		.each do |name, details|
			# Given a virtual named such as "welcome.cn", merge it with metadata 
			# from "welcome" if it exists.
			basename, locale = name.split(".", 2)

			if [basename]
				details = [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
				# LOG.debug("Invalid comparison between #{a.path} and #{b.path} using key #{options[:sort]}!")
			end
			
			if result == 0 || result == nil
				a.title <=> b.title
			else
				result
			end
		end
	end
	
	return links
end

.indices(path, &block) ⇒ Object



122
123
124
125
126
127
128
129
130
# File 'lib/utopia/link.rb', line 122

def self.indices(path, &block)
	entries = Dir.entries(path).delete_if{|filename| !filename.match(INDEX_XNODE_FILTER)}

	if block_given?
		entries.each &block
	else
		return entries
	end
end

.metadata(path) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/utopia/link.rb', line 113

def self.(path)
	links_path = File.join(path, LINKS_YAML)
	if File.exist?(links_path)
		return YAML::load(File.read(links_path))
	else
		return {}
	end
end