Skip to content

Commit

Permalink
Merge dfa2274 into 2ec3911
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoviola committed Dec 21, 2019
2 parents 2ec3911 + dfa2274 commit 27d86ca
Show file tree
Hide file tree
Showing 5 changed files with 432 additions and 14 deletions.
192 changes: 181 additions & 11 deletions app/controllers/export_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class ExportController < ApplicationController
# no layout if xhr request
layout Proc.new { |controller| controller.request.xhr? ? false : nil } #, :only => [:update, :update_profile]

README = []

def index
@collection = Collection.friendly.find(params[:collection_id])
#check if there are any translated works in the collection
Expand All @@ -25,6 +27,21 @@ def show
render :layout => false
end

def text
@work = Work.includes(pages: [:notes, {page_versions: :user}]).find_by(id: params[:work_id])
render :layout => false
end

def transcript
@work = Work.includes(pages: [:notes, {page_versions: :user}]).find_by(id: params[:work_id])
render :layout => false
end

def translation
@work = Work.includes(pages: [:notes, {page_versions: :user}]).find_by(id: params[:work_id])
render :layout => false
end

def tei
params[:format] = 'xml'# if params[:format].blank?

Expand Down Expand Up @@ -77,27 +94,180 @@ def table_csv

end

def desc_to_file(text:, dirname:, out:)
README << text

path = File.join dirname, 'README.txt'
out.put_next_entry path

README.each do |desc|
out.write desc
end
end

def export_plaintext_transcript(name:, dirname:, out:)
path = File.join dirname, 'plaintext', "#{name}_transcript.txt"

out.put_next_entry path

case name
when "verbatim"
out.write @work.verbatim_transcription_plaintext
desc_to_file(text: "plaintext/verbatim_transcript.txt - file containing the per-document verbatim plaintext export of the transcript.\n", dirname: dirname, out: out)
when "emended"
out.write @work.emended_transcription_plaintext
desc_to_file(text: "plaintext/emended_transcript.txt - file containing the per-document emended plaintext export of the transcript.\n", dirname: dirname, out: out)
when "searchable"
out.write @work.searchable_plaintext
desc_to_file(text: "plaintext/searchable_transcript.txt - file containing the per-document searchable plaintext export of the transcript.\n", dirname: dirname, out: out)
end
end

def export_plaintext_translation(name:, dirname:, out:)
path = File.join dirname, 'plaintext', "#{name}_translation.txt"

out.put_next_entry path

out.write @work.public_send "#{name}_translation_plaintext"
desc_to_file(text: "plaintext/#{name}_translation.txt - file containing the per-document #{name} plaintext export of the translation if it is present.\n", dirname: dirname, out: out)
end

def export_plaintext_transcript_pages(name:, dirname:, out:, page:)
path = File.join dirname, 'plaintext', "#{name}_transcript_pages", "#{page.title}.txt"

out.put_next_entry path

out.write page.public_send "#{name}_transcription_plaintext"
desc_to_file(text: "plaintext/#{name}_transcript_pages/#{page.title}.txt - files containing per-page #{name} plaintext export.\n", dirname: dirname, out: out)
end

def export_plaintext_translation_pages(name:, dirname:, out:, page:)
path = File.join dirname, 'plaintext', "#{name}_translation_pages", "#{page.title}.txt"

out.put_next_entry path

out.write page.public_send "#{name}_translation_plaintext"
desc_to_file(text: "plaintext/#{name}_translation_pages/#{page.title}.txt - files containing per-page #{name} plaintext export.\n", dirname: dirname, out: out)
end

def export_view(name:, dirname:, out:)
path = File.join dirname, 'html', "#{name}.html"
out.put_next_entry path

case name
when "full"
full_view = render_to_string(:action => 'show', :formats => [:html], :work_id => @work.id, :layout => false, :encoding => 'utf-8')
out.write full_view
when "text"
text_view = render_to_string(:action => 'text', :formats => [:html], :work_id => @work.id, :layout => false, :encoding => 'utf-8')
out.write text_view
when "transcript"
transcript_view = render_to_string(:action => 'transcript', :formats => [:html], :work_id => @work.id, :layout => false, :encoding => 'utf-8')
out.write transcript_view
when "translation"
translation_view = render_to_string(:action => 'translation', :formats => [:html], :work_id => @work.id, :layout => false, :encoding => 'utf-8')
out.write translation_view
end
end

