Skip to content
Open
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
10 changes: 9 additions & 1 deletion lib/govspeak.rb
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,6 @@ def render_image(image)
# before $C otherwise the first ($C)TA fill be matched to a contact tag.
wrap_with_div("summary", "$!")
wrap_with_div("form-download", "$D")
wrap_with_div("contact", "$C")
wrap_with_div("place", "$P", Govspeak::Document)
wrap_with_div("information", "$I", Govspeak::Document)
wrap_with_div("additional-information", "$AI")
Expand All @@ -329,6 +328,15 @@ def render_image(image)
BODY
end

extension("contact", surrounded_by("$C")) do |body|
<<~BODY

<div class="contact"><p markdown="1">
#{body.lstrip.sub(/[\s\\]*\z/, '').gsub(/[ \\]*\r?\n/, '<br />')}
</p></div>
BODY
end

extension("legislative list", /#{NEW_PARAGRAPH_LOOKBEHIND}\$LegislativeList\s*$(.*?)\$EndLegislativeList/m) do |body|
# The surrounding div is neccessary to accurately identify legislative lists
# in post-processing.
Expand Down
169 changes: 125 additions & 44 deletions test/govspeak_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,125 @@ class GovspeakTest < Minitest::Test
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 "address div is separated from paragraph text by a couple of line-breaks" do
# else kramdown processes address div as part of paragraph text and escapes HTML
input = %(Paragraph1

$A
123 Test Street
Testcase Cliffs
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
end

test_given_govspeak "
$A
street
road
$A" do
assert_html_output %(
<div class="address"><div class="adr org fn"><p>
street<br>road
</p></div></div>)
assert_text_output "street road"
end

test_given_govspeak "
$A
street with ACRONYM
road
$A

*[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
</p></div></div>)
end

test "trailing space after the contact should not prevent parsing" do
input = %($C
123 Test Street
Testcase Cliffs
Teston
0123 456 7890 $C )
doc = Govspeak::Document.new(input)
assert_equal %(\n<div class="contact"><p>\n123 Test Street<br>Testcase Cliffs<br>Teston<br>0123 456 7890\n</p></div>\n), doc.to_html
end

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

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

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

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

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

test "contact div is separated from paragraph text by a couple of line-breaks" do
# else kramdown processes contact div as part of paragraph text and escapes HTML
input = %(Paragraph1

$C
123 Test Street
Testcase Cliffs
Teston
0123 456 7890 $C)
doc = Govspeak::Document.new(input)
assert_equal %(<p>Paragraph1</p>\n\n<div class="contact"><p>\n123 Test Street<br>Testcase Cliffs<br>Teston<br>0123 456 7890\n</p></div>\n), doc.to_html
end

test_given_govspeak "
$C
street
road
$C" do
assert_html_output %(
<div class="contact"><p>
street<br>road
</p></div>)
assert_text_output "street road"
end

test_given_govspeak "
$C
street with ACRONYM
road
$C

*[ACRONYM]: This is the acronym explanation" do
assert_html_output %(
<div class="contact"><p>
street with <abbr title="This is the acronym explanation">ACRONYM</abbr><br>road
</p></div>)
end

test "should convert barchart" do
input = <<~GOVSPEAK
|col|
Expand All @@ -208,19 +327,6 @@ class GovspeakTest < Minitest::Test
assert_equal %(<table class=\"js-barchart-table mc-stacked compact mc-negative mc-auto-outdent\">\n <thead>\n <tr>\n <th scope="col">col</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <td>val</td>\n </tr>\n </tbody>\n</table>\n), html
end

test "address div is separated from paragraph text by a couple of line-breaks" do
# else kramdown processes address div as part of paragraph text and escapes HTML
input = %(Paragraph1

$A
123 Test Street
Testcase Cliffs
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
end

test "processing an extension does not modify the provided input" do
input = "^ I am very informational"
Govspeak::Document.new(input).to_html
Expand Down Expand Up @@ -403,37 +509,12 @@ class GovspeakTest < Minitest::Test

test_given_govspeak "$C help, send cake $C" do
assert_html_output %(
<div class="contact">
<p>help, send cake</p>
</div>)
<div class="contact"><p>
help, send cake
</p></div>)
assert_text_output "help, send cake"
end

test_given_govspeak "
$A
street
road
$A" do
assert_html_output %(
<div class="address"><div class="adr org fn"><p>
street<br>road
</p></div></div>)
assert_text_output "street road"
end

test_given_govspeak "
$A
street with ACRONYM
road
$A

*[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
</p></div></div>)
end

test_given_govspeak "
$P
$I
Expand Down Expand Up @@ -667,9 +748,9 @@ class GovspeakTest < Minitest::Test
<p>Click here to start the tool</p>
</div>

<div class="contact">
<p>Here is some text</p>
</div>)
<div class="contact"><p>
Here is some text
</p></div>)
end

test_given_govspeak "
Expand Down
Loading