Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strip trailing space from lines in address blocks #371

Merged
merged 5 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## 8.8.0

* Strip trailing backslashes from the lines of an address ([#371](https://github.com/alphagov/govspeak/pull/371))
* When replacing line breaks with "<br>"s in the parsing of address blocks,
replace the whole "\r\n" line break and not just the "\n" character ([#371](https://github.com/alphagov/govspeak/pull/371))
* Only strip a leading line break from an address block, not just any old first
occurrence of a line break ([#371](https://github.com/alphagov/govspeak/pull/371))
* Strip trailing whitespace from the lines of an address ([#371](https://github.com/alphagov/govspeak/pull/371))

## 8.7.0

* Allow data attributes in spans ([#364](https://github.com/alphagov/govspeak/pull/364))
Expand Down
7 changes: 6 additions & 1 deletion lib/govspeak.rb
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,12 @@ def render_image(image)
end

extension("address", surrounded_by("$A")) do |body|
%(\n<div class="address"><div class="adr org fn"><p markdown="1">\n#{body.sub("\n", '').gsub("\n", '<br />')}\n</p></div></div>\n)
<<~BODY
mike3985 marked this conversation as resolved.
Show resolved Hide resolved

<div class="address"><div class="adr org fn"><p markdown="1">
#{body.lstrip.sub(/[\s\\]*\z/, '').gsub(/[ \\]*\r?\n/, '<br />')}
mike3985 marked this conversation as resolved.
Show resolved Hide resolved
</p></div></div>
BODY
end

extension("legislative list", /#{NEW_PARAGRAPH_LOOKBEHIND}\$LegislativeList\s*$(.*?)\$EndLegislativeList/m) do |body|
Expand Down
2 changes: 1 addition & 1 deletion lib/govspeak/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Govspeak
VERSION = "8.7.0".freeze
VERSION = "8.8.0".freeze
end
41 changes: 37 additions & 4 deletions test/govspeak_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,40 @@ class GovspeakTest < Minitest::Test
Teston
0123 456 7890 $A )
doc = Govspeak::Document.new(input)
assert_equal %(\n<div class="address"><div class="adr org fn"><p>\n123 Test Street<br>Testcase Cliffs<br>Teston<br>0123 456 7890 \n</p></div></div>\n), doc.to_html
assert_equal %(\n<div class="address"><div class="adr org fn"><p>\n123 Test Street<br>Testcase Cliffs<br>Teston<br>0123 456 7890\n</p></div></div>\n), doc.to_html
end

test "newlines in address block content are replaced with <br>s" do
input = %($A\n123 Test Street\nTestcase Cliffs\nTeston\n0123 456 7890\n$A)
doc = Govspeak::Document.new(input)
assert_equal %(\n<div class="address"><div class="adr org fn"><p>\n123 Test Street<br>Testcase Cliffs<br>Teston<br>0123 456 7890\n</p></div></div>\n), doc.to_html
end

test "combined return-and-newlines in address block content are replaced with <br>s" do
input = %($A\r\n123 Test Street\r\nTestcase Cliffs\r\nTeston\r\n0123 456 7890\r\n$A)
doc = Govspeak::Document.new(input)
assert_equal %(\n<div class="address"><div class="adr org fn"><p>\n123 Test Street<br>Testcase Cliffs<br>Teston<br>0123 456 7890\n</p></div></div>\n), doc.to_html
end

test "trailing backslashes are stripped from address block content" do
# two trailing backslashes would normally be converted into a line break by Kramdown
input = %($A\r\n123 Test Street\\\r\nTestcase Cliffs\\\nTeston\\\\\r\n0123 456 7890\\\\\n$A)
doc = Govspeak::Document.new(input)
assert_equal %(\n<div class="address"><div class="adr org fn"><p>\n123 Test Street<br>Testcase Cliffs<br>Teston<br>0123 456 7890\n</p></div></div>\n), doc.to_html
end

test "trailing spaces are stripped from address block content" do
# two trailing spaces would normally be converted into a line break by Kramdown
input = %($A\r\n123 Test Street \r\nTestcase Cliffs \nTeston \r\n0123 456 7890 \n$A)
doc = Govspeak::Document.new(input)
assert_equal %(\n<div class="address"><div class="adr org fn"><p>\n123 Test Street<br>Testcase Cliffs<br>Teston<br>0123 456 7890\n</p></div></div>\n), doc.to_html
end

test "trailing backslashes and spaces are stripped from address block content" do
# two trailing backslashes or two trailing spaces would normally be converted into a line break by Kramdown
input = %($A\r\n123 Test Street \\\r\nTestcase Cliffs\\ \nTeston\\\\ \r\n0123 456 7890 \\\\\n$A)
doc = Govspeak::Document.new(input)
assert_equal %(\n<div class="address"><div class="adr org fn"><p>\n123 Test Street<br>Testcase Cliffs<br>Teston<br>0123 456 7890\n</p></div></div>\n), doc.to_html
end

test "should convert barchart" do
Expand Down Expand Up @@ -141,7 +174,7 @@ class GovspeakTest < Minitest::Test
Teston
0123 456 7890 $A)
doc = Govspeak::Document.new(input)
assert_equal %(<p>Paragraph1</p>\n\n<div class="address"><div class="adr org fn"><p>\n123 Test Street<br>Testcase Cliffs<br>Teston<br>0123 456 7890 \n</p></div></div>\n), doc.to_html
assert_equal %(<p>Paragraph1</p>\n\n<div class="address"><div class="adr org fn"><p>\n123 Test Street<br>Testcase Cliffs<br>Teston<br>0123 456 7890\n</p></div></div>\n), doc.to_html
end

test_given_govspeak("^ I am very informational ^") do
Expand Down Expand Up @@ -388,7 +421,7 @@ class GovspeakTest < Minitest::Test
$A" do
assert_html_output %(
<div class="address"><div class="adr org fn"><p>
street<br>road<br>
street<br>road
</p></div></div>)
assert_text_output "street road"
end
Expand All @@ -402,7 +435,7 @@ class GovspeakTest < Minitest::Test
*[ACRONYM]: This is the acronym explanation" do
assert_html_output %(
<div class="address"><div class="adr org fn"><p>
street with <abbr title="This is the acronym explanation">ACRONYM</abbr><br>road<br>
street with <abbr title="This is the acronym explanation">ACRONYM</abbr><br>road
</p></div></div>)
end

Expand Down
Loading