diff options
Diffstat (limited to 'lib/to-html.rb')
-rw-r--r-- | lib/to-html.rb | 107 |
1 files changed, 66 insertions, 41 deletions
diff --git a/lib/to-html.rb b/lib/to-html.rb index 6785974..2979062 100644 --- a/lib/to-html.rb +++ b/lib/to-html.rb @@ -1,12 +1,12 @@ -OT_LOGO = "http://opentox.informatik.uni-freiburg.de/ot-logo.png" +OT_LOGO = File.join(CONFIG[:services]["opentox-validation"],"resources/ot-logo.png") class String # encloses URI in text with with link tag # @return [String] new text with marked links def link_urls - self.gsub(/(?i)http(s?):\/\/[^\r\n\s']*/, '<a href=\0>\0</a>') + self.gsub(/(?i)http(s?):\/\/[^\r\n\s']*/, '<a href="\0">\0</a>') end end @@ -15,98 +15,123 @@ module OpenTox # produces a html page for making web services browser friendly # format of text (=string params) is preserved (e.g. line breaks) # urls are marked as links - # @example post params: - # [ [ [:mandatory_param_1], [:mandatory_param_2], [:optional_param,"default_value"] ], - # [ [:alteranative_mandatory_param_1], [:alteranative_mandatory_param_2] ] - # ] + # # @param [String] text this is the actual content, # @param [optional,String] related_links info on related resources # @param [optional,String] description general info - # @param [optional,Array] post_params, array of arrays containing info on POST operation, see example + # @param [optional,Array] post_command, infos for the post operation, object defined below # @return [String] html page - def self.text_to_html( text, subjectid=nil, related_links=nil, description=nil, post_params=nil ) + def self.text_to_html( text, subjectid=nil, related_links=nil, description=nil, post_command=nil ) # TODO add title as parameter title = nil #$sinatra.url_for($sinatra.request.env['PATH_INFO'], :full) if $sinatra html = "<html>" html += "<title>"+title+"</title>" if title - html += "<img src="+OT_LOGO+"><body>" + html += "<img src=\""+OT_LOGO+"\"><\/img><body>" if AA_SERVER user = OpenTox::Authorization.get_user(subjectid) if subjectid html += "<pre><p align=\"right\">" unless user - html += "You are currently not logged in to "+$url_provider.url_for("",:full)+ - ", <a href="+$url_provider.url_for("/login",:full)+">login</a>" + html += "You are currently not signed in to "+$url_provider.url_for("",:full)+ + ", <a href="+$url_provider.url_for("/sign_in",:full)+">sign in</a>" else - html += "You are logged in as '#{user}' to "+$url_provider.url_for("",:full)+ - ", <a href="+$url_provider.url_for("/logout",:full)+">logout</a>" + html += "You are signed in as '#{user}' to "+$url_provider.url_for("",:full)+ + ", <a href="+$url_provider.url_for("/sign_out",:full)+">sign out</a>" end html += " </p></pre>" end html += "<h3>Description</h3><pre><p>"+description.link_urls+"</p></pre>" if description html += "<h3>Related links</h3><pre><p>"+related_links.link_urls+"</p></pre>" if related_links - if post_params - html += "<h3>POST parameters</h3>" - count = 0 - post_params.each do |p| - html += "<pre><p>alternatively:</p></pre>" if count > 0 - html += "<pre><p><table><thead><tr><th>param</th><th>default_value</th></tr></thead>" - p.each do |k,v| - html += "<tr><th>"+k.to_s+"</th><th>"+(v!=nil ? v.to_s : "<i>mandatory</i>")+"</th></tr>" - end - html += "</table></p></pre>" - count += 1 - end + if post_command + raise "not a post command" unless post_command.is_a?(OpenTox::PostCommand) + html += "<h3>POST command</h3>" + html += post_command.to_html end - html += "<h3>Content</h3>" if description || related_links + html += "<h3>Content</h3>" if description || related_links || post_command html += "<pre><p style=\"padding:15px; border:10px solid \#5D308A\">" html += text.link_urls - html += "</p></pre></body><html>" + html += "</p></pre></body></html>" html end - def self.login( msg=nil ) + def self.sign_in( msg=nil ) html = "<html><title>Login</title><img src="+OT_LOGO+"><body>" - html += "<form method='POST' action='"+$url_provider.url_for("/login",:full)+"'>" + html += "<form method='POST' action='"+$url_provider.url_for("/sign_in",:full)+"'>" html += "<pre><p style=\"padding:15px; border:10px solid \#5D308A\">" html += msg+"\n\n" if msg - html += "Please login to "+$url_provider.url_for("",:full)+"\n\n" + html += "Please sign in to "+$url_provider.url_for("",:full)+"\n\n" html += "<table border=0>" html += "<tr><td>user:</td><td><input type='text' name='user' size='15' /></td></tr>"+ "<tr><td>password:</td><td><input type='password' name='password' size='15' /></td></tr>"+ #"<input type=hidden name=back_to value="+back_to.to_s+">"+ - "<tr><td><input type='submit' value='Login' /></td></tr>" - html += "</table></p></pre></form></body><html>" + "<tr><td><input type='submit' value='Sign in' /></td></tr>" + html += "</table></p></pre></form></body></html>" html end + + class PostAttribute + attr_accessor :name, :is_mandatory, :default, :description + + def initialize(name, is_mandatory=true, default=nil, description=nil) + @name = name + @is_mandatory = is_mandatory + @default = default + @description = description + end + end + + class PostCommand + attr_accessor :attributes, :uri, :name + + def initialize( uri, name="Send" ) + @uri = uri + @name = name + @attributes = [] + end + + def to_html + html = "<form method='POST' action='"+@uri.to_s+"'>" + html << "<pre><p>" + html << "<table border=0>" + #html << "<tr><td colspan='3'><i><sup>Mandatory params are marked with *.</sup></i></td></tr>" + attributes.each do |a| + mandatory_string = a.is_mandatory ? "*" : "" + html << "<tr><td>"+a.name.to_s+":"+mandatory_string+"</td>" + html << "<td><input type='text' name='"+a.name.to_s+ + "' size='50' value='"+a.default.to_s+"'/></td>" + html << "<td><i><sup>"+a.description.to_s+"</sup></i></td></tr>" + end + html << "<tr><td colspan='3'><input type='submit' value='"+@name.to_s+"' /></td></tr>" + html << "</table></p></pre></form>" + html + end + end end -=begin -get '/logout/?' do +get '/sign_out/?' do response.set_cookie("subjectid",{:value=>nil}) content_type "text/html" - content = "Sucessfully logged out from "+$url_provider.url_for("",:full) + content = "Sucessfully signed out from "+$url_provider.url_for("",:full) OpenTox.text_to_html(content) end -get '/login/?' do +get '/sign_in/?' do content_type "text/html" - OpenTox.login + OpenTox.sign_in end -post '/login/?' do +post '/sign_in/?' do subjectid = OpenTox::Authorization.authenticate(params[:user], params[:password]) if (subjectid) response.set_cookie("subjectid",{:value=>subjectid}) content_type "text/html" - content = "Sucessfully logged in as '"+params[:user]+"' to "+$url_provider.url_for("",:full) + content = "Sucessfully signed in as '"+params[:user]+"' to "+$url_provider.url_for("",:full) OpenTox.text_to_html(content,subjectid) else content_type "text/html" - OpenTox.login("Login failed, please try again") + OpenTox.sign_in("Login failed, please try again") end end -=end |