Skip to content

Commit

Permalink
Add support for acronyms in Call To Action elements
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
brucebolt committed Aug 21, 2023
1 parent fdd5598 commit a0aeaa6
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions lib/govspeak.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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)
Expand Down Expand Up @@ -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<div class="call-to-action">\n#{doc}</div>\n)
footnotes = body.scan(/\[\^(\d+)\]/).flatten

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

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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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], "<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

0 comments on commit a0aeaa6

Please sign in to comment.