diff options
author | Gareth Rees <gareth@mysociety.org> | 2014-03-17 11:15:40 +0000 |
---|---|---|
committer | Gareth Rees <gareth@mysociety.org> | 2014-03-28 09:39:04 +0000 |
commit | d57ca2a22579df4c634d554989c0ee9e4ebb5165 (patch) | |
tree | e1d11c626cedf57373be95b6b1ec6ce4dc22ea30 /lib/attachment_to_html/adapters/text.rb | |
parent | 0adf9399cbef42054809479c8f1b64dad7bbf8ca (diff) |
Add AttachmentToHTML library
Extracts the attachment processing from FoiAttachment#body_to_html
AttachmentToHTML contains adapters which convert
- text/plain
- application/pdf
- application/rtf
Results are returned as an AttachmentHTML::HTML instance which contains
the raw HTML and other metadata about the conversion.
Diffstat (limited to 'lib/attachment_to_html/adapters/text.rb')
-rw-r--r-- | lib/attachment_to_html/adapters/text.rb | 84 |
1 files changed, 84 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..1ce616cf7 --- /dev/null +++ b/lib/attachment_to_html/adapters/text.rb @@ -0,0 +1,84 @@ +require 'nokogiri' + +module AttachmentToHTML + module Adapters + # Convert text/plain documents in to HTML + class Text + + attr_reader :attachment, :wrapper + + # 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 + def initialize(attachment, opts = {}) + @attachment = attachment + @wrapper = opts.fetch(:wrapper, 'wrapper') + end + + # Public: Convert the attachment to HTML + # + # Returns a String + def to_html + @html ||= generate_html + end + + # Public: Was the document conversion successful? + # + # Returns a Boolean + def success? + has_content? || contains_images? + end + + 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 + 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? + end + + def contains_images? + parsed.css('body img').any? + end + + # Parse the output of to_html to check for success + # + # Returns a Nokogiri::HTML::Document + def parsed + @parsed ||= Nokogiri::HTML.parse(to_html) + end + + end + end +end |