Skip to content

Commit

Permalink
Merge pull request #285 from alphagov/fix-abbr-cta
Browse files Browse the repository at this point in the history
Reintroduce support for acronyms in Legislative Lists and Calls To Action
  • Loading branch information
brucebolt authored Aug 23, 2023
2 parents f385864 + 442b0a7 commit 9ae30e4
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 51 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Unreleased

* Reintroduce support for acronyms in Call To Action blocks and in Legislative Lists ([#285](https://github.com/alphagov/govspeak/pull/285))

## 8.1.0

* Allow attachment links wtihin numbered lists ([#283](https://github.com/alphagov/govspeak/pull/283))
Expand Down
22 changes: 10 additions & 12 deletions lib/govspeak.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,13 @@ def footnote_definitions(source)
is_legislative_list = source.scan(/\$LegislativeList.*?\[\^\d\]*.*?\$EndLegislativeList/m).size.positive?
is_cta = source.scan(/\$CTA.*?\[\^\d\]*.*?\$CTA/m).size.positive?
footnotes = source.scan(/^\s*\[\^(\d+)\]:(.*)/)
@acronyms = source.scan(/(?<=\*)\[(.*)\]:(.*)/)
@acronyms.concat(source.scan(/(?<=\*)\[(.*)\]:(.*)/))
if (is_legislative_list || is_cta) && footnotes.size.positive?
list_items = footnotes.map do |footnote|
number = footnote[0]
text = footnote[1].strip
footnote_definition = Govspeak::Document.new(text).to_html[/(?<=<p>).*(?=<\/p>)/]
footnote_definition = add_acronym_alt_text(footnote_definition)

<<~HTML_SNIPPET
<li id="fn:#{number}" role="doc-endnote">
Expand All @@ -163,10 +164,6 @@ def footnote_definitions(source)
</div>
HTML_CONTAINER
end

unless @footnote_definition_html.nil? && @acronyms.size.positive?
add_acronym_alt_text(@footnote_definition_html)
end
end

def remove_forbidden_characters(source)
Expand Down Expand Up @@ -326,6 +323,7 @@ def render_image(image)

extension("call-to-action", surrounded_by("$CTA")) do |body|
doc = Kramdown::Document.new(preprocess(body.strip), @options).to_html
doc = add_acronym_alt_text(doc)
doc = %(\n<div class="call-to-action">\n#{doc}</div>\n)
footnotes = body.scan(/\[\^(\d+)\]/).flatten

Expand All @@ -337,7 +335,6 @@ def render_image(image)
doc.sub!(/(\[\^#{footnote}\])/, html)
end

add_acronym_alt_text(doc) if @acronyms.size.positive?
doc
end

Expand All @@ -358,6 +355,8 @@ def render_image(image)

extension("legislative list", /#{NEW_PARAGRAPH_LOOKBEHIND}\$LegislativeList\s*$(.*?)\$EndLegislativeList/m) do |body|
Govspeak::KramdownOverrides.with_kramdown_ordered_lists_disabled do
body = add_acronym_alt_text(body.strip)

Kramdown::Document.new(body.strip).to_html.tap do |doc|
doc.gsub!("<ul>", "<ol>")
doc.gsub!("</ul>", "</ol>")
Expand All @@ -372,8 +371,6 @@ def render_image(image)

doc.sub!(/(\[\^#{footnote}\])/, html)
end

add_acronym_alt_text(doc) if @acronyms.size.positive?
end
end
end
Expand Down Expand Up @@ -454,10 +451,11 @@ def encode(text)
end

def add_acronym_alt_text(html)
# FIXME: this code is buggy and replaces abbreviations in HTML tags - removing the functionality for now
# @acronyms.each do |acronym|
# html.gsub!(acronym[0], "<abbr title=\"#{acronym[1].strip}\">#{acronym[0]}</abbr>")
# end
@acronyms.each do |acronym|
html.gsub!(acronym[0], "<abbr title=\"#{acronym[1].strip}\">#{acronym[0]}</abbr>")
end

html
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/govspeak_button_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class GovspeakTest < Minitest::Test
" do
assert_text_output "Start now JSA"
assert_html_output %(
<p><a class="gem-c-button govuk-button govuk-button--start" role="button" data-module="govuk-button" draggable="false" href="https://www.apply-for-new-style-jsa.dwp.gov.uk/?lang=cy"> Start now <abbr title="Jobseeker's Allowance">JSA</abbr><svg class="govuk-button__start-icon govuk-!-display-none-print" xmlns="http://www.w3.org/2000/svg" width="17.5" height="19" viewbox="0 0 33 40" focusable="false" aria-hidden="true"><path fill="currentColor" d="M0 0h13l20 20-20 20H0l20-20z"></path></svg></a></p>
<p><a class="gem-c-button govuk-button govuk-button--start" role="button" data-module="govuk-button" draggable="false" href="https://www.apply-for-new-style-jsa.dwp.gov.uk/?lang=cy"><span> Start now <abbr title="Jobseeker's Allowance">JSA</abbr></span><svg class="govuk-button__start-icon govuk-!-display-none-print" xmlns="http://www.w3.org/2000/svg" width="17.5" height="19" viewbox="0 0 33 40" focusable="false" aria-hidden="true"><path fill="currentColor" d="M0 0h13l20 20-20 20H0l20-20z"></path></svg></a></p>
)
end
end
98 changes: 60 additions & 38 deletions test/govspeak_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,21 @@ class GovspeakTest < Minitest::Test
)
end

test_given_govspeak "
$CTA
Contact the SGD on 0800 000 0000 or contact the class on 0800 001 0001
$CTA
*[class]: Other Government Department
*[SGD]: Some Government Department
" do
assert_html_output %(
<div class="call-to-action">
<p>Contact the <abbr title="Some Government Department">SGD</abbr> on 0800 000 0000 or contact the <abbr title="Other Government Department">class</abbr> on 0800 001 0001</p>
</div>
)
end

test_given_govspeak "
1. rod
2. jane
Expand Down Expand Up @@ -1090,44 +1105,51 @@ class GovspeakTest < Minitest::Test
)
end

# FIXME: this code is buggy and replaces abbreviations in HTML tags - removing the functionality for now
# test_given_govspeak "
# $LegislativeList
# * 1. Item 1[^1] with an ACRONYM
# * 2. Item 2[^2]
# * 3. Item 3
# $EndLegislativeList
#
# [^1]: Footnote definition one
# [^2]: Footnote definition two with an ACRONYM
#
# *[ACRONYM]: This is the acronym explanation
# " do
# assert_html_output %(
# <ol class="legislative-list">
# <li>1. Item 1<sup id="fnref:1" role="doc-noteref"><a href="#fn:1" class="footnote" rel="footnote">[footnote 1]</a></sup> with an <abbr title="This is the acronym explanation">ACRONYM</abbr>
# </li>
# <li>2. Item 2<sup id="fnref:2" role="doc-noteref"><a href="#fn:2" class="footnote" rel="footnote">[footnote 2]</a></sup>
# </li>
# <li>3. Item 3</li>
# </ol>
#
# <div class="footnotes" role="doc-endnotes">
# <ol>
# <li id="fn:1" role="doc-endnote">
# <p>
# Footnote definition one<a href="#fnref:1" class="reversefootnote" role="doc-backlink" aria-label="go to where this is referenced">↩</a>
# </p>
# </li>
# <li id="fn:2" role="doc-endnote">
# <p>
# Footnote definition two with an <abbr title="This is the acronym explanation">ACRONYM</abbr><a href="#fnref:2" class="reversefootnote" role="doc-backlink" aria-label="go to where this is referenced">↩</a>
# </p>
# </li>
# </ol>
# </div>
# )
# end
test_given_govspeak "
$LegislativeList
* 1. Item 1[^1] with an ACRONYM
* 2. Item 2[^2]
* 3. Item 3[^3]
$EndLegislativeList
[^1]: Footnote definition one
[^2]: Footnote definition two with an ACRONYM
[^3]: Footnote definition three with an acronym that matches an HTML tag class
*[ACRONYM]: This is the acronym explanation
*[class]: Testing HTML matching
" do
assert_html_output %(
<ol class="legislative-list">
<li>1. Item 1<sup id="fnref:1" role="doc-noteref"><a href="#fn:1" class="footnote" rel="footnote">[footnote 1]</a></sup> with an <abbr title="This is the acronym explanation">ACRONYM</abbr>
</li>
<li>2. Item 2<sup id="fnref:2" role="doc-noteref"><a href="#fn:2" class="footnote" rel="footnote">[footnote 2]</a></sup>
</li>
<li>3. Item 3<sup id="fnref:3" role="doc-noteref"><a href="#fn:3" class="footnote" rel="footnote">[footnote 3]</a></sup>
</li>
</ol>
<div class="footnotes" role="doc-endnotes">
<ol>
<li id="fn:1" role="doc-endnote">
<p>
Footnote definition one<a href="#fnref:1" class="reversefootnote" role="doc-backlink" aria-label="go to where this is referenced">↩</a>
</p>
</li>
<li id="fn:2" role="doc-endnote">
<p>
Footnote definition two with an <abbr title="This is the acronym explanation">ACRONYM</abbr><a href="#fnref:2" class="reversefootnote" role="doc-backlink" aria-label="go to where this is referenced">↩</a>
</p>
</li>
<li id="fn:3" role="doc-endnote">
<p>
Footnote definition three with an acronym that matches an HTML tag <abbr title="Testing HTML matching">class</abbr><a href="#fnref:3" class="reversefootnote" role="doc-backlink" aria-label="go to where this is referenced">↩</a>
</p>
</li>
</ol>
</div>
)
end

test_given_govspeak "
The quick brown
Expand Down

0 comments on commit 9ae30e4

Please sign in to comment.