def export_html_full_pages(dirname:, out:, page:)
path = File.join dirname, 'html', 'full_pages', "#{page.title}.html"

out.put_next_entry path

page_view = render_to_string('display/display_page.html.slim', :locals => {:@work => @work, :@page => page}, :layout => false)
out.write page_view
end

def export_work
dirname = @work.slug.truncate(200, omission: "")

respond_to do |format|
format.zip do
buffer = Zip::OutputStream.write_buffer do |out|
%w(verbatim emended searchable).each do |format|
export_plaintext_transcript(name: format, dirname: dirname, out: out)
end

%w(verbatim emended).each do |format|
export_plaintext_translation(name: format, dirname: dirname, out: out)
end

@work.pages.each do |page|
%w(verbatim emended).each do |format|
export_plaintext_transcript_pages(name: format, dirname: dirname, out: out, page: page)
end

%w(verbatim emended).each do |format|
export_plaintext_translation_pages(name: format, dirname: dirname, out: out, page: page)
end
end

%w(full text transcript translation).each do |format|
export_view(name: format, dirname: dirname, out: out)
end

@work.pages.each do |page|
export_html_full_pages(dirname: dirname, out: out, page: page)
end
end

buffer.rewind
send_data buffer.read, filename: "#{@collection.title}-#{@work.title}.zip"
end
end
end

def export_all_works
unless @collection.subjects_disabled
@works = Work.includes(pages: [:notes, {page_versions: :user}]).where(collection_id: @collection.id)
else
@works = Work.includes(pages: [:notes, {page_versions: :user}]).where(collection_id: @collection.id)
end
end

#create a zip file which is automatically downloaded to the user's machine
# create a zip file which is automatically downloaded to the user's machine
respond_to do |format|
format.html
format.zip do
compressed_filestream = Zip::OutputStream.write_buffer do |zos|
@works.each do |work|
@work = work
export_view = render_to_string(:action => 'show', :formats => [:html], :work_id => work.id, :layout => false, :encoding => 'utf-8')
zos.put_next_entry "#{work.slug.truncate(200, omission: "")}.xhtml"
zos.print export_view
buffer = Zip::OutputStream.write_buffer do |out|
@works.each do |work|
@work = work
dirname = work.slug.truncate(200, omission: "")

export_view = render_to_string(:action => 'show', :formats => [:html], :work_id => work.id, :layout => false, :encoding => 'utf-8')
out.put_next_entry "#{dirname}/#{work.slug.truncate(200, omission: "")}.xhtml"
out.print export_view

%w(verbatim emended searchable).each do |format|
export_plaintext_transcript(name: format, dirname: dirname, out: out)
end

%w(verbatim emended).each do |format|
export_plaintext_translation(name: format, dirname: dirname, out: out)
end

@work.pages.each do |page|
%w(verbatim emended).each do |format|
export_plaintext_transcript_pages(name: format, dirname: dirname, out: out, page: page)
end

%w(verbatim emended).each do |format|
export_plaintext_translation_pages(name: format, dirname: dirname, out: out, page: page)
end
end

%w(full text transcript translation).each do |format|
export_view(name: format, dirname: dirname, out: out)
end

@work.pages.each do |page|
export_html_full_pages(dirname: dirname, out: out, page: page)
end
end
end
end
compressed_filestream.rewind
send_data compressed_filestream.read, filename: "#{@collection.title}.zip"

buffer.rewind
send_data buffer.read, filename: "#{@collection.title}.zip"
end
end
cookies['download_finished'] = 'true'
Expand Down
10 changes: 7 additions & 3 deletions app/views/export/index.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ table.datagrid
th #{@header}
th Review
th.w100 Progress
th(colspan="4") Export As
th(colspan="6") Export As

