Skip to content

Commit c134952

Browse files
authored
Merge pull request #34 from ResearchObject/ro-crate-preview-improvements
Improve how entities rendered in previews.
2 parents df04950 + bbd558b commit c134952

2 files changed

Lines changed: 76 additions & 12 deletions

File tree

lib/ro_crate/ro-crate-preview.html.erb

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
<%
2+
def entity_to_html(entity)
3+
if entity.is_a?(Array)
4+
if entity.length == 1
5+
entity_to_html(entity.first)
6+
else
7+
"<ul><li>#{entity.map { |e| entity_to_html(e) }.join('</li><li>')}</li></ul>"
8+
end
9+
elsif entity.is_a?(ROCrate::Entity)
10+
label = entity['name'] || entity.id
11+
if entity.external?
12+
"<a href=\"#{entity.id}\" target=\"_blank\">#{label}</a>"
13+
else
14+
label
15+
end
16+
else
17+
entity
18+
end
19+
end
20+
%>
121
<!DOCTYPE html>
222
<html lang="en">
323
<head>
@@ -18,36 +38,32 @@
1838
<dl>
1939
<% if author %>
2040
<dt>Author</dt>
21-
<dd><%= author %></dd>
41+
<dd><%= entity_to_html author %></dd>
2242
<% end %>
2343
<% if contact_point %>
2444
<dt>Contact</dt>
25-
<dd><%= contact_point %></dd>
45+
<dd><%= entity_to_html contact_point %></dd>
2646
<% end %>
2747
<% if publisher %>
2848
<dt>Publisher</dt>
29-
<dd><%= publisher %></dd>
49+
<dd><%= entity_to_html publisher %></dd>
3050
<% end %>
3151
<% if license %>
3252
<dt>License</dt>
33-
<dd><%= license %></dd>
53+
<dd><%= entity_to_html license %></dd>
3454
<% end %>
3555
</dl>
3656

3757
<h2>Contents</h2>
3858
<ul>
3959
<% data_entities.each do |data_entity| %>
40-
<li id="__data_entity_<%= data_entity.id.gsub(/\s/, '-') %>">
41-
<% if data_entity.external? %>
42-
<strong><a href="<%= data_entity.id %>" target="_blank"><%= data_entity.name || data_entity.id %></a></strong>
43-
<% else %>
44-
<strong><%= data_entity.name || data_entity.id %></strong>
45-
<% end %>
60+
<li>
61+
<strong><%= entity_to_html data_entity %></strong>
4662
<% if data_entity.content_size %>
47-
<br/>Size: <%= data_entity.content_size %>
63+
<br/>Size: <%= entity_to_html data_entity.content_size %>
4864
<% end %>
4965
<% if data_entity.encoding_format %>
50-
<br/>Format: <%= data_entity.encoding_format %>
66+
<br/>Format: <%= entity_to_html data_entity.encoding_format %>
5167
<% end %>
5268
</li>
5369
<% end %>

test/preview_test.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# encoding: utf-8
2+
require 'test_helper'
3+
4+
class PreviewTest < Test::Unit::TestCase
5+
test 'simple attributes' do
6+
crate = ROCrate::Crate.new
7+
crate.author = 'Finn'
8+
9+
html = crate.preview.source.read
10+
assert_includes html, '<dd>Finn</dd>'
11+
end
12+
13+
test 'list attributes' do
14+
crate = ROCrate::Crate.new
15+
crate.author = ['Finn', 'Josiah']
16+
17+
html = crate.preview.source.read
18+
assert_includes html, '<dd><ul><li>Finn</li><li>Josiah</li></ul></dd>'
19+
end
20+
21+
test 'entity attributes' do
22+
crate = ROCrate::Crate.new
23+
crate.author = crate.add_person('https://orcid.org/0000-0002-0048-3300', name: 'Finn')
24+
25+
html = crate.preview.source.read
26+
assert_includes html, '<dd><a href="https://orcid.org/0000-0002-0048-3300" target="_blank">Finn</a></dd>'
27+
end
28+
29+
test 'complex attributes' do
30+
crate = ROCrate::Crate.new
31+
crate.author = [crate.add_person('https://orcid.org/0000-0002-0048-3300', name: 'Finn'), 'Josiah']
32+
33+
html = crate.preview.source.read
34+
35+
assert_includes html, '<dd><ul><li><a href="https://orcid.org/0000-0002-0048-3300" target="_blank">Finn</a></li><li>Josiah</li></ul></dd>'
36+
end
37+
38+
test 'files' do
39+
crate = ROCrate::Crate.new
40+
crate.add_file(fixture_file('info.txt'))
41+
crate.add_external_file('https://raw.githubusercontent.com/ResearchObject/ro-crate-ruby/master/README.md')
42+
43+
html = crate.preview.source.read
44+
45+
assert_includes html, '<strong>info.txt</strong>'
46+
assert_includes html, '<strong><a href="https://raw.githubusercontent.com/ResearchObject/ro-crate-ruby/master/README.md" target="_blank">https://raw.githubusercontent.com/ResearchObject/ro-crate-ruby/master/README.md</a></strong>'
47+
end
48+
end

0 commit comments

Comments
 (0)