#!ruby #!/usr/local/bin/ruby # Javascript Workspace Wiki Server # # Copyright (c) 2007 Takashi Yammaiya # # Permission is hereby granted, free of charge, to any person # obtaining a copy of this software and associated documentation files # (the "Software"), to deal in the Software without restriction, # including without limitation the rights to use, copy, modify, merge, # publish, distribute, sublicense, and/or sell copies of the Software, # and to permit persons to whom the Software is furnished to do so, # subject to the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. require "cgi" DATA_DIR = "jsdata" # data directory class Workspace def main @cgi = CGI.new if (!self.title) redirect return end if (@cgi.has_key?('text')) && @cgi['submit'] != "reload" save_text @cgi['text'] new_text = @cgi['text'] else new_text = load_text end print "Content-type: text/html\n\n" text = @cgi.has_key?('text') ? text : '' output = TEMPLATE.sub('{{TEXT}}', CGI.escapeHTML(new_text)); print output.gsub('{{TITLE}}', title_html) end def title return nil unless @cgi.path_info return nil unless @cgi.path_info.length > 1 return @cgi.path_info[1 .. -1] end def title_html return CGI.escapeHTML(title) end def file_name return DATA_DIR + '/' + CGI.escape(title) + '.txt' end # Redirect to Home if request path is empty. def redirect print @cgi.header({'status' => '302 Found', 'Location' => @cgi.script_name + "/Home" }) end def save_text string File.open(file_name, "w") {|f| f.print string } end def load_text begin File.open(file_name, "r") {|f| return f.read } rescue Errno::ENOENT return "" end end end TEMPLATE = < Javascript Workspace {{TITLE}}

{{TITLE}}

EOT Workspace.new.main