13340: Correctly show all locator links on a text.
[arvados.git] / apps / workbench / app / views / application / _show_text_with_locators.html.erb
index f25bd2b4c91fadd92eafbbb2cea0d68203c362f3..b34b4cac8fd4862146e9f0a89b268c5b16ff8f5d 100644 (file)
@@ -1,16 +1,44 @@
+<%# Copyright (C) The Arvados Authors. All rights reserved.
+
+SPDX-License-Identifier: AGPL-3.0 %>
+
 <%# The locators in the given text are expected to be of the form JSON_KEEP_LOCATOR_REGEXP %>
 
 <% data_height = data_height || 100 %>
   <div style="max-height:<%=data_height%>px; overflow:auto;">
-    <% text_data.each_line do |l| %>
-      <% text_part = l %>
-      <% match = keep_locator_in_json l %>
-      <% if match %>
-        <% text_part = match[1] %>
-        <% rindex = match[2].rindex('"'); match2 = match[2][0..rindex-1] %>
-        <% pdh_readable = object_readable(match2) %>
-        <% quote_char = '"' %>
+    <% text_data.each_line do |line| %>
+      <% matches = keep_locator_in_json line %>
+
+      <% if matches.nil? or matches.empty? %>
+        <span style="white-space: pre-wrap; margin: none;"><%= line %></span>
+      <% else
+        subs = []
+        matches.uniq.each do |loc|
+          pdh, filename = loc.split('/', 2)
+
+          if object_readable(pdh)
+            # Add PDH link
+            replacement = link_to_arvados_object_if_readable(pdh, pdh, friendly_name: true)
+            if filename
+              link_params = {controller: 'collections', action: 'show_file', uuid: pdh, file: filename}
+              if preview_allowed_for(filename)
+                params = {disposition: 'inline'}
+              else
+                params = {disposition: 'attachment'}
+              end
+              file_link = link_to(raw("/"+filename), link_params.merge(params))
+              # Add file link
+              replacement << file_link
+            end
+            # Add link(s) substitution
+            subs << [loc, replacement]
+          end
+        end
+        # Replace all readable locators with links
+        subs.each do |loc, link|
+          line.gsub!(loc, link)
+        end %>
+        <span style="white-space: pre-wrap; margin: none;"><%= raw line %></span>
       <% end %>
-      <span style="white-space: pre-wrap; margin: none;"><%= text_part %><% if match %><% if pdh_readable then %><%= link_to_arvados_object_if_readable(match[3], match[3], friendly_name: true) %><%= link_to_arvados_object_if_readable(match2, match2, link_text: match[4]) %><% else %><%= match2%><% end %><%=quote_char+match[5]%><br/><% end %></span>
     <% end %>
   </div>