diff --git a/app/helpers/schema_org_helper.rb b/app/helpers/schema_org_helper.rb new file mode 100644 index 00000000..63798e2c --- /dev/null +++ b/app/helpers/schema_org_helper.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +# rubocop:disable Rails/OutputSafety +module SchemaOrgHelper + def keywords_helper(subjects) + keywords_json = subjects.map do |subject| + '"' + html_escape(subject) + '"' + end + + html = "[" + keywords_json.join(",") + "]" + html.html_safe + end + + def authors_helper(authors) + authors_json = authors.each.map do |author| + json_str = "\n\t\t\t{\n\t\t\t" + '"name": ' + '"' + author.value + '"' + if author.affiliation_name.present? + json_str += ",\n\t\t\t" + '"affiliation": ' + '"' + author.affiliation_name + '"' + end + if author.orcid.present? + json_str += ",\n\t\t\t" + '"identifier": ' + '"' + author.orcid + '"' + end + json_str += "\n\t\t\t}" + json_str + end + html = "[" + authors_json.join(",") + "]" + html.html_safe + end + + def license_helper(licenses) + if licenses.count == 0 + "" + else + html = '"license": {' + html += "\n\t\t\t" + '"@type": ' + '"Dataset"' + ",\n" \ + "\t\t\t" + '"text": ' + '"' + licenses[0]['identifier'] + '"' + ",\n" \ + "\t\t\t" + '"url": ' + '"' + licenses[0]['uri'] + '"' + html += "\n\t\t\t}," + html.html_safe + end + end +end +# rubocop:enable Rails/OutputSafety diff --git a/app/views/layouts/blacklight/base.html.erb b/app/views/layouts/blacklight/base.html.erb index 87c28bb8..a8ca5bce 100644 --- a/app/views/layouts/blacklight/base.html.erb +++ b/app/views/layouts/blacklight/base.html.erb @@ -33,6 +33,7 @@ More info: https://getbootstrap.com/docs/4.6/components/popovers/ --> + <%= render partial: 'shared/schema_org'%>