aboutsummaryrefslogtreecommitdiffstats
path: root/lib/attachment_to_html
diff options
context:
space:
mode:
authorGareth Rees <gareth@mysociety.org>2014-04-02 12:11:56 +0100
committerGareth Rees <gareth@mysociety.org>2014-04-07 17:09:58 +0100
commit0532eeee63f06e796f0e967f39dfa5f23d4821f7 (patch)
tree06b6755504b189f9ab2ace3d050dd93990a0e5ec /lib/attachment_to_html
parente7d0f9a8b350ffe3c17451d6bb18051c7230ca61 (diff)
Simpler AttachmentToHTML::Adapters::RTF interface
TODO: We really should be testing the full output of RTF#body, but we currently want to remain consistent with Adapters::PDF as many methods are shared between the Adapters. A more correct spec might be: expected = %Q(<font size=3><font color="#000000">thisisthebody</font></font>) adapter.body.should == expected
Diffstat (limited to 'lib/attachment_to_html')
-rw-r--r--lib/attachment_to_html/adapters/rtf.rb62
1 files changed, 16 insertions, 46 deletions
diff --git a/lib/attachment_to_html/adapters/rtf.rb b/lib/attachment_to_html/adapters/rtf.rb
index 871ca2c60..859c0e541 100644
--- a/lib/attachment_to_html/adapters/rtf.rb
+++ b/lib/attachment_to_html/adapters/rtf.rb
@@ -3,27 +3,31 @@ module AttachmentToHTML
# Convert application/rtf documents in to HTML
class RTF
- attr_reader :attachment, :wrapper, :tmpdir
+ attr_reader :attachment, :tmpdir
# Public: Initialize a RTF converter
#
# attachment - the FoiAttachment to convert to HTML
# opts - a Hash of options (default: {}):
- # :wrapper - String id of the div that wraps the
- # attachment body
# :tmpdir - String name of directory to store the
# converted document
def initialize(attachment, opts = {})
@attachment = attachment
- @wrapper = opts.fetch(:wrapper, 'wrapper')
@tmpdir = opts.fetch(:tmpdir, ::Rails.root.join('tmp'))
end
- # Public: Convert the attachment to HTML
+ # Public: The title to use in the <title> tag
#
# Returns a String
- def to_html
- @html ||= generate_html
+ 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?
@@ -35,51 +39,17 @@ module AttachmentToHTML
private
- def generate_html
- html = "<!DOCTYPE html>"
- html += "<html>"
- html += "<head>"
- html += "<title>#{ title }</title>"
- html += "</head>"
- html += "<body>"
- html += "<div id=\"#{ wrapper }\">"
- html += "<div id=\"view-html-content\">"
- html += body
- html += "</div>"
- html += "</div>"
- html += "</body>"
- html += "</html>"
- end
-
- def title
- @title ||= attachment.display_filename
+ def parse_body
+ match = convert.match(/<body[^>]*>(.*?)<\/body>/mi)
+ match ? match[1] : ''
end
- def body
- parsed_body
- end
-
- # Parse the output of the converted attachment so that we can pluck
- # the parts we need and insert in to our own sensible template
- #
- # Returns a Nokogiri::HTML::Document
- def parsed
- @parsed ||= Nokogiri::HTML.parse(convert)
- end
-
- def parsed_body
- parsed.css('body').inner_html
- end
-
- # Does the body element have any content, excluding HTML tags?
- #
- # Returns a Boolean
def has_content?
- !parsed.css('body').inner_text.empty?
+ !body.gsub(/\s+/,"").gsub(/\<[^\>]*\>/, "").empty?
end
def contains_images?
- parsed.css('body img').any?
+ body.match(/<img[^>]*>/mi) ? true : false
end
def convert