diff options
author | Gareth Rees <gareth@mysociety.org> | 2014-04-14 17:14:01 +0100 |
---|---|---|
committer | Gareth Rees <gareth@mysociety.org> | 2014-04-14 17:14:01 +0100 |
commit | 0c54aa3bc1bda24fa6cca97e52753a5ea07e7638 (patch) | |
tree | 0b3dc1c6aae0e277e51c1e43c8519f21e36401cd /lib/attachment_to_html/adapters/text.rb | |
parent | fb0742f39fc9f5ba9e45ef08a4e4312ea10660f1 (diff) | |
parent | 9f283e2e48e859d1ba6a31baa783feb177cccb17 (diff) |
Merge branch 'issues/337-attachment-title' into rails-3-develop
Diffstat (limited to 'lib/attachment_to_html/adapters/text.rb')
-rw-r--r-- | lib/attachment_to_html/adapters/text.rb | 63 |
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 |