summaryrefslogtreecommitdiff
path: root/lib/to-html.rb
blob: 1bc14965fef2b01c4b1b1db2f506e9de145d1242 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

OT_LOGO = "http://opentox.informatik.uni-freiburg.de/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:\/\/[^\r\n\s']*/, '<a href=\0>\0</a>')
  end
end

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
  # @return [String] html page
  def self.text_to_html( text, related_links=nil, description=nil, post_params=nil )
    
    title = $sinatra.url_for($sinatra.request.env['PATH_INFO'], :full) if $sinatra
    
    html = <<EOF
<html>
EOF
    html.chomp!
    html += "<title>"+title+"</title>" if title
    html += <<EOF 
<img src="
EOF
    html.chomp!
    html += OT_LOGO
    html += <<EOF 
">
<body>
EOF
   html.chomp!
   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
   end
   html += "<h3>Content</h3>" if description || related_links
   html += <<EOF
<pre>
<p style="padding:15px; border:10px solid #5D308A">
EOF
   html.chomp!
   html += text.link_urls
   html += <<EOF
</p>
</pre>
</body>
<html>
EOF
    html
  end
  
end

#puts OpenTox.text_to_html("bla")