aboutsummaryrefslogtreecommitdiffstats
path: root/lib/attachment_to_html/adapters/text.rb
diff options
context:
space:
mode:
authorLouise Crow <louise.crow@gmail.com>2014-05-15 16:09:29 +0100
committerLouise Crow <louise.crow@gmail.com>2014-05-15 16:09:29 +0100
commit26348ce676f7ebbabcc535b2ecf00f99f8fe85c0 (patch)
tree6034dd656a0f9af152f24d6c491b5e61a7190a7d /lib/attachment_to_html/adapters/text.rb
parent851ef575cf3c55a3bb194381497b958c2a3ebf1a (diff)
parentb3fa23047bf96bd6a08273c9491ac1ee3b4a3f80 (diff)
Merge branch 'release/0.18' into wdtk
Diffstat (limited to 'lib/attachment_to_html/adapters/text.rb')
-rw-r--r--lib/attachment_to_html/adapters/text.rb63
1 files changed, 63 insertions, 0 deletions
diff --git a/lib/attachment_to_html/adapters/text.rb b/lib/attachment_to_html/adapters/text.rb
new file mode 100644
index 000000000..b431ada5e
--- /dev/null
+++ b/lib/attachment_to_html/adapters/text.rb
@@ -0,0 +1,63 @@
+require 'nokogiri'
+
+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
+
+ # Public: Was the document conversion successful?
+ #
+ # Returns a Boolean
+ def success?
+ has_content? || contains_images?
+ end
+
+ private
+
+ def convert
+ text = attachment.body.strip
+ text = CGI.escapeHTML(text)
+ 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