diff options
author | Gareth Rees <gareth@mysociety.org> | 2014-03-19 11:40:59 +0000 |
---|---|---|
committer | Gareth Rees <gareth@mysociety.org> | 2014-03-28 09:39:04 +0000 |
commit | b6561750b971030767a0e8146be1414471518086 (patch) | |
tree | 2455aaa6d901c0c0b1830e22651cc5ddf914364a /app/controllers/request_controller.rb | |
parent | d57ca2a22579df4c634d554989c0ee9e4ebb5165 (diff) |
Use AttachmentToHTML to generate FoiAttachment#body_as_html
Diffstat (limited to 'app/controllers/request_controller.rb')
-rw-r--r-- | app/controllers/request_controller.rb | 20 |
1 files changed, 12 insertions, 8 deletions
diff --git a/app/controllers/request_controller.rb b/app/controllers/request_controller.rb index 6445dd685..d4c303e52 100644 --- a/app/controllers/request_controller.rb +++ b/app/controllers/request_controller.rb @@ -763,18 +763,22 @@ class RequestController < ApplicationController key_path = foi_fragment_cache_path(key) image_dir = File.dirname(key_path) FileUtils.mkdir_p(image_dir) - html, wrapper_id = @attachment.body_as_html(image_dir) + html = @attachment.body_as_html(image_dir, :attachment_url => CGI.escape(@attachment_url)) - view_html_stylesheet = render_to_string :partial => "request/view_html_stylesheet" - html.sub!(/<head>/i, "<head>" + view_html_stylesheet) - html.sub!(/<body[^>]*>/i, '<body><prefix-here><div id="' + wrapper_id + '"><div id="view-html-content">') - html.sub!(/<\/body[^>]*>/i, '</div></div></body>') + view_html_stylesheet = render_to_string(:partial => "request/view_html_stylesheet") + view_html_prefix = render_to_string(:partial => "request/view_html_prefix") - view_html_prefix = render_to_string :partial => "request/view_html_prefix" - html.sub!("<prefix-here>", view_html_prefix) - html.sub!("<attachment-url-here>", CGI.escape(@attachment_url)) + # Parse the generated HTML so we can inject more stuff + parsed = Nokogiri::HTML.parse(html) + # Insert the stylesheet in the head + parsed.css('head').children.after(view_html_stylesheet) + # Insert the content prefix + parsed.css('body').children.before(view_html_prefix) + # Get the raw html to render + html = parsed.to_html @incoming_message.html_mask_stuff!(html) + response.content_type = 'text/html' render :text => html end |