module Charbase class App < Sinatra::Base set :conf, ENV['CHARBASE_CONFIG'] conf = YAML.load_file(settings.conf) set :port, conf["port"] || 4567 set :siteroot, conf["siteroot"] set :public_folder, File.expand_path(conf["public"],conf["siteroot"]) set :views, File.expand_path(conf["views"] ,conf[ "siteroot" ]) set :dsn, conf["dsn"] set :site, conf["site"] def initialize @site = settings.site parseDSN() super end def parseDSN() (driver,path) = settings.dsn.split(/:/,2) if driver == "yaml" @characters = YAML.load_file(path.sub(/%siteroot%/,settings.siteroot)) elsif driver == "json" end end get "/" do char = {} char["characters"] = @characters["characters"].select { |c| c["hidden"] != true } liquid :index, :locals => {:s => @site, :c => char, :year => DateTime.now.strftime("%Y")} end get "/assets/css/characters.css" do char = {} char["characters"] = @characters["characters"].select { |c| c["hidden"] != true } content_type "text/css" liquid :charcss, :locals => {:s => @site, :c => char} end get "/characters.*" do |ext| char = {} char["characters"] = @characters["characters"].select { |c| c["hidden"] != true } status 200 if ext == "yml" content_type "text/plain" body YAML.dump(char) elsif ext == "json" content_type "application/json" body char.to_json elsif ext == "xml" content_type "text/xml" liquid :charactersxml, :locals => char end end get "/character/*.*" do |nick, ext| c = @characters["characters"].select { |n| n["nick"] == nick }.first status 200 if ext == "yml" content_type "text/plain" body YAML.dump(c) elsif ext == "json" content_type "application/json" body c.to_json elsif ext == html liquid :character, :locals => {:character => c} end end get "/character/:nick" do |nick| char = @characters["characters"].select { |n| n["nick"] == nick }.first # this is a nasty hack, eventually i should find a way to add a filter # to liquid to do this. for x in [ "description","appearance","personality","attire","skills","magic" ].push(*char["overrides"]["paragraph"]) if char[x] != nil and char[x].include?("\n\n") char[x].gsub!(/\n\n/,"

") end end # end hack liquid :character, :locals => {:site => @site, :character => char, :year => DateTime.now.strftime("%Y")} end get "/assets/css/:nick.css" do |nick| char = @characters["characters"].select { |n| n["nick"] == nick }.first content_type "text/css" liquid :usercss, :locals => {:site => @site, :css => char["overrides"]["css"], :nick => char["nick"]} end end end