Class: Utopia::Content

Inherits:
Object
  • Object
show all
Defined in:
lib/utopia/content.rb,
lib/utopia/content/tag.rb,
lib/utopia/content/link.rb,
lib/utopia/content/node.rb,
lib/utopia/content/links.rb,
lib/utopia/content/processor.rb

Defined Under Namespace

Classes: Link, Links, Node, Processor, Tag, Transaction, UnbalancedTagError

Constant Summary collapse

XNODE_EXTENSION =
'.xnode'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Content

Returns a new instance of Content.



31
32
33
34
35
36
37
38
39
# File 'lib/utopia/content.rb', line 31

def initialize(app, options = {})
	@app = app

	@root = File.expand_path(options[:root] || Utopia::default_root)

	@templates = options[:cache_templates] ? {} : nil

	@tags = options.fetch(:tags, {})
end

Instance Attribute Details

#passthroughObject (readonly)

Returns the value of attribute passthrough.



42
43
44
# File 'lib/utopia/content.rb', line 42

def passthrough
  @passthrough
end

#rootObject (readonly)

Returns the value of attribute root.



41
42
43
# File 'lib/utopia/content.rb', line 41

def root
  @root
end

Instance Method Details

#call(env) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/utopia/content.rb', line 105

def call(env)
	request = Rack::Request.new(env)
	path = Path.create(request.path_info)
	
	# Check if the request is to a non-specific index. This only works for requests with a given name:
	basename = path.basename
	directory_path = File.join(@root, path.dirname.components, basename.name)

	# If the request for /foo/bar{extensions} is actually a directory, rewrite it to /foo/bar/index{extensions}:
	if File.directory? directory_path
		index_path = [basename.name, basename.rename("index")]
		
		return [307, {"Location" => path.dirname.join(index_path).to_s}, []]
	end

	locale = env[Localization::CURRENT_LOCALE_KEY]
	if link = Links.for(@root, path, locale)
		if node = lookup_node(link.path)
			response = Rack::Response.new
			
			attributes = nil
			
			if request.respond_to?(:controller)
				attributes = request.controller
			end
			
			node.process!(request, response, (attributes || {}).to_hash)
			
			return response.finish
		end
	end
	
	return @app.call(env)
end

#fetch_xml(path) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/utopia/content.rb', line 44

def fetch_xml(path)
	if @templates
		@templates.fetch(path) do |key|
			@templates[key] = Trenni::Template.load(path)
		end
	else
		Trenni::Template.load(path)
	end
end

#lookup_node(request_path) ⇒ Object

The request_path is an absolute uri path, e.g. /foo/bar. If an xnode file exists on disk for this exact path, it is instantiated, otherwise nil.



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/utopia/content.rb', line 92

def lookup_node(request_path)
	name = request_path.last
	name_xnode = name.to_s + XNODE_EXTENSION

	node_path = File.join(@root, request_path.dirname.components, name_xnode)

	if File.exist? node_path
		return Node.new(self, request_path.dirname + name, request_path, node_path)
	end

	return nil
end

#lookup_tag(name, parent_path) ⇒ Object

Look up a named tag such as <entry />



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
# File 'lib/utopia/content.rb', line 55

def lookup_tag(name, parent_path)
	if @tags.key? name
		return @tags[name]
	end
	
	if String === name && name.index("/")
		name = Path.create(name)
	end
	
	if Path === name
		name = parent_path + name
		name_path = name.components.dup
		name_path[-1] += XNODE_EXTENSION
	else
		name_path = name + XNODE_EXTENSION
	end

	parent_path.ascend do |dir|
		tag_path = File.join(root, dir.components, name_path)

		if File.exist? tag_path
			return Node.new(self, dir + name, parent_path + name, tag_path)
		end

		if String === name_path
			tag_path = File.join(root, dir.components, "_" + name_path)

			if File.exist? tag_path
				return Node.new(self, dir + name, parent_path + name, tag_path)
			end
		end
	end
	
	return nil
end