diff options
author | Gareth Rees <gareth@mysociety.org> | 2014-04-02 11:31:55 +0100 |
---|---|---|
committer | Gareth Rees <gareth@mysociety.org> | 2014-04-07 16:11:14 +0100 |
commit | 44eff43ee8024a03fe4c327638ac0dbc1b47f4fd (patch) | |
tree | 1a47beaa5aedc0b60c2b908e87bf8d628bcf34f0 | |
parent | ea1e040780f00938331e92472780c91b7e0f43a2 (diff) |
Simpler AttachmentToHTML::Adapters::Text interface
-rw-r--r-- | lib/attachment_to_html/adapters/text.rb | 61 | ||||
-rw-r--r-- | spec/lib/attachment_to_html/adapters/text_spec.rb | 95 |
2 files changed, 42 insertions, 114 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 diff --git a/spec/lib/attachment_to_html/adapters/text_spec.rb b/spec/lib/attachment_to_html/adapters/text_spec.rb index 599670603..b2e8141e0 100644 --- a/spec/lib/attachment_to_html/adapters/text_spec.rb +++ b/spec/lib/attachment_to_html/adapters/text_spec.rb @@ -3,93 +3,47 @@ require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') describe AttachmentToHTML::Adapters::Text do let(:attachment) { FactoryGirl.build(:body_text) } - let(:text_adapter) { AttachmentToHTML::Adapters::Text.new(attachment) } + let(:adapter) { AttachmentToHTML::Adapters::Text.new(attachment) } - describe :wrapper do + describe :title do - it 'defaults to wrapper' do - text_adapter.wrapper.should == 'wrapper' - end - - it 'accepts a wrapper option' do - text_adapter = AttachmentToHTML::Adapters::Text.new(attachment, :wrapper => 'wrap') - text_adapter.wrapper.should == 'wrap' + it 'uses the attachment filename for the title' do + adapter.title.should == attachment.display_filename end end - describe :to_html do - - it 'should be a valid html document' do - parsed = Nokogiri::HTML.parse(text_adapter.to_html) do |config| - config.strict - end - parsed.errors.any?.should be_false - end - - it 'contains the attachment filename in the title tag' do - parsed = Nokogiri::HTML.parse(text_adapter.to_html) do |config| - config.strict - end - parsed.css('title').inner_html.should == attachment.display_filename - end + describe :body do - it 'contains the wrapper div in the body tag' do - text_adapter = AttachmentToHTML::Adapters::Text.new(attachment, :wrapper => 'wrap') - parsed = Nokogiri::HTML.parse(text_adapter.to_html) do |config| - config.strict - end - parsed.css('body').children.first.attributes['id'].value.should == 'wrap' + it 'extracts the body from the document' do + adapter.body.should == attachment.body end - it 'contains the attachment body in the wrapper div' do - text_adapter = AttachmentToHTML::Adapters::Text.new(attachment, :wrapper => 'wrap') - parsed = Nokogiri::HTML.parse(text_adapter.to_html) do |config| - config.strict - end - parsed.css('div#wrap div#view-html-content').inner_html.should == attachment.body - end - it 'strips the body of trailing whitespace' do attachment = FactoryGirl.build(:body_text, :body => ' Hello ') - text_adapter = AttachmentToHTML::Adapters::Text.new(attachment) - parsed = Nokogiri::HTML.parse(text_adapter.to_html) do |config| - config.strict - end - parsed.css('div#wrapper div#view-html-content').inner_html.should == 'Hello' + adapter = AttachmentToHTML::Adapters::Text.new(attachment) + adapter.body.should == 'Hello' end - # NOTE: Can't parse this spec with Nokogiri at the moment because even - # in strict mode Nokogiri tampers with the HTML returned: - # Failure/Error: parsed.css('div#wrapper div#view-html-content'). - # inner_html.should == expected - # expected: "Usage: foo "bar" >baz<" - # got: "Usage: foo \"bar\" >baz<" (using ==) it 'escapes special characters' do attachment = FactoryGirl.build(:body_text, :body => 'Usage: foo "bar" >baz<') - text_adapter = AttachmentToHTML::Adapters::Text.new(attachment) + adapter = AttachmentToHTML::Adapters::Text.new(attachment) expected = %Q(Usage: foo "bar" >baz<) - text_adapter.to_html.should include(expected) + adapter.body.should == expected end it 'creates hyperlinks for text that looks like a url' do attachment = FactoryGirl.build(:body_text, :body => 'http://www.whatdotheyknow.com') - text_adapter = AttachmentToHTML::Adapters::Text.new(attachment) - parsed = Nokogiri::HTML.parse(text_adapter.to_html) do |config| - config.strict - end - parsed.css('div#wrapper div#view-html-content a').first.text.should == 'http://www.whatdotheyknow.com' - parsed.css('div#wrapper div#view-html-content a').first['href'].should == 'http://www.whatdotheyknow.com' + adapter = AttachmentToHTML::Adapters::Text.new(attachment) + expected = %Q(<a href='http://www.whatdotheyknow.com'>http://www.whatdotheyknow.com</a>) + adapter.body.should == expected end it 'substitutes newlines for br tags' do attachment = FactoryGirl.build(:body_text, :body => "A\nNewline") - text_adapter = AttachmentToHTML::Adapters::Text.new(attachment) - parsed = Nokogiri::HTML.parse(text_adapter.to_html) do |config| - config.strict - end + adapter = AttachmentToHTML::Adapters::Text.new(attachment) expected = %Q(A<br>Newline) - parsed.css('div#wrapper div#view-html-content').inner_html.should == expected + adapter.body.should == expected end end @@ -97,23 +51,18 @@ describe AttachmentToHTML::Adapters::Text do describe :success? do it 'is successful if the body has content excluding the tags' do - text_adapter.to_html - text_adapter.success?.should be_true + adapter.stub(:body).and_return('<p>some content</p>') + adapter.success?.should be_true end it 'is successful if the body contains images' do - mocked_return = %Q(<!DOCTYPE html><html><head></head><body><img src="logo.png" /></body></html>) - text_adapter = AttachmentToHTML::Adapters::Text.new(attachment) - text_adapter.stub(:to_html).and_return(mocked_return) - text_adapter.success?.should be_true + adapter.stub(:body).and_return(%Q(<img src="logo.png" />)) + adapter.success?.should be_true end it 'is not successful if the body has no content other than tags' do - empty_txt = load_file_fixture('empty.txt') - attachment = FactoryGirl.build(:body_text, :body => empty_txt) - text_adapter = AttachmentToHTML::Adapters::Text.new(attachment) - text_adapter.to_html - text_adapter.success?.should be_false + adapter.stub(:body).and_return('<p></p>') + adapter.success?.should be_false end end |