Class: Utopia::Middleware::Content

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

Defined Under Namespace

Classes: Node, Processor, Transaction, UnbalancedTagError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Content.



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/utopia/middleware/content.rb', line 34

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

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

	# Set to hash to enable caching
	@nodes = {}
	@files = nil

	@tags = options[:tags] || {}
end

Instance Attribute Details

#passthroughObject (readonly)

Returns the value of attribute passthrough.



47
48
49
# File 'lib/utopia/middleware/content.rb', line 47

def passthrough
  @passthrough
end

#rootObject (readonly)

Returns the value of attribute root.



46
47
48
# File 'lib/utopia/middleware/content.rb', line 46

def root
  @root
end

Instance Method Details

#call(env) ⇒ Object



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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/utopia/middleware/content.rb', line 113

def call(env)
	request = Rack::Request.new(env)
	path = Path.create(request.path_info).to_absolute

	# Check if the request is to a non-specific index.
	name, extensions = path.basename.split(".", 2)
	directory_path = File.join(@root, path.dirname.components, name)

	if File.directory? directory_path
		if extensions
			index_path = [name, "index.#{extensions}"]
		else
			index_path = [name, "index"]
		end
		
		return [307, {"Location" => path.dirname.join(index_path).to_s}, []]
	end

	# Otherwise look up the node
	node = lookup_node(path)

	if node
		if request.head?
			return [200, {}, []]
		else
			response = Rack::Response.new
			
			attributes = {}
			
			if request.controller
				attributes = request.controller.to_hash
			end
			
			node.process!(request, response, attributes)
			return response.finish
		end
	else
		return @app.call(env)
	end
end

#fetch_xml(path) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/utopia/middleware/content.rb', line 49

def fetch_xml(path)
	read_file = lambda { Trenni::Template.load(path) }
	
	if @files
		@files.fetch(path) do |key|
			@files[key] = read_file.call
		end
	else
		read_file.call
	end
end

#lookup_node(request_path) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/utopia/middleware/content.rb', line 100

def lookup_node(request_path)
	name = request_path.basename
	name_xnode = name + ".xnode"

	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 />



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
92
93
94
95
96
97
98
# File 'lib/utopia/middleware/content.rb', line 62

def lookup_tag(name, parent_path)
	if @tags.key? name
		return @tags[name]
	elsif Utopia::Tags.all.key? name
		return Utopia::Tags.all[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"
	else
		name_path = name + ".xnode"
	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