From 4f9a54bff5245c31dd5a9ca21e234d5fd485c1c6 Mon Sep 17 00:00:00 2001 From: Bruce Bolt Date: Mon, 21 Aug 2023 12:04:46 +0100 Subject: [PATCH 1/7] Add test for abbreviations in Call To Action elements This test is currently failing as abbreviations within Call To Action elements do not currently work (as reported by a user). This functionality will be added in later commits. --- test/govspeak_test.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/govspeak_test.rb b/test/govspeak_test.rb index 95be72e2..93976552 100644 --- a/test/govspeak_test.rb +++ b/test/govspeak_test.rb @@ -622,6 +622,21 @@ class GovspeakTest < Minitest::Test ) end + test_given_govspeak " + $CTA + Contact the SGD on 0800 000 0000 or contact the OGD on 0800 001 0001 + $CTA + + *[OGD]: Other Government Department + *[SGD]: Some Government Department + " do + assert_html_output %( +
+

Contact the SGD on 0800 000 0000 or contact the OGD on 0800 001 0001

+
+ ) + end + test_given_govspeak " 1. rod 2. jane From 9ca47bbb018b5fec337ca50193bf22ea89a427f9 Mon Sep 17 00:00:00 2001 From: Bruce Bolt Date: Mon, 21 Aug 2023 12:35:08 +0100 Subject: [PATCH 2/7] Reintroduce test for acronyms in Legislative Lists This test currently fails as the code to support this has been commented out. Work to fix the issue will be done in a later commit. --- test/govspeak_test.rb | 75 +++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 38 deletions(-) diff --git a/test/govspeak_test.rb b/test/govspeak_test.rb index 93976552..7f1b9b14 100644 --- a/test/govspeak_test.rb +++ b/test/govspeak_test.rb @@ -1105,44 +1105,43 @@ 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 %( - #
    - #
  1. 1. Item 1[footnote 1] with an ACRONYM - #
  2. - #
  3. 2. Item 2[footnote 2] - #
  4. - #
  5. 3. Item 3
  6. - #
- # - #
- #
    - #
  1. - #

    - # Footnote definition one - #

    - #
  2. - #
  3. - #

    - # Footnote definition two with an ACRONYM - #

    - #
  4. - #
- #
- # ) - # end + 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 %( +
    +
  1. 1. Item 1[footnote 1] with an ACRONYM +
  2. +
  3. 2. Item 2[footnote 2] +
  4. +
  5. 3. Item 3
  6. +
+ +
+
    +
  1. +

    + Footnote definition one +

    +
  2. +
  3. +

    + Footnote definition two with an ACRONYM +

    +
  4. +
+
+ ) + end test_given_govspeak " The quick brown From fdd5598717db2495916e117e629510103fb5edb1 Mon Sep 17 00:00:00 2001 From: Bruce Bolt Date: Mon, 21 Aug 2023 13:41:13 +0100 Subject: [PATCH 3/7] Add test for previously problematic scenario In a previous Zendesk ticket (https://govuk.zendesk.com/agent/tickets/4819383), a problem was reported where abbreviations within HTML tag elements were being replaced with abbreviation tags. The fix at the time was to revert the code for adding abbreviations to legislative lists and calls to action. This adds a test for the previously problematic scenario, so we can implement the abbreviations functionality again in a later commit. --- test/govspeak_test.rb | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/test/govspeak_test.rb b/test/govspeak_test.rb index 7f1b9b14..b950b514 100644 --- a/test/govspeak_test.rb +++ b/test/govspeak_test.rb @@ -624,15 +624,15 @@ class GovspeakTest < Minitest::Test test_given_govspeak " $CTA - Contact the SGD on 0800 000 0000 or contact the OGD on 0800 001 0001 + Contact the SGD on 0800 000 0000 or contact the class on 0800 001 0001 $CTA - *[OGD]: Other Government Department + *[class]: Other Government Department *[SGD]: Some Government Department " do assert_html_output %(
-

Contact the SGD on 0800 000 0000 or contact the OGD on 0800 001 0001

+

