govspeak 10.2.5 → 10.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 885072211496e1f2cce10e8fe3ff9418575cd9952bb946071ccb79ed2f8b69c7
4
- data.tar.gz: 41e668ae971c69d4f95d3c27634eabfd08e0f2803dfb20c2b5219ef5b528beba
3
+ metadata.gz: 5a3bf8e18b1cfd9bb3c71bf3774e9232c0c3defa120fd60f983fa9acf47c0b51
4
+ data.tar.gz: 813f4543fa558f0394e29f06afcc52b6e95ed10714b25ae02607c7f185f78a8b
5
5
  SHA512:
6
- metadata.gz: 684d975fc93d6618e37d760d89b92756a11cd1817fea10bda56ffb8e9d4a82c608fa19a877c2b809f2aa932b9b780aa1c7fd5e4bb588a4f72e456c450f1f5f3a
7
- data.tar.gz: 1973391e3ff9ca6337f0cca4a817625b61911d3e5275c060e8181576cc9f617780a80210f6f31ded094af2ae9002a2ffacf28271de57984fe8c2c4a60f0af3a7
6
+ metadata.gz: 4aa85cc0a63094caaf65aef58851ece00cf9dd3cf32d2ff8c876758a07fb7d139ac7dc19a9b3446b73b8d31a8bef137c4b3b0e41ba1079223c4530fe95e212cf
7
+ data.tar.gz: ccaa75526e6f9e947f1a2409156a60ca16d904b29c1a877ce8b84264ea2c655dd2a6991c2b992608d992ee26bb0ceb2189e1bc6b12f698091b9956b154f1ac25
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
2
 