-for work in @works
-work_stats(work)
Expand Down Expand Up @@ -69,11 +69,15 @@ table.datagrid
=link_to iiif_manifest_path(work.id)
=svg_symbol '#icon-export', class: 'icon'
span IIIF
td.nowrap
-unless work.table_cells.blank?
-unless work.table_cells.blank?
td.nowrap
=link_to({ :controller => 'export', :action => 'table_csv', :work_id => work.id }, class: 'btnCsvTblExport btnExport')
=svg_symbol '#icon-export', class: 'icon'
span Table CSV
td.nowrap
=link_to(:controller => 'export', :action => 'export_work', :work_id => work.id, format: :zip)
=svg_symbol '#icon-export', class: 'icon'
span ZIP

=render(:partial => 'shared/pagination', :locals => { :model => @works, :entries_info => true })

Expand Down
120 changes: 120 additions & 0 deletions app/views/export/text.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> <%= @work.title %></title>
</head>

<body>
<h1 class="work-title"><%= @work.title %></h1>
<div class="export-metadata">FromThePage export of <%= @work.title %> from <%= @collection.title %> made on <%= Time.now %>.
<% unless @work.identifier.blank? %>
<p>Identifier: <%=@work.identifier%></p>
<% end %>
<p>
FromThePage version: <%= fromthepage_version %>
</p>
</div>

<hr />
<h2 class="divider">Page Transcripts</h2>

<div class="pages">
<% @work.pages.each do |page| %>
<div id="page-<%=page.id%>">
<h3><%= page.title %></h3>
<div class="page-content">
<%= raw(xml_to_html(page.xml_text, true, true)) %>
</div>
<% if page.notes.present? %>
<div class="page-notes">
<strong>Notes:</strong>
<% page.notes.each do |note| %>
<div>
<p><span class="page-note-username"><%=note.user.display_name%></span></p>
<p class="page-note-content"><%= sanitize(note.body, tags: %w(strong b em i a), attributes: %w(href))%></p>

</div>
<% end %>
</div>
<% end %>
</div>
<br />
<% end %>
</div>

<% if @work.supports_translation %>
<hr />
<h2 class="divider">Page Translations</h2>

<div class="pages">
<% @work.pages.each do |page| %>
<div id="translation-<%=page.id%>">
<h3><%= page.title %></h3>
<div class="page-content">
<%= raw(xml_to_html(page.xml_translation, true, true)) %>
</div>
</div>
<br />
<% if page.notes.present? %>
<div class="page-notes">
<strong>Notes:</strong>
<% page.notes.each do |note| %>
<div>
<p><span class="page-note-username"><%=note.user.display_name%></span></p>
<p class="page-note-content"><%= sanitize(note.body, tags: %w(strong b em i a), attributes: %w(href))%></p>

</div>
<% end %>
</div>
<% end %>
<% end %>
</div>
<% end %>

<hr />
<% unless @work.collection.subjects_disabled %>
<h2 class="divider">Subjects and Indices</h2>

<div class="subjects">
<% @work.articles.sort{ |x,y| x.title.upcase <=> y.title.upcase }.each do |article| %>
<div id="article-<%=article.id%>">
<h3 class="article-title"><%=article.title %></h3>
<div class="article-content">
<%= raw(xml_to_html(article.xml_text, true, true)) %>
</div>
<% if article.categories.present? %>
<div class="article-categories">
<strong>Categories:</strong>
<ul>
<% article.categories.each do |cat| %>
<li><small><%= raw((cat.ancestors.reverse.push(cat).map { |c| c.title }).join(" &rarr; ")) %></small></li>
<% end %>
</ul>
</div>
<% end %>
<div class="article-indices">
<strong>Pages:</strong>
<ul>
<% article.show_links(@collection).each do |link| %>
<li>
<small>
<% if link.text_type == 'translation' %>
<%= link_to(link.page.title, "#page-#{link.page_id}") %>
<% else %>
<%= link_to(link.page.title, "#translation-#{link.page_id}") %> (translation)
<% end %>
</small>
</li>
<% end %>
</ul>
</div>
<!-- categorization here -->
<!-- subject article edit history here -->
</div>
<br />
<% end %>
</div>
<% end %>
</body>
</html>
Loading

0 comments on commit 27d86ca

Please sign in to comment.