# vim:set syntax=ruby: require "msgpack" require 'digest/md5' require 'fileutils' require 'open-uri' class SearchIndex < ActiveRecord::Base end class Page < ActiveRecord::Base def blog str text = < ## #{self.blogtitle} EOS str.sub(/%blog%.*\n/, text)+"\n\n [back to index of texts](/text)" end def blogtitle if self.body.split("\n").first =~ /%blog%\ .*/ self.body.split("\n").first.sub(/%blog%\ /, '') else self.name.sub(/^.*?\//, '') end end def html request if self.body.split("\n").last == "subdomain only" a = self.body.split("\n") a.pop self.body = a.join("\n") end case self.body.split("\n").first when /^haml/ return self.haml request when /^coffee/ return self.coffee when /^ruby/ str = "" when /^title/ str = "" ary = self.body.split("\n") str = "" ary.shift str = ary.join("\n") else str = self.body end str = self.blog(str) if str =~ /%blog%/ Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, fenced_code_blocks: true, tables: true).render(str) end def title case self.body.split("\n").first when /title/ str = self.body.split("\n").shift.split("title\ ").last when /%blog%/ str = self.blogtitle when /haml/ str = self.body.split("\n").shift.split("haml\ ").last when /builder/ str = self.body.split("\n").shift.split("builder\ ").last when /coffee/ str = self.body.split("\n").shift.split("coffee\ ").last when /ruby/ str = self.body.split("\n").shift.split("ruby\ ").last else str = self.name end str.chomp! str = self.name if str == nil or str == "" or str == "haml" str.gsub(/=created_at=/, self.created_at.getutc.to_s) end def raw ary = self.body.split("\n") ary.pop if ary.last == 'subdomain only' ary.shift if ary.first =~ /^mime/ mime = ary.shift.split(" ").last else mime = "text" end return ary.join("\n"), mime end def haml request ary = self.body.split("\n") str = "" ary.shift str = ary.join("\n") Haml::Engine.new(str).render end def ruby request, env ary = self.body.split("\n") str = "" ary.shift if ary.first =~ /^mime/ mime = ary.shift.split(" ").last else mime = :html end str = ary.join("\n") return [eval(str), mime] end def builder request ary = self.body.split("\n") str = "" ary.shift str = ary.join("\n") xml = ::Builder::XmlMarkup.new(:indent => 2) eval str end def coffee ary = self.body.split("\n") str = "" ary.shift str = ary.join("\n") CoffeeScript.compile(str) end def layout ary = self.body.split("\n") ary.shift ary.join("\n") end end class Store < ActiveRecord::Base def value MessagePack.unpack(self.body) rescue nil end def value= hash self.body = hash.to_msgpack end end def set_prefix unless REDIS.get("ssig33comprefix") REDIS.set("ssig33comprefix", rand(256**16).to_s(16)) end end def set_cache url, body, *time set_prefix key = REDIS.get("ssig33comprefix")+Digest::MD5.hexdigest(url) REDIS.set(key, body) end def get_cache url set_prefix key = REDIS.get("ssig33comprefix")+Digest::MD5.hexdigest(url) REDIS.get(key) end def page id, request, env file = open("public/#{id}/index.html").read rescue nil return file if file @page = Page.where(:name => id).order("created_at desc").first @page = Page.where(name: CGI.unescape(id)).order("created_at desc").first unless @page if request.query_string q = CGI.parse(request.query_string) if q['history_id'] and q['history_id'].first != nil and q['history_id'].first != '' @page = Page.find(q['history_id'].first) end end redirect "/edit/#{id}" unless @page return [@page.haml(request), :html] if @page.body.split("\n").first =~ /^haml/ case @page.body.split("\n").first when /^raw/ , /^layout/ raw, mime = @page.raw return [raw, :"#{mime}"] when /^coffee/ return [@page.coffee, :js] when /^html/ return [@page.raw, :html] when /^builder/ return [@page.builder(request), :xml] when /^ruby/ return @page.ruby(request, env) else if layout = Page.where(:name => "layout").order("created_at desc").first return [haml(layout.layout), :html] else return [haml(:page), :html] end end end configure do set :logging, false set :app_file, __FILE__ use Rack::Session::Cookie, :secret => 'fsdjkfhsjkhr23f8fhsdjkvhnsdjhrfuiscflaaadn8or' end get '/favicon.ico' do end get '/' do id = 'index' id.chop! if id.reverse[0] == "/" body, type = page id, request, env content_type type body end get %r{/.*favicon.ico} do content_type :png open("public/favicon.ico").read end get %r{/edit/(.*)} do @id = params[:captures].first.to_s @page = Page.where(:name => @id).order("created_at desc").first @page = Page.new if @page == nil ary = Page.where(:name => "source/edit.haml").order("created_at desc").first.body.split("\n") ary.shift status 404 haml ary.join("\n") end get %r{/(.*)} do id = params[:captures].first.to_s id.chop! if id.reverse[0] == "/" body, type = page id, request, env content_type type body end post '/update' do if Digest::MD5.hexdigest(params[:password]).to_s != PASSWORD return "ERROR" else page = Page.new page.name = params[:id] page.body = params[:body] page.save s = SearchIndex.where(page_name: page.name).first_or_initialize s.body = page.body s.page_id = page.id s.save redirect "/#{params[:id]}" end end post '/destroy' do raise if Digest::MD5.hexdigest(params[:password]).to_s != PASSWORD Page.where(:name => params[:id]).delete_all redirect "/" end post '/usr2' do raise if Digest::MD5.hexdigest(params[:password]).to_s != PASSWORD system "rm site.pid.oldbin" system "kill -QUIT `cat site.pid`" redirect "/" end post %r{/(.*)} do id = params[:captures].first.to_s id.chop! if id.reverse[0] == "/" page id, request, env end error do "error... #{env['sinatra.error']}" end helpers do def h str CGI.escapeHTML str.to_s rescue "" end def title if request.path_info == "/" or request.path_info == "/index" return "ssig33.com" else return "ssig33.com - #{@page.title}" end end end