Skip to content

Commit

Permalink
Strip spaces between elements on inline attachments
Browse files Browse the repository at this point in the history
Outputting <span class="attachment-inline"> <a href="">test</a> </span>
will output as ` test ` which is often not what an author wants.

This is a rather ugly fix for it.
  • Loading branch information
kevindew committed Sep 27, 2016
1 parent aadccd8 commit 3755124
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/govspeak.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,9 @@ def insert_strong_inside_p(body, parser=Govspeak::Document)
next "" unless attachment
attachment = AttachmentPresenter.new(attachment)
content = File.read(__dir__ + '/templates/inline_attachment.html.erb')
ERB.new(content).result(binding).gsub(/\n/, "")
rendered = ERB.new(content).result(binding)
# remove new lines with spaces, add a space between link and open bracket
rendered.gsub(/\n\s*/, "").gsub(%r{(</a>)(\()}, "\\1 \\2")
end

def render_image(url, alt_text, caption = nil)
Expand Down
10 changes: 9 additions & 1 deletion test/govspeak_attachments_inline_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ def render_govspeak(govspeak, attachments = [])
assert_match(%r{<span id="attachment[^\n]*</span>}, rendered)
end

test "doesn't have spaces between the span and the link" do
rendered = render_govspeak(
"[embed:attachments:inline:2bc1]",
[build_attachment(content_id: "2bc1", id: nil)]
)
assert_match(%r{<span class="attachment-inline"><a href=".*">.*</a></span>}, rendered)
end

test "will show HTML type (in brackets) if file_extension is specified as html" do
rendered = render_govspeak(
"[embed:attachments:inline:1fe8]",
Expand Down Expand Up @@ -129,7 +137,7 @@ def render_govspeak(govspeak, attachments = [])
type = %{<span class="type">Plain text</span>}
file_size = %{<span class="file-size">2 KB</span>}
pages = %{<span class="page-length">2 pages</span>}
assert_match(/#{link}\s*\(#{type}, #{file_size}, #{pages}\)/, rendered)
assert_match(/#{link}\s+\(#{type}, #{file_size}, #{pages}\)/, rendered)
end

test "can render two inline attachments on the same line" do
Expand Down

0 comments on commit 3755124

Please sign in to comment.