Contact the SGD on 0800 000 0000 or contact the class on 0800 001 0001

) end @@ -1109,13 +1109,15 @@ class GovspeakTest < Minitest::Test $LegislativeList * 1. Item 1[^1] with an ACRONYM * 2. Item 2[^2] - * 3. Item 3 + * 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 %(
    @@ -1123,7 +1125,8 @@ class GovspeakTest < Minitest::Test
  1. 2. Item 2[footnote 2]
  2. -
  3. 3. Item 3
  4. +
  5. 3. Item 3[footnote 3] +
@@ -1137,6 +1140,11 @@ class GovspeakTest < Minitest::Test

Footnote definition two with an ACRONYM

+ +
  • +

    + Footnote definition three with an acronym that matches an HTML tag class +

  • From a0aeaa643f9d88f451c22ceb5fb796c7ef1c4750 Mon Sep 17 00:00:00 2001 From: Bruce Bolt Date: Mon, 21 Aug 2023 12:32:40 +0100 Subject: [PATCH 4/7] Add support for acronyms in Call To Action elements This was previously not working (before the code was commented out) as: - multiple calls to `preprocess` meant `@acronyms` was empty on susbsequent runs (using `concat` solves that) - `add_acronym_alt_text` was returning the output of the `each` method (which was some array) rather than the updated HTML Support for other types of element was already commented out and that behaviour has been retained here. It will be fixed in later commits. --- lib/govspeak.rb | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/govspeak.rb b/lib/govspeak.rb index eb8d87b8..edc56d1e 100644 --- a/lib/govspeak.rb +++ b/lib/govspeak.rb @@ -139,7 +139,7 @@ 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] @@ -164,9 +164,9 @@ def footnote_definitions(source) HTML_CONTAINER end - unless @footnote_definition_html.nil? && @acronyms.size.positive? - add_acronym_alt_text(@footnote_definition_html) - end + # unless @footnote_definition_html.nil? && @acronyms.size.positive? + # add_acronym_alt_text(@footnote_definition_html) + # end end def remove_forbidden_characters(source) @@ -326,6 +326,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
    \n#{doc}
    \n) footnotes = body.scan(/\[\^(\d+)\]/).flatten @@ -337,7 +338,6 @@ def render_image(image) doc.sub!(/(\[\^#{footnote}\])/, html) end - add_acronym_alt_text(doc) if @acronyms.size.positive? doc end @@ -373,7 +373,7 @@ def render_image(image) doc.sub!(/(\[\^#{footnote}\])/, html) end - add_acronym_alt_text(doc) if @acronyms.size.positive? + # add_acronym_alt_text(doc) if @acronyms.size.positive? end end end @@ -454,10 +454,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], "#{acronym[0]}") - # end + @acronyms.each do |acronym| + html.gsub!(acronym[0], "#{acronym[0]}") + end + + html end end end From 7daa93c777ff6a0f2c597a78675547ded6d2fc72 Mon Sep 17 00:00:00 2001 From: Bruce Bolt Date: Mon, 21 Aug 2023 13:25:19 +0100 Subject: [PATCH 5/7] Add support for acronyms in Legislative Lists This code was previously removed as it was considered unreliable as acronyms within HTML tag attributes were also being replaced. Adding it back in, but now parsing the acronyms before converting to HTML. --- lib/govspeak.rb | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/lib/govspeak.rb b/lib/govspeak.rb index edc56d1e..4308f7ad 100644 --- a/lib/govspeak.rb +++ b/lib/govspeak.rb @@ -145,6 +145,7 @@ def footnote_definitions(source) number = footnote[0] text = footnote[1].strip footnote_definition = Govspeak::Document.new(text).to_html[/(?<=

    ).*(?=<\/p>)/] + footnote_definition = add_acronym_alt_text(footnote_definition) <<~HTML_SNIPPET

  • @@ -163,10 +164,6 @@ def footnote_definitions(source) 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) @@ -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!("
      ", "
        ") doc.gsub!("
    ", "") @@ -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 From ed07d0bc260f38934ac10d389cefc7e74369833c Mon Sep 17 00:00:00 2001 From: Bruce Bolt Date: Tue, 22 Aug 2023 11:36:45 +0100 Subject: [PATCH 6/7] Update test to match change in `govuk_publishing_components` This adds the `span` that was added to buttons in https://github.com/alphagov/govuk_publishing_components/pull/3478. --- test/govspeak_button_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/govspeak_button_test.rb b/test/govspeak_button_test.rb index fe4be57a..991f72b1 100644 --- a/test/govspeak_button_test.rb +++ b/test/govspeak_button_test.rb @@ -118,7 +118,7 @@ class GovspeakTest < Minitest::Test " do assert_text_output "Start now JSA" assert_html_output %( -

    Start now JSA

    +

    Start now JSA

    ) end end From 442b0a76d8fa876e248c8ee7df8de224c90f41d5 Mon Sep 17 00:00:00 2001 From: Bruce Bolt Date: Tue, 22 Aug 2023 16:05:12 +0100 Subject: [PATCH 7/7] Update CHANGELOG for addition of acronym feature --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 42252623..be7f0d3a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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))