aboutsummaryrefslogtreecommitdiffstats
path: root/lib/attachment_to_html
diff options
context:
space:
mode:
Diffstat (limited to 'lib/attachment_to_html')
-rw-r--r--lib/attachment_to_html/adapters/text.rb61
1 files changed, 20 insertions, 41 deletions
diff --git a/lib/attachment_to_html/adapters/text.rb b/lib/attachment_to_html/adapters/text.rb
index 1ce616cf7..b431ada5e 100644
--- a/lib/attachment_to_html/adapters/text.rb
+++ b/lib/attachment_to_html/adapters/text.rb
@@ -5,24 +5,29 @@ module AttachmentToHTML
# Convert text/plain documents in to HTML
class Text
- attr_reader :attachment, :wrapper
+ attr_reader :attachment
# Public: Initialize a Text 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
+ # No options currently accepted
def initialize(attachment, opts = {})
@attachment = attachment
- @wrapper = opts.fetch(:wrapper, 'wrapper')
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?
@@ -34,51 +39,25 @@ 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
- end
-
- def body
+ def convert
text = attachment.body.strip
text = CGI.escapeHTML(text)
text = MySociety::Format.make_clickable(text)
text = text.gsub(/\n/, '<br>')
end
- # Does the body element have any content, excluding HTML tags?
- #
- # Returns a Boolean
- def has_content?
- !parsed.css('body').inner_text.empty?
+ def parse_body
+ convert
end
- def contains_images?
- parsed.css('body img').any?
+ def has_content?
+ !body.gsub(/\s+/,"").gsub(/\<[^\>]*\>/, "").empty?
end
- # Parse the output of to_html to check for success
- #
- # Returns a Nokogiri::HTML::Document
- def parsed
- @parsed ||= Nokogiri::HTML.parse(to_html)
+ def contains_images?
+ body.match(/<img[^>]*>/mi) ? true : false
end
- end
+ end
end
end