diff options
Diffstat (limited to 'lib/attachment_to_html')
-rw-r--r-- | lib/attachment_to_html/adapter.rb | 67 | ||||
-rw-r--r-- | lib/attachment_to_html/adapters/could_not_convert.rb | 44 | ||||
-rw-r--r-- | lib/attachment_to_html/adapters/google_docs_viewer.rb | 39 | ||||
-rw-r--r-- | lib/attachment_to_html/adapters/pdf.rb | 51 | ||||
-rw-r--r-- | lib/attachment_to_html/adapters/rtf.rb | 51 | ||||
-rw-r--r-- | lib/attachment_to_html/adapters/text.rb | 42 | ||||
-rw-r--r-- | lib/attachment_to_html/attachment_to_html.rb | 3 | ||||
-rw-r--r-- | lib/attachment_to_html/view.rb | 1 |
8 files changed, 95 insertions, 203 deletions
diff --git a/lib/attachment_to_html/adapter.rb b/lib/attachment_to_html/adapter.rb new file mode 100644 index 000000000..ac8a16411 --- /dev/null +++ b/lib/attachment_to_html/adapter.rb @@ -0,0 +1,67 @@ +module AttachmentToHTML + class Adapter + attr_reader :attachment + + # Public: Initialize a converter + # + # attachment - the FoiAttachment to convert to HTML + # opts - a Hash of options (default: {}): + # No options currently accepted + def initialize(attachment, opts = {}) + @attachment = attachment + end + + # Public: The title to use in the <title> tag + # + # Returns a String + def title + @title ||= attachment.display_filename + end + + # Public: The contents of the extracted html <body> tag + # + # Returns a String + def body + @body ||= parse_body + end + + def parse_body + convert + end + + # Public: Was the document conversion successful? + # + # Returns true + def success? + true + end + + def has_content? + !body.gsub(/\s+/,"").gsub(/\<[^\>]*\>/, "").empty? + end + + def contains_images? + body.match(/<img[^>]*>/mi) + end + + def create_tempfile(text) + tempfile = if RUBY_VERSION.to_f >= 1.9 + Tempfile.new('foiextract', '.', :encoding => text.encoding) + else + Tempfile.new('foiextract', '.') + end + tempfile.print(text) + tempfile.flush + tempfile + end + + def cleanup_tempfile(tempfile) + tempfile.close + tempfile.delete + end + + def attachment_body + @attachment_body ||= attachment.default_body + end + end +end diff --git a/lib/attachment_to_html/adapters/could_not_convert.rb b/lib/attachment_to_html/adapters/could_not_convert.rb index 8e4bf39dc..745a54114 100644 --- a/lib/attachment_to_html/adapters/could_not_convert.rb +++ b/lib/attachment_to_html/adapters/could_not_convert.rb @@ -1,49 +1,15 @@ +# -*- encoding : utf-8 -*- module AttachmentToHTML module Adapters - class CouldNotConvert - - attr_reader :attachment - - # Public: Initialize a PDF converter - # - # attachment - the FoiAttachment to convert to HTML - # opts - a Hash of options (default: {}): - # No options currently accepted - def initialize(attachment, opts = {}) - @attachment = attachment - end - - # Public: The title to use in the <title> tag - # - # Returns a String - def title - @title ||= attachment.display_filename - end - - # Public: The contents of the extracted html <body> tag - # - # Returns a String - def body - @body ||= parse_body - end - - - # Public: Was the document conversion successful? - # As this is a fallback option and not doing anything dynamic - # we're assuming this is successful whatever the case - # - # Returns true - def success? - true - end - + # As this is a fallback option and not doing anything dynamic + # we're assuming this is successful whatever the case + class CouldNotConvert < Adapter private def parse_body "<p>Sorry, we were unable to convert this file to HTML. " \ "Please use the download link at the top right.</p>" end - end end -end
\ No newline at end of file +end diff --git a/lib/attachment_to_html/adapters/google_docs_viewer.rb b/lib/attachment_to_html/adapters/google_docs_viewer.rb index 991fbb757..0817d08fd 100644 --- a/lib/attachment_to_html/adapters/google_docs_viewer.rb +++ b/lib/attachment_to_html/adapters/google_docs_viewer.rb @@ -1,9 +1,14 @@ +# -*- encoding : utf-8 -*- module AttachmentToHTML module Adapters # Renders the attachment in a Google Docs Viewer - class GoogleDocsViewer - - attr_reader :attachment, :attachment_url + # + # We can't really tell whether the document conversion has been + # successful as such; We're assuming that given a correctly + # constructed iframe (which is tested) that Google will make this + # Just Work. + class GoogleDocsViewer < Adapter + attr_reader :attachment_url # Public: Initialize a GoogleDocsViewer converter # @@ -12,35 +17,10 @@ module AttachmentToHTML # :attachment_url - a String url to the attachment for # Google to render (default: nil) def initialize(attachment, opts = {}) - @attachment = attachment + super @attachment_url = opts.fetch(:attachment_url, nil) end - # Public: The title to use in the <title> tag - # - # Returns a String - def title - @title ||= attachment.display_filename - end - - # Public: The contents of the extracted html <body> tag - # - # Returns a String - def body - @body ||= parse_body - end - - # Public: Was the document conversion successful? - # We can't really tell whether the document conversion has been - # successful as such; We're assuming that given a correctly - # constructed iframe (which is tested) that Google will make this - # Just Work. - # - # Returns true - def success? - true - end - private def parse_body @@ -50,7 +30,6 @@ module AttachmentToHTML def protocol AlaveteliConfiguration.force_ssl ? 'https' : 'http' end - end end end diff --git a/lib/attachment_to_html/adapters/pdf.rb b/lib/attachment_to_html/adapters/pdf.rb index a010b0342..afc8fbcb0 100644 --- a/lib/attachment_to_html/adapters/pdf.rb +++ b/lib/attachment_to_html/adapters/pdf.rb @@ -1,10 +1,11 @@ +# -*- encoding : utf-8 -*- module AttachmentToHTML module Adapters # Convert application/pdf documents in to HTML - class PDF + class PDF < Adapter TOO_MANY_IMAGES = 51 - attr_reader :attachment, :tmpdir + attr_reader :tmpdir # Public: Initialize a PDF converter # @@ -13,24 +14,10 @@ module AttachmentToHTML # :tmpdir - String name of directory to store the # converted document def initialize(attachment, opts = {}) - @attachment = attachment + super @tmpdir = opts.fetch(:tmpdir, ::Rails.root.join('tmp')) end - # Public: The title to use in the <title> tag - # - # Returns a String - def title - @title ||= attachment.display_filename - end - - # Public: The contents of the extracted html <body> tag - # - # Returns a String - def body - @body ||= parse_body - end - # Public: Was the document conversion successful? # # Returns a Boolean @@ -47,14 +34,6 @@ module AttachmentToHTML match ? match[1] : '' end - def has_content? - !body.gsub(/\s+/,"").gsub(/\<[^\>]*\>/, "").empty? - end - - def contains_images? - body.match(/<img[^>]*>/mi) ? true : false - end - # Works around https://bugs.freedesktop.org/show_bug.cgi?id=77932 in pdftohtml def contains_too_many_images? number_of_images_in_body >= TOO_MANY_IMAGES @@ -81,28 +60,6 @@ module AttachmentToHTML html end end - - def create_tempfile(text) - tempfile = if RUBY_VERSION.to_f >= 1.9 - Tempfile.new('foiextract', '.', - :encoding => text.encoding) - else - Tempfile.new('foiextract', '.') - end - tempfile.print(text) - tempfile.flush - tempfile - end - - def cleanup_tempfile(tempfile) - tempfile.close - tempfile.delete - end - - def attachment_body - @attachment_body ||= attachment.body - end - end end end diff --git a/lib/attachment_to_html/adapters/rtf.rb b/lib/attachment_to_html/adapters/rtf.rb index 95f499689..4a08bf618 100644 --- a/lib/attachment_to_html/adapters/rtf.rb +++ b/lib/attachment_to_html/adapters/rtf.rb @@ -1,9 +1,10 @@ +# -*- encoding : utf-8 -*- module AttachmentToHTML module Adapters # Convert application/rtf documents in to HTML - class RTF + class RTF < Adapter - attr_reader :attachment, :tmpdir + attr_reader :tmpdir # Public: Initialize a RTF converter # @@ -12,24 +13,10 @@ module AttachmentToHTML # :tmpdir - String name of directory to store the # converted document def initialize(attachment, opts = {}) - @attachment = attachment + super @tmpdir = opts.fetch(:tmpdir, ::Rails.root.join('tmp')) end - # Public: The title to use in the <title> tag - # - # Returns a String - def title - @title ||= attachment.display_filename - end - - # Public: The contents of the extracted html <body> tag - # - # Returns a String - def body - @body ||= parse_body - end - # Public: Was the document conversion successful? # # Returns a Boolean @@ -44,14 +31,6 @@ module AttachmentToHTML match ? match[1] : '' end - def has_content? - !body.gsub(/\s+/,"").gsub(/\<[^\>]*\>/, "").empty? - end - - def contains_images? - body.match(/<img[^>]*>/mi) ? true : false - end - def convert # Get the attachment body outside of the chdir call as getting # the body may require opening files too @@ -82,28 +61,6 @@ module AttachmentToHTML end html end - - def create_tempfile(text) - tempfile = if RUBY_VERSION.to_f >= 1.9 - Tempfile.new('foiextract', '.', - :encoding => text.encoding) - else - Tempfile.new('foiextract', '.') - end - tempfile.print(text) - tempfile.flush - tempfile - end - - def cleanup_tempfile(tempfile) - tempfile.close - tempfile.delete - end - - def attachment_body - @attachment_body ||= attachment.body - end - end end end diff --git a/lib/attachment_to_html/adapters/text.rb b/lib/attachment_to_html/adapters/text.rb index e99183f0e..61e4e57a8 100644 --- a/lib/attachment_to_html/adapters/text.rb +++ b/lib/attachment_to_html/adapters/text.rb @@ -1,33 +1,8 @@ +# -*- encoding : utf-8 -*- module AttachmentToHTML module Adapters # Convert text/plain documents in to HTML - class Text - - attr_reader :attachment - - # Public: Initialize a Text converter - # - # attachment - the FoiAttachment to convert to HTML - # opts - a Hash of options (default: {}): - # No options currently accepted - def initialize(attachment, opts = {}) - @attachment = attachment - end - - # Public: The title to use in the <title> tag - # - # Returns a String - def title - @title ||= attachment.display_filename - end - - # Public: The contents of the extracted html <body> tag - # - # Returns a String - def body - @body ||= parse_body - end - + class Text < Adapter # Public: Was the document conversion successful? # # Returns a Boolean @@ -43,19 +18,6 @@ module AttachmentToHTML text = MySociety::Format.make_clickable(text) text = text.gsub(/\n/, '<br>') end - - def parse_body - convert - end - - def has_content? - !body.gsub(/\s+/,"").gsub(/\<[^\>]*\>/, "").empty? - end - - def contains_images? - body.match(/<img[^>]*>/mi) ? true : false - end - end end end diff --git a/lib/attachment_to_html/attachment_to_html.rb b/lib/attachment_to_html/attachment_to_html.rb index 2f7c08264..2e8d35ca9 100644 --- a/lib/attachment_to_html/attachment_to_html.rb +++ b/lib/attachment_to_html/attachment_to_html.rb @@ -1,5 +1,8 @@ +# -*- encoding : utf-8 -*- require 'view' +require 'attachment_to_html/adapter' + Dir[File.dirname(__FILE__) + '/adapters/*.rb'].each do |file| require file end diff --git a/lib/attachment_to_html/view.rb b/lib/attachment_to_html/view.rb index e6991d44e..0d5b205b7 100644 --- a/lib/attachment_to_html/view.rb +++ b/lib/attachment_to_html/view.rb @@ -1,3 +1,4 @@ +# -*- encoding : utf-8 -*- module AttachmentToHTML class View < ERB |