From 7e27259d5db7349e0e29762244298c986146ca37 Mon Sep 17 00:00:00 2001 From: mguetlein Date: Fri, 7 Dec 2012 16:59:56 +0100 Subject: extend html support get request in service root now returns uri-list accept header for html requests is fixed in before-method fix link parsing in to_html method add png image to to_html method --- lib/4store.rb | 9 ++++----- lib/utils/html.rb | 9 ++++++--- lib/utils/shims/feature.rb | 2 +- 3 files changed, 11 insertions(+), 9 deletions(-) (limited to 'lib') diff --git a/lib/4store.rb b/lib/4store.rb index 3ed081d..5a2714b 100644 --- a/lib/4store.rb +++ b/lib/4store.rb @@ -6,9 +6,8 @@ module OpenTox @@content_type_formats = [ "application/rdf+xml", "text/turtle", "text/plain" ] def self.list mime_type - mime_type = "text/html" if mime_type.match(%r{\*/\*}) bad_request_error "'#{mime_type}' is not a supported mime type. Please specify one of #{@@accept_formats.join(", ")} in the Accept Header." unless @@accept_formats.include? mime_type - if mime_type =~ /uri-list/ + if mime_type =~ /(uri-list|html)/ sparql = "SELECT DISTINCT ?g WHERE {GRAPH ?g {?s <#{RDF.type}> <#{klass}>; ?p ?o. } }" else sparql = "CONSTRUCT {?s ?p ?o.} WHERE {?s <#{RDF.type}> <#{klass}>; ?p ?o. }" @@ -17,7 +16,6 @@ module OpenTox end def self.get uri, mime_type - mime_type = "text/html" if mime_type.match(%r{\*/\*}) bad_request_error "'#{mime_type}' is not a supported mime type. Please specify one of #{@@accept_formats.join(", ")} in the Accept Header." unless @@accept_formats.include? mime_type sparql = "CONSTRUCT {?s ?p ?o.} FROM <#{uri}> WHERE { ?s ?p ?o. }" rdf = query sparql, mime_type @@ -61,8 +59,9 @@ module OpenTox case mime_type when 'application/sparql-results+xml' RestClient.get(sparql_uri, :params => { :query => sparql }, :accept => mime_type).body - when "text/uri-list" - RestClient.get(sparql_uri, :params => { :query => sparql }, :accept => "text/plain").body.gsub(/"|<|>/,'').split("\n").drop(1).join("\n") + when /(uri-list|html)/ + uri_list = RestClient.get(sparql_uri, :params => { :query => sparql }, :accept => "text/plain").body.gsub(/"|<|>/,'').split("\n").drop(1).join("\n") + uri_list = OpenTox.text_to_html(uri_list) if mime_type=~/html/ else bad_request_error "#{mime_type} is not a supported mime type for SELECT statements." end diff --git a/lib/utils/html.rb b/lib/utils/html.rb index 91dfc64..d74f357 100644 --- a/lib/utils/html.rb +++ b/lib/utils/html.rb @@ -7,15 +7,17 @@ * 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 - regex = Regexp.new '(https?:\/\/[\S]+)([>"])' - self.gsub( regex, '\1\2' ) + self.gsub(/(?i)http(s?):\/\/[^\r\n\s']*/, '\0') end + end module OpenTox @@ -29,7 +31,7 @@ module OpenTox # @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 ) + 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 @@ -41,6 +43,7 @@ module OpenTox html += "

Related links

"+related_links.link_urls+"

" if related_links html += "

Content

" if description || related_links html += "

" + html += "\n" if png_image html += text.link_urls html += "

" html diff --git a/lib/utils/shims/feature.rb b/lib/utils/shims/feature.rb index 297748b..f49bb39 100644 --- a/lib/utils/shims/feature.rb +++ b/lib/utils/shims/feature.rb @@ -30,7 +30,7 @@ module OpenTox metadata[RDF.type] = [] unless metadata[RDF.type] metadata[RDF.type] << RDF::OT.Feature unless metadata[RDF.type].include?(RDF::OT.Feature) metadata[RDF::DC.title] = title unless (metadata[RDF::DC.title]) - feature = feature_new = OpenTox::Feature.new File.join($feature[:uri], SecureRandom.uuid), @subjectid + feature = feature_new = OpenTox::Feature.new(File.join($feature[:uri], SecureRandom.uuid), @subjectid) feature_new.metadata = metadata sparql = "SELECT DISTINCT ?feature WHERE { ?feature <#{RDF.type}> <#{RDF::OT['feature'.capitalize]}>. ?feature <#{RDF::DC.title}> '#{title.to_s}' }" feature_uris = OpenTox::Backend::FourStore.query(sparql,"text/uri-list").split("\n") -- cgit v1.2.3