diff --git a/lib/govspeak.rb b/lib/govspeak.rb index 9fcaf9d8..632b1c2f 100644 --- a/lib/govspeak.rb +++ b/lib/govspeak.rb @@ -189,7 +189,7 @@ def insert_strong_inside_p(body, parser=Govspeak::Document) end end - extension('attachment', /\[embed:attachments:([0-9a-f-]+)\]/) do |content_id, body| + extension('attachment', /\[embed:attachments:(?!inline:|image:)\s*(.*?)\s*\]/) do |content_id, body| attachment = attachments.detect { |a| a[:content_id].match(content_id) } next "" unless attachment attachment = AttachmentPresenter.new(attachment) @@ -197,7 +197,7 @@ def insert_strong_inside_p(body, parser=Govspeak::Document) ERB.new(content).result(binding) end - extension('attachment inline', /\[embed:attachments:inline:([0-9a-f-]+)\]/) do |content_id| + extension('attachment inline', /\[embed:attachments:inline:\s*(.*?)\s*\]/) do |content_id| attachment = attachments.detect { |a| a[:content_id].match(content_id) } next "" unless attachment attachment = AttachmentPresenter.new(attachment) @@ -209,7 +209,7 @@ def insert_strong_inside_p(body, parser=Govspeak::Document) %{#{link}#{attributes}} end - extension('attachment image', /\[embed:attachments:image:([0-9a-f-]+)\]/) do |content_id| + extension('attachment image', /\[embed:attachments:image:\s*(.*?)\s*\]/) do |content_id| attachment = attachments.detect { |a| a[:content_id].match(content_id) } next "" unless attachment attachment = AttachmentPresenter.new(attachment) @@ -299,7 +299,7 @@ def self.devolved_options end end - extension('embed link', /\[embed:link:([0-9a-f-]+)\]/) do |content_id| + extension('embed link', /\[embed:link:\s*(.*?)\s*\]/) do |content_id| link = links.detect { |l| l[:content_id].match(content_id) } next "" unless link if link[:url] @@ -314,7 +314,7 @@ def render_hcard_address(contact) end private :render_hcard_address - extension('Contact', /\[Contact:([0-9a-f-]+)\]/) do |content_id| + extension('Contact', /\[Contact:\s*(.*?)\s*\]/) do |content_id| contact = contacts.detect { |c| c[:content_id].match(content_id) } next "" unless contact contact = ContactPresenter.new(contact) diff --git a/test/govspeak_attachments_image_test.rb b/test/govspeak_attachments_image_test.rb index f0b87130..28fbf0d2 100644 --- a/test/govspeak_attachments_image_test.rb +++ b/test/govspeak_attachments_image_test.rb @@ -21,7 +21,7 @@ def compress_html(html) end test "renders an empty string for an image attachment not found" do - assert_match("", render_govspeak("[embed:attachments:image:1fe8]", [build_attachment])) + assert_equal("\n", render_govspeak("[embed:attachments:image:1fe8]", [build_attachment])) end test "wraps an attachment in a figure with the id if the id is present" do @@ -40,6 +40,14 @@ def compress_html(html) assert_match(/
/, rendered) end + test "renders an attachment if there are spaces around the content_id" do + rendered = render_govspeak( + "[embed:attachments:image: 1fe8 ]", + [build_attachment(id: 10, content_id: "1fe8")] + ) + assert_match(/
/, rendered) + end + test "has an image element to the file" do rendered = render_govspeak( "[embed:attachments:image:1fe8]", @@ -87,4 +95,14 @@ def compress_html(html) regex = %r{
(.*?)
} assert_match(regex, rendered) end + + test "renders an empty string for no match" do + rendered = render_govspeak("[embed:attachments:image:1fe8]") + assert_equal("\n", rendered) + end + + test "renders an empty string for a filename instead of a content id" do + rendered = render_govspeak("[embed:attachments:image: path/to/file.jpg ]") + assert_equal("\n", rendered) + end end diff --git a/test/govspeak_attachments_inline_test.rb b/test/govspeak_attachments_inline_test.rb index a2094265..9cee4148 100644 --- a/test/govspeak_attachments_inline_test.rb +++ b/test/govspeak_attachments_inline_test.rb @@ -17,7 +17,7 @@ def render_govspeak(govspeak, attachments = []) end test "renders an empty string for an inline attachment not found" do - assert_match("", render_govspeak("[embed:attachments:inline:1fe8]", [build_attachment])) + assert_equal("\n", render_govspeak("[embed:attachments:inline:1fe8]", [build_attachment])) end test "wraps an attachment in a span with the id if the id is present" do @@ -36,6 +36,14 @@ def render_govspeak(govspeak, attachments = []) assert_match(//, rendered) end + test "renders an attachment where the content id has spaces" do + rendered = render_govspeak( + "[embed:attachments:inline: 1fe8 ]", + [build_attachment(id: nil, content_id: "1fe8")] + ) + assert_match(//, rendered) + end + test "links to the attachment file" do rendered = render_govspeak( "[embed:attachments:inline:1fe8]", @@ -151,4 +159,14 @@ def render_govspeak(govspeak, attachments = []) assert_match(%r{Text File}, rendered) assert_match(%r{PDF File}, rendered) end + + test "will render a not found attachment as an empty string" do + rendered = render_govspeak("[embed:attachments:inline:1fe8]") + assert_equal("\n", rendered) + end + + test "will render a file name used as a content id as an empty string" do + rendered = render_govspeak("[embed:attachments:inline: path/to/file name.jpg ]") + assert_equal("\n", rendered) + end end diff --git a/test/govspeak_attachments_test.rb b/test/govspeak_attachments_test.rb index 35d72cf7..4992cc87 100644 --- a/test/govspeak_attachments_test.rb +++ b/test/govspeak_attachments_test.rb @@ -21,7 +21,7 @@ def render_govspeak(govspeak, attachments = [], options = {}) Govspeak::Document.new(govspeak, options).to_html end - test "Wraps an attachment in a section.attachment.embedded" do + test "wraps an attachment in a section.attachment.embedded" do rendered = render_govspeak( "[embed:attachments:3ed2]", [build_attachment(content_id: "3ed2")] @@ -29,7 +29,15 @@ def render_govspeak(govspeak, attachments = [], options = {}) assert_match(/
/, rendered) end - test "Wraps an external attachment in a section.attachment.hosted-externally" do + test "can convert an attachment with spaces" do + rendered = render_govspeak( + "[embed:attachments: 3ed2 ]", + [build_attachment(content_id: "3ed2")] + ) + assert_match(/
/, rendered) + end + + test "wraps an external attachment in a section.attachment.hosted-externally" do rendered = render_govspeak( "[embed:attachments:3ed2]", [build_attachment(content_id: "3ed2", external?: true)] @@ -37,7 +45,7 @@ def render_govspeak(govspeak, attachments = [], options = {}) assert_match(/
/, rendered) end - test "Outputs a pub-cover.png thumbnail by default" do + test "outputs a pub-cover.png thumbnail by default" do rendered = render_govspeak( "[embed:attachments:3ed2]", [build_attachment(content_id: "3ed2")] @@ -45,7 +53,7 @@ def render_govspeak(govspeak, attachments = [], options = {}) assert_match(%r{}, compress_html(rendered)) end - test "Outputs a title link within a h2" do + test "outputs a title link within a h2" do rendered = render_govspeak( "[embed:attachments:3ed2]", [build_attachment(content_id: "3ed2", id: 1, url: "http://a.b/c.pdf", title: "Attachment Title")] @@ -101,7 +109,7 @@ def render_govspeak(govspeak, attachments = [], options = {}) assert_match(%r{

\s*Attachment Title

}, compress_html(rendered)) end - test "Title link has rel='external' for an external link" do + test "title link has rel='external' for an external link" do rendered = render_govspeak( "[embed:attachments:3ed2]", [build_attachment(content_id: "3ed2", id: 1, url: "http://a.b/c.pdf", external?: true)] @@ -109,7 +117,7 @@ def render_govspeak(govspeak, attachments = [], options = {}) assert_match(%r{}, rendered) end - test "Accessible attachment doesn't have the aria-describedby attribute" do + test "accessible attachment doesn't have the aria-describedby attribute" do rendered = render_govspeak( "[embed:attachments:3ed2]", [build_attachment(content_id: "3ed2", url: "http://a.b/c.pdf", accessible?: true)] @@ -117,7 +125,7 @@ def render_govspeak(govspeak, attachments = [], options = {}) assert_match(%r{}, rendered) end - test "Outputs reference if isbn is present" do + test "outputs reference if isbn is present" do rendered = render_govspeak( "[embed:attachments:3ed2]", [build_attachment(content_id: "3ed2", isbn: "123")] @@ -125,7 +133,7 @@ def render_govspeak(govspeak, attachments = [], options = {}) assert_match(%r{Ref: ISBN 123}, rendered) end - test "Outputs reference if uniuque_reference is present" do + test "outputs reference if uniuque_reference is present" do rendered = render_govspeak( "[embed:attachments:3ed2]", [build_attachment(content_id: "3ed2", unique_reference: "123")] @@ -133,7 +141,7 @@ def render_govspeak(govspeak, attachments = [], options = {}) assert_match(%r{Ref: 123}, rendered) end - test "Outputs reference if command_paper_number is present" do + test "outputs reference if command_paper_number is present" do rendered = render_govspeak( "[embed:attachments:3ed2]", [build_attachment(content_id: "3ed2", command_paper_number: "11")] @@ -141,7 +149,7 @@ def render_govspeak(govspeak, attachments = [], options = {}) assert_match(%r{Ref: 11}, rendered) end - test "Outputs reference if hoc_paper_number is present" do + test "outputs reference if hoc_paper_number is present" do rendered = render_govspeak( "[embed:attachments:3ed2]", [build_attachment(content_id: "3ed2", hoc_paper_number: "15", parliamentary_session: "1")] @@ -149,7 +157,7 @@ def render_govspeak(govspeak, attachments = [], options = {}) assert_match(%r{Ref: HC 15 1}, rendered) end - test "Can have multiple references" do + test "can have multiple references" do rendered = render_govspeak( "[embed:attachments:3ed2]", [build_attachment(content_id: "3ed2", isbn: "123", unique_reference: "55")] @@ -173,7 +181,7 @@ def render_govspeak(govspeak, attachments = [], options = {}) assert_match(%r{\s*Unnumbered act paper\s*}, compress_html(rendered)) end - test "Unnumbered command paper takes precedence to unnumbered act paper" do + test "unnumbered command paper takes precedence to unnumbered act paper" do rendered = render_govspeak( "[embed:attachments:3ed2]", [build_attachment(content_id: "3ed2", unnumbered_command_paper?: true, unnumbered_hoc_paper?: true)] @@ -181,7 +189,7 @@ def render_govspeak(govspeak, attachments = [], options = {}) assert_match(%r{\s*Unnumbered command paper\s*}, compress_html(rendered)) end - test "Shows a preview link for a previewable format" do + test "shows a preview link for a previewable format" do rendered = render_govspeak( "[embed:attachments:3ed2]", [build_attachment(content_id: "3ed2", file_extension: "csv", url: "http://a.b/c.csv")] @@ -392,6 +400,12 @@ def render_govspeak(govspeak, attachments = [], options = {}) test "attachment that isn't provided" do govspeak = "[embed:attachments:906ac8b7-850d-45c6-98e0-9525c680f891]" rendered = Govspeak::Document.new(govspeak).to_html - assert_match("", rendered) + assert_equal("\n", rendered) + end + + test "attachment where filename is provided, rather than a content_id" do + govspeak = "[embed:attachments:/path/to/file%20name.pdf]" + rendered = Govspeak::Document.new(govspeak).to_html + assert_equal("\n", rendered) end end