3
+ ## 10.4.0
4
+
5
+ * Support abbreviations in informationals
6
+
7
+ ## 10.3.0
8
+
9
+ * Add Welsh translations for various components: devolved content headings, footnote labels, and image credit labels ([#417](https://github.com/alphagov/govspeak/pull/417))
10
+
3
11
  ## 10.2.5
4
12
 
5
13
  * Update dependencies
data/README.md CHANGED
@@ -33,7 +33,17 @@ Also, consider if:
33
33
  Any pages that use govspeak to generate Content will need to *republished* in order for the new changes to be reflected.
34
34
 
35
35
  - Data Labs can help identify which pages need updating by [submitting a request](https://gov-uk.atlassian.net/wiki/spaces/GOVUK/pages/1860075525/GOV.UK+Data+Labs#Submitting-a-data-science-request) and [#govuk-2ndline](https://docs.publishing.service.gov.uk/manual/2nd-line.html) can help with republishing
36
-
36
+
37
+ ## Debugging
38
+
39
+ If `log_snapshots: true` is provided as an option when initialising a
40
+ `Govspeak::Document`, a few useful snapshots will be provided. This can help
41
+ with understanding how the content transforms from source through:
42
+
43
+ - preprocessing (our main Govspeak rendering point);
44
+ - Kramdown processing (the Markdown rendering point); and
45
+ - postprocessing (an additional Govspeak rendering point).
46
+
37
47
  # Extensions
38
48
 
39
49
  In addition to the [standard Markdown syntax](http://daringfireball.net/projects/markdown/syntax "Markdown syntax"), we have added our own extensions.
@@ -150,11 +150,13 @@ module Govspeak
150
150
  extension("use custom footnotes") do |document|
151
151
  document.css("a.footnote").map do |el|
152
152
  footnote_number = el[:href].gsub(/\D/, "")
153
- el.inner_html = "[footnote #{footnote_number}]"
153
+ label = I18n.t("govspeak.footnote.label", locale: govspeak_document.locale)
154
+ el.inner_html = "[#{label} #{footnote_number}]"
154
155
  end
155
156
  document.css("[role='doc-backlink']").map do |el|
156
157
  backlink_number = " #{el.css('sup')[0].content}" if el.css("sup")[0].present?
157
- el["aria-label"] = "go to where this is referenced#{backlink_number}"
158
+ aria_label = I18n.t("govspeak.footnote.backlink_aria_label", locale: govspeak_document.locale)
159
+ el["aria-label"] = "#{aria_label}#{backlink_number}"
158
160
  end
159
161
  end
160
162
 
@@ -1,9 +1,10 @@
1
1
  module Govspeak
2
2
  class ImagePresenter
3
- attr_reader :image
3
+ attr_reader :image, :locale
4
4
 
5
- def initialize(image)
5
+ def initialize(image, locale: "en")
6
6
  @image = image
7
+ @locale = locale
7
8
  end
8
9
 
9
10
  def url
@@ -34,7 +35,7 @@ module Govspeak
34
35
  lines = []
35
36
  lines << "<figcaption>"
36
37
  lines << %(<p>#{caption}</p>) if caption.present?
37
- lines << %(<p>#{I18n.t('govspeak.image.figure.credit', credit:)}</p>) if credit.present?
38
+ lines << %(<p>#{I18n.t('govspeak.image.figure.credit', credit:, locale:)}</p>) if credit.present?
38
39
  lines << "</figcaption>"
39
40
  lines.join
40
41
  end
@@ -1,3 +1,3 @@
1
1
  module Govspeak
2
- VERSION = "10.2.5".freeze
2
+ VERSION = "10.4.0".freeze
3
3
  end
data/lib/govspeak.rb CHANGED
@@ -37,7 +37,7 @@ module Govspeak
37
37
  @extensions = []
38
38
 
39
39
  attr_accessor :images
40
- attr_reader :attachments, :contacts, :links, :locale
40
+ attr_reader :attachments, :contacts, :links, :locale, :log_snapshots
41
41
 
42
42
  def self.to_html(source, options = {})
43
43
  new(source, options).to_html
@@ -51,6 +51,10 @@ module Govspeak
51
51
  options = options.dup.deep_symbolize_keys
52
52
  @source = source ? source.dup : ""
53
53
 
54
+ @log_snapshots = options.fetch(:log_snapshots, false)
55
+ log_snapshot("options", options)
56
+ log_snapshot("source", @source)
57
+
54
58
  @images = options.delete(:images) || []
55
59
  @allowed_elements = options.delete(:allowed_elements) || []
56
60
  @allowed_image_hosts = options.delete(:allowed_image_hosts) || []
@@ -73,7 +77,11 @@ module Govspeak
73
77
  kramdown_doc.to_html
74
78
  end
75
79
 
76
- Govspeak::PostProcessor.process(html, self)
80
+ log_snapshot("after Kramdown process", html)
81
+
82
+ Govspeak::PostProcessor.process(html, self).tap do
83
+ log_snapshot("after postprocess", _1)
84
+ end
77
85
  end
78
86
  end
79
87
 
@@ -117,7 +125,8 @@ module Govspeak
117
125
  instance_exec(*Regexp.last_match.captures, &block)
118
126
  end
119
127
  end
120
- source
128
+
129
+ source.tap { log_snapshot("after preprocess", _1) }
121
130
  end
122
131
 
123
132
  def remove_forbidden_characters(source)
@@ -197,8 +206,11 @@ module Govspeak
197
206
  end
198
207
 
199
208
  extension("informational", surrounded_by("^")) do |body|
200
- %(\n\n<div role="note" aria-label="Information" class="application-notice info-notice">
201
- #{Govspeak::Document.new(body.strip).to_html}</div>\n)
209
+ <<~BODY
210
+ \n\n<div role="note" aria-label="Information" class="application-notice info-notice" markdown="1">
211
+ #{body.strip}
212
+ </div>
213
+ BODY
202
214
  end
203
215
 
204
216
  extension("helpful", surrounded_by("%")) do |body|
@@ -225,7 +237,7 @@ module Govspeak
225
237
  image = images[image_number.to_i - 1]
226
238
  next "" unless image
227
239
 
228
- render_image(ImagePresenter.new(image))
240
+ render_image(ImagePresenter.new(image, locale:))
229
241
  end
230
242
 
231
243
  # DEPRECATED: use 'AttachmentLink:attachment-id' instead
@@ -332,20 +344,18 @@ module Govspeak
332
344
  BODY
333
345
  end
334
346
 
335
- def self.devolved_options
336
- { "scotland" => "Scotland",
337
- "england" => "England",
338
- "england-wales" => "England and Wales",
339
- "northern-ireland" => "Northern Ireland",
340
- "wales" => "Wales",
341
- "london" => "London" }
342
- end
347
+ %w[scotland
348
+ england
349
+ england-wales
350
+ northern-ireland
351
+ wales
352
+ london].each do |devolved_option|
353
+ extension("devolved-#{devolved_option}", /:#{devolved_option}:(.*?):#{devolved_option}:/m) do |body|
354
+ header_content = I18n.t("govspeak.devolved.#{devolved_option}", locale:)
343
355
 
344
- devolved_options.each do |k, v|
345
- extension("devolved-#{k}", /:#{k}:(.*?):#{k}:/m) do |body|
346
356
  <<~HTML
347
- <div class="devolved-content #{k}">
348
- <p class="devolved-header">This section applies to #{v}</p>
357
+ <div class="devolved-content #{devolved_option}">
358
+ <p class="devolved-header">#{header_content}</p>
349
359
  <div class="devolved-body">#{Govspeak::Document.new(body.strip).to_html}</div>
350
360
  </div>
351
361
  HTML
@@ -375,7 +385,7 @@ module Govspeak
375
385
  image = images.detect { |c| c.is_a?(Hash) && c[:id] == image_id }
376
386
  next "" unless image
377
387
 
378
- render_image(ImagePresenter.new(image))
388
+ render_image(ImagePresenter.new(image, locale:))
379
389
  end
380
390
 
381
391
  extension("Attachment", /^\[Attachment:\s*(.*?)\s*\]/) do |attachment_id|
@@ -392,6 +402,13 @@ module Govspeak
392
402
 
393
403
  private
394
404
 
405
+ def log_snapshot(description, *objects)
406
+ return unless log_snapshots
407
+
408
+ puts "===== #{description} ====="
409
+ !objects.empty? && objects.each { puts _1 }
410
+ end
411
+
395
412
  def kramdown_doc
396
413
  @kramdown_doc ||= Kramdown::Document.new(preprocess(@source), @options)
397
414
  end
data/locales/cy.yml CHANGED
@@ -4,3 +4,16 @@ cy:
4
4
  contact:
5
5
  contact_form: Ffurflen cysylltu
6
6
  email: E-bost
7
+ devolved:
8
+ scotland: Mae'r adran hon yn berthnasol i'r Alban
9
+ england: Mae'r adran hon yn berthnasol i Loegr
10
+ england-wales: Mae'r adran hon yn berthnasol i Gymru a Lloegr
11
+ northern-ireland: Mae'r adran hon yn berthnasol i Ogledd Iwerddon
12
+ wales: Mae'r adran hon yn berthnasol i Gymru
13
+ london: Mae'r adran hon yn berthnasol i Lundain
14
+ footnote:
15
+ label: troednodyn
16
+ backlink_aria_label: ewch i ble mae hyn wedi'i gyfeirio
17
+ image:
18
+ figure:
19
+ credit: "Credyd delwedd: %{credit}"
data/locales/en.yml CHANGED
@@ -4,6 +4,16 @@ en:
4
4
  contact:
5
5
  email: Email
6
6
  contact_form: Contact form
7
+ devolved:
8
+ scotland: This section applies to Scotland
9
+ england: This section applies to England
10
+ england-wales: This section applies to England and Wales
11
+ northern-ireland: This section applies to Northern Ireland
12
+ wales: This section applies to Wales
13
+ london: This section applies to London
14
+ footnote:
15
+ label: footnote
16
+ backlink_aria_label: go to where this is referenced
7
17
  image:
8
18
  figure:
9
19
  credit: "Image credit: %{credit}"
@@ -198,4 +198,22 @@ class GovspeakContactsTest < Minitest::Test
198
198
  assert_match(%(<p class="comments">My description about <a href="https://www.gov.uk">https://www.gov.uk</a></p>),
199
199
  compress_html(rendered))
200
200
  end
201
+
202
+ test "uses localised email label when locale is not English" do
203
+ contact = build_contact
204
+ govspeak = "[Contact:4f3383e4-48a2-4461-a41d-f85ea8b89ba0]"
205
+
206
+ rendered = Govspeak::Document.new(govspeak, contacts: [contact], locale: "cy").to_html
207
+ assert_match(%(<p class="email"><span class="type">E-bost</span>),
208
+ compress_html(rendered))
209
+ end
210
+
211
+ test "uses localised contact form label when locale is not English" do
212
+ contact = build_contact
213
+ govspeak = "[Contact:4f3383e4-48a2-4461-a41d-f85ea8b89ba0]"
214
+
215
+ rendered = Govspeak::Document.new(govspeak, contacts: [contact], locale: "cy").to_html
216
+ assert_match(%(<p class="contact_form_url"><span class="type">Ffurflen cysylltu</span>),
217
+ compress_html(rendered))
218
+ end
201
219
  end
@@ -0,0 +1,21 @@
1
+ require "test_helper"
2
+ require "govspeak_test_helper"
3
+
4
+ class GovspeakDevolvedTest < Minitest::Test
5
+ include GovspeakTestHelper
6
+
7
+ test "uses localised heading when locale is not English" do
8
+ given_govspeak ":scotland: Rwy'n ddatganoledig iawn\n ac yn Albanaidd iawn \n:scotland:", locale: "cy" do
9
+ assert_html_output(
10
+ %(
11
+ <div class="devolved-content scotland">
12
+ <p class="devolved-header">Mae'r adran hon yn berthnasol i'r Alban</p>
13
+ <div class="devolved-body">
14
+ <p>Rwy’n ddatganoledig iawn
15
+ ac yn Albanaidd iawn</p>
16
+ </div>
17
+ </div>),
18
+ )
19
+ end
20
+ end
21
+ end
@@ -43,4 +43,24 @@ class GovspeakFootnoteTest < Minitest::Test
43
43
  </div>),
44
44
  )
45
45
  end
46
+
47
+ test "uses localised labels when locale is not English" do
48
+ given_govspeak "
49
+ Gellir ychwanegu troednodiadau[^1].
50
+
51
+ [^1]: Ac yna wedi'i ddiffinio'n ddiweddarach.", locale: "cy" do
52
+ assert_html_output(
53
+ %(
54
+ <p>Gellir ychwanegu troednodiadau<sup id="fnref:1"><a href="#fn:1" class="footnote" rel="footnote" role="doc-noteref">[troednodyn 1]</a></sup>.</p>
55
+
56
+ <div class="footnotes" role="doc-endnotes">
57
+ <ol>
58
+ <li id="fn:1">
59
+ <p>Ac yna wedi’i ddiffinio’n ddiweddarach. <a href="#fnref:1" class="reversefootnote" role="doc-backlink" aria-label="ewch i ble mae hyn wedi'i gyfeirio">↩</a></p>
60
+ </li>
61
+ </ol>
62
+ </div>),
63
+ )
64
+ end
65
+ end
46
66
  end
@@ -49,6 +49,14 @@ class GovspeakImagesTest < Minitest::Test
49
49
  end
50
50
  end
51
51
 
52
+ test "Image:image-id syntax adds image credit with localised label when locale is not English" do
53
+ given_govspeak "[Image:image-id]", { images: [build_image(credit: "Fy Nghredyd ac ati")], locale: "cy" } do
54
+ assert_html_output(
55
+ "<figure class=\"image embedded\"><div class=\"img\"><img src=\"http://example.com/image.jpg\" alt=\"my alt\"></div>\n<figcaption><p>Credyd delwedd: Fy Nghredyd ac ati</p></figcaption></figure>",
56
+ )
57
+ end
58
+ end
59
+
52
60
  test "Image:image-id syntax ignores a blank credit" do
53
61
  given_govspeak "[Image:image-id]", images: [build_image(credit: " ")] do
54
62
  assert_html_output(
@@ -0,0 +1,100 @@
1
+ require "test_helper"
2
+ require "govspeak_test_helper"
3
+
4
+ class GovspeakImagesTest < Minitest::Test
5
+ include GovspeakTestHelper
6
+ extend Minitest::Spec::DSL
7
+
8
+ test "renders okay with just an opening tag" do
9
+ given_govspeak "^ I am very informational" do
10
+ assert_html_output %(
11
+ <div role="note" aria-label="Information" class="application-notice info-notice">
12
+ <p>I am very informational</p>
13
+ </div>)
14
+ assert_text_output "I am very informational"
15
+ end
16
+ end
17
+
18
+ test "renders okay with opening and closing tags" do
19
+ given_govspeak("^ I am very informational ^") do
20
+ assert_html_output %(
21
+ <div role="note" aria-label="Information" class="application-notice info-notice">
22
+ <p>I am very informational</p>
23
+ </div>)
24
+ assert_text_output "I am very informational"
25
+ end
26
+ end
27
+
28
+ test "renders okay with no space after the opening tag" do
29
+ given_govspeak("^I am very informational") do
30
+ assert_html_output %(
31
+ <div role="note" aria-label="Information" class="application-notice info-notice">
32
+ <p>I am very informational</p>
33
+ </div>)
34
+ assert_text_output "I am very informational"
35
+ end
36
+ end
37
+
38
+ test "avoids combining into a single block with an immediately-preceding line" do
39
+ given_govspeak "The following is very informational\n^ I am very informational ^" do
40
+ assert_html_output %(
41
+ <p>The following is very informational</p>
42
+
43
+ <div role="note" aria-label="Information" class="application-notice info-notice">
44
+ <p>I am very informational</p>
45
+ </div>)
46
+ assert_text_output "The following is very informational I am very informational"
47
+ end
48
+ end
49
+
50
+ describe "support for nested Kramdown" do
51
+ test "supports headings" do
52
+ given_govspeak "^ ## I am an informational heading" do
53
+ assert_html_output %(
54
+ <div role="note" aria-label="Information" class="application-notice info-notice">
55
+ <h2 id="i-am-an-informational-heading">I am an informational heading</h2>
56
+ </div>)
57
+ assert_text_output "I am an informational heading"
58
+ end
59
+ end
60
+
61
+ test "supports abbreviations" do
62
+ given_govspeak "^ I am very informational
63
+
64
+ Live, love, informational
65
+
66
+ *[informational]: do with it what you will
67
+ " do
68
+ assert_html_output %(
69
+ <div role="note" aria-label="Information" class="application-notice info-notice">
70
+ <p>I am very <abbr title="do with it what you will">informational</abbr></p>
71
+ </div>
72
+
73
+ <p>Live, love, <abbr title="do with it what you will">informational</abbr></p>)
74
+ assert_text_output "I am very informational Live, love, informational"
75
+ end
76
+ end
77
+
78
+ test "supports links" do
79
+ given_govspeak "^ [My informational link](https://www.gov.uk)" do
80
+ assert_html_output %(
81
+ <div role="note" aria-label="Information" class="application-notice info-notice">
82
+ <p><a href="https://www.gov.uk">My informational link</a></p>
83
+ </div>
84
+ )
85
+ assert_text_output "My informational link"
86
+ end
87
+ end
88
+
89
+ test "supports bold emphasis" do
90
+ given_govspeak "^ I am **very** informational" do
91
+ assert_html_output %(
92
+ <div role="note" aria-label="Information" class="application-notice info-notice">
93
+ <p>I am <strong>very</strong> informational</p>
94
+ </div>
95
+ )
96
+ assert_text_output "I am very informational"
97
+ end
98
+ end
99
+ end
100
+ end
@@ -177,38 +177,12 @@ Teston
177
177
  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
178
178
  end
179
179
 
180
- test_given_govspeak("^ I am very informational ^") do
181
- assert_html_output %(
182
- <div role="note" aria-label="Information" class="application-notice info-notice">
183
- <p>I am very informational</p>
184
- </div>)
185
- assert_text_output "I am very informational"
186
- end
187
-
188
180
  test "processing an extension does not modify the provided input" do
189
181
  input = "^ I am very informational"
190
182
  Govspeak::Document.new(input).to_html
191
183
  assert_equal "^ I am very informational", input
192
184
  end
193
185
 
194
- test_given_govspeak "The following is very informational\n^ I am very informational ^" do
195
- assert_html_output %(
196
- <p>The following is very informational</p>
197
-
198
- <div role="note" aria-label="Information" class="application-notice info-notice">
199
- <p>I am very informational</p>
200
- </div>)
201
- assert_text_output "The following is very informational I am very informational"
202
- end
203
-
204
- test_given_govspeak "^ I am very informational" do
205
- assert_html_output %(
206
- <div role="note" aria-label="Information" class="application-notice info-notice">
207
- <p>I am very informational</p>
208
- </div>)
209
- assert_text_output "I am very informational"
210
- end
211
-
212
186
  test_given_govspeak "% I am very helpful %" do
213
187
  assert_html_output %(
214
188
  <div role="note" aria-label="Warning" class="application-notice help-notice">
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: govspeak
3
3
  version: !ruby/object:Gem::Version
4
- version: 10.2.5
4
+ version: 10.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - GOV.UK Dev
@@ -213,14 +213,14 @@ dependencies:
213
213
  requirements:
214
214
  - - '='
215
215
  - !ruby/object:Gem::Version
216
- version: 5.1.5
216
+ version: 5.1.15
217
217
  type: :development
218
218
  prerelease: false
219
219
  version_requirements: !ruby/object:Gem::Requirement
220
220
  requirements:
221
221
  - - '='
222
222
  - !ruby/object:Gem::Version
223
- version: 5.1.5
223
+ version: 5.1.15
224
224
  - !ruby/object:Gem::Dependency
225
225
  name: simplecov
226
226
  requirement: !ruby/object:Gem::Requirement
@@ -317,10 +317,12 @@ files:
317
317
  - test/govspeak_attachments_inline_test.rb
318
318
  - test/govspeak_button_test.rb
319
319
  - test/govspeak_contacts_test.rb
320
+ - test/govspeak_devolved_content_test.rb
320
321
  - test/govspeak_extract_contact_content_ids_test.rb
321
322
  - test/govspeak_footnote_test.rb
322
323
  - test/govspeak_images_bang_test.rb
323
324
  - test/govspeak_images_test.rb
325
+ - test/govspeak_informational_test.rb
324
326
  - test/govspeak_link_extractor_test.rb
325
327
  - test/govspeak_link_test.rb
326
328
  - test/govspeak_structured_headers_test.rb
@@ -349,7 +351,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
349
351
  - !ruby/object:Gem::Version
350
352
  version: '0'
351
353
  requirements: []
352
- rubygems_version: 3.6.8
354
+ rubygems_version: 3.6.9
353
355
  specification_version: 4
354
356
  summary: Markup language for single domain
355
357
  test_files:
@@ -360,10 +362,12 @@ test_files:
360
362
  - test/govspeak_attachments_inline_test.rb
361
363
  - test/govspeak_button_test.rb
362
364
  - test/govspeak_contacts_test.rb
365
+ - test/govspeak_devolved_content_test.rb
363
366
  - test/govspeak_extract_contact_content_ids_test.rb
364
367
  - test/govspeak_footnote_test.rb
365
368
  - test/govspeak_images_bang_test.rb
366
369
  - test/govspeak_images_test.rb
370
+ - test/govspeak_informational_test.rb
367
371
  - test/govspeak_link_extractor_test.rb
368
372
  - test/govspeak_link_test.rb
369
373
  - test/govspeak_structured_headers_test.rb