$: << "lib"
require 'enid'
require 'htemplate'
begin
require 'enid/ferret'
rescue LoadError
end
$: << "/Users/chris/src/atom-tools/lib"
require 'atom/feed'
unless defined? AUTHOR
AUTHOR = Atom::Author.new
AUTHOR.name = "Anonymous Coward"
end
$: << "/Users/chris/projects/rack/lib"
$: << "/Users/chris/projects/coset/lib"
require 'rack'
require 'coset'
Rack::File::MIME_TYPES['atom'] = 'application/atom+xml'
class Rack::Request
def app_root
url = scheme + "://"
url << host
if scheme == "https" && port != 443 ||
scheme == "http" && port != 80
url << ":#{port}"
end
url << script_name
end
end
class WebEnid < Coset
def initialize(dir)
@dir = dir
@top = Enid.load_all dir
@world = @top.add_to_world
@top.resolve_reverse_links(@world)
@top.resolve @world
@world = @top.add_to_world
@top.sort!
@top.open_index "index/ferretsearch" if defined? Ferret
@index = @top.reverse_index
@linkmenu = @world["linkmenu"].children rescue []
@updated = Time.now
pp @world.keys
end
GET("/") {
res["Location"] = "/home"
res.status = 302
}
GET("/reload") {
initialize @dir
res["Location"] = "/home"
res.status = 302
}
GET("/reindex") {
@top.reindex @world
res["Location"] = "/home"
res.status = 302
}
GET("/favicon.ico") {
res["Content-Type"] = "image/png"
res.write File.read("static/favicon.ico")
}
DEFAULT_VIEW = HTemplate.new(<<'EOF')
vuxu.org: $@title
$ if @linkmenu
$ end
$ unless @parents.empty?
up:
$ end
$:@content
EOF
GET("/search") {
term = req["q"]
@title = "Search for #{term}"
if defined? Ferret
results, total = @top.search_ferret(term)
unless results.empty?
@content = "Search for #{term}: #{total} entries
"
@content << '"
else
@content = "No results for #{term}
"
end
else
ids = []
@top.search(term) { |entry|
ids << entry.id
break if ids.size > 100
}
ids.uniq!
@content = "Search for #{term}: #{ids.size} entries
"
@content << '"
end
@parents = []
res["Content-Type"] = "text/html"
res.write DEFAULT_VIEW.expand(self)
}
GET("/{id:all}{EXT}") {
if @world.include? @id
@entry = @world[@id]
@title = @entry.entry.pretty
@parents = @index[@id].map { |pid|
@world[pid]
}
@content = @entry.to_html
else
res.status = 404
return
end
p @EXT
wants "text/html" do
res["Content-Type"] = "text/html"
res.write DEFAULT_VIEW.expand(self)
end
wants "application/atom+xml" do
res["Content-Type"] = "application/atom+xml"
feed = Atom::Feed.new
feed.id = req.app_root + "/" + @entry.href + ".atom#atom-feed"
selflink = Atom::Link.new
selflink["href"] = req.app_root + "/" + @entry.href + ".atom"
selflink["rel"] = "self"
feed.links << selflink
feed.title = @entry.entry.pretty
feed.updated = @updated
feed.authors << AUTHOR
@entry.children.each { |child|
post = Atom::Entry.new
post.base = req.app_root
post.id = req.app_root + "/" + child.href
post.title = child.entry.pretty
post.title["type"] = "html"
post.content = child.to_html(0, 3, true)
post.content["type"] = "html"
post.updated = @updated
permalink = Atom::Link.new
permalink["href"] = "/" + child.href
permalink["rel"] = "alternate"
post.links << permalink
feed << post
}
p req
res.write feed
end
}
end
if $0 == __FILE__
app = Rack::URLMap.new "/static" => Rack::File.new("static"),
"/attachment" => Rack::File.new("entries"),
"/" => WebEnid.new("entries")
app = Rack::ShowExceptions.new app
app = Rack::ShowStatus.new app
Rack::Handler::Mongrel.run app, :Port => 9999
end