summaryrefslogtreecommitdiff
path: root/lib/utils/html.rb
blob: d1fb3239b9de276d1dcdaf362267af75a1fefd48 (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
#OT_LOGO = File.join(CONFIG[:services]["opentox-validation"],"resources/ot-logo.png")

=begin
* Name: html.rb
* Description: Tools to provide html output
* Author: Andreas Maunz <andreas@maunz.de>
* Date: 10/2012
=end

require "base64"

# AM: needed since this gem has a nested directory structure

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>')
  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
  #
  # @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_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_command=nil, png_image=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+"\"><\/img><body>"
      
    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
    html += "<h3>Content</h3>" if description || related_links
    html += "<pre><p style=\"padding:15px; border:10px solid \#B9DCFF\">"
    html += "<img src=\"data:image/png;base64,#{Base64.encode64(png_image)}\">\n" if png_image
    html += text.link_urls
    html += "</p></pre></body></html>"
    html
  end
  
end