Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 28 additions & 12 deletions lib/ro_crate/ro-crate-preview.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
<%
def entity_to_html(entity)
if entity.is_a?(Array)
if entity.length == 1
entity_to_html(entity.first)
else
"<ul><li>#{entity.map { |e| entity_to_html(e) }.join('</li><li>')}</li></ul>"
end
elsif entity.is_a?(ROCrate::Entity)
label = entity['name'] || entity.id
if entity.external?
"<a href=\"#{entity.id}\" target=\"_blank\">#{label}</a>"
else
label
end
else
entity
end
end
%>
<!DOCTYPE html>
<html lang="en">
<head>
Expand All @@ -18,36 +38,32 @@
<dl>
<% if author %>
<dt>Author</dt>
<dd><%= author %></dd>
<dd><%= entity_to_html author %></dd>
<% end %>
<% if contact_point %>
<dt>Contact</dt>
<dd><%= contact_point %></dd>
<dd><%= entity_to_html contact_point %></dd>
<% end %>
<% if publisher %>
<dt>Publisher</dt>
<dd><%= publisher %></dd>
<dd><%= entity_to_html publisher %></dd>
<% end %>
<% if license %>
<dt>License</dt>
<dd><%= license %></dd>
<dd><%= entity_to_html license %></dd>
<% end %>
</dl>

<h2>Contents</h2>
<ul>
<% data_entities.each do |data_entity| %>
<li id="__data_entity_<%= data_entity.id.gsub(/\s/, '-') %>">
<% if data_entity.external? %>
<strong><a href="<%= data_entity.id %>" target="_blank"><%= data_entity.name || data_entity.id %></a></strong>
<% else %>
<strong><%= data_entity.name || data_entity.id %></strong>
<% end %>
<li>
<strong><%= entity_to_html data_entity %></strong>
<% if data_entity.content_size %>
<br/>Size: <%= data_entity.content_size %>
<br/>Size: <%= entity_to_html data_entity.content_size %>
<% end %>
<% if data_entity.encoding_format %>
<br/>Format: <%= data_entity.encoding_format %>
<br/>Format: <%= entity_to_html data_entity.encoding_format %>
<% end %>
</li>
<% end %>
Expand Down
48 changes: 48 additions & 0 deletions test/preview_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# encoding: utf-8
require 'test_helper'

class PreviewTest < Test::Unit::TestCase
test 'simple attributes' do
crate = ROCrate::Crate.new
crate.author = 'Finn'

html = crate.preview.source.read
assert_includes html, '<dd>Finn</dd>'
end

test 'list attributes' do
crate = ROCrate::Crate.new
crate.author = ['Finn', 'Josiah']

html = crate.preview.source.read
assert_includes html, '<dd><ul><li>Finn</li><li>Josiah</li></ul></dd>'
end

test 'entity attributes' do
crate = ROCrate::Crate.new
crate.author = crate.add_person('https://orcid.org/0000-0002-0048-3300', name: 'Finn')

html = crate.preview.source.read
assert_includes html, '<dd><a href="https://orcid.org/0000-0002-0048-3300" target="_blank">Finn</a></dd>'
end

test 'complex attributes' do
crate = ROCrate::Crate.new
crate.author = [crate.add_person('https://orcid.org/0000-0002-0048-3300', name: 'Finn'), 'Josiah']

html = crate.preview.source.read

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>'
end

test 'files' do
crate = ROCrate::Crate.new
crate.add_file(fixture_file('info.txt'))
crate.add_external_file('https://raw.githubusercontent.com/ResearchObject/ro-crate-ruby/master/README.md')

html = crate.preview.source.read

assert_includes html, '<strong>info.txt</strong>'
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>'
end
end