govspeak 6.0.0 → 6.1.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.
Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +8 -0
  3. data/README.md +1 -45
  4. data/lib/govspeak.rb +18 -8
  5. data/lib/govspeak/blockquote_extra_quote_remover.rb +4 -3
  6. data/lib/govspeak/html_sanitizer.rb +1 -1
  7. data/lib/govspeak/post_processor.rb +25 -3
  8. data/lib/govspeak/presenters/attachment_presenter.rb +7 -194
  9. data/lib/govspeak/version.rb +1 -1
  10. data/locales/ar.yml +0 -17
  11. data/locales/be.yml +0 -17
  12. data/locales/bg.yml +0 -19
  13. data/locales/cs.yml +0 -19
  14. data/locales/cy.yml +0 -18
  15. data/locales/de.yml +0 -18
  16. data/locales/el.yml +0 -19
  17. data/locales/en.yml +0 -20
  18. data/locales/es-419.yml +0 -18
  19. data/locales/es.yml +0 -18
  20. data/locales/et.yml +0 -19
  21. data/locales/fa.yml +0 -17
  22. data/locales/fr.yml +0 -18
  23. data/locales/he.yml +0 -18
  24. data/locales/hi.yml +0 -18
  25. data/locales/hu.yml +0 -18
  26. data/locales/hy.yml +0 -18
  27. data/locales/id.yml +0 -17
  28. data/locales/it.yml +0 -18
  29. data/locales/ja.yml +0 -16
  30. data/locales/ko.yml +0 -16
  31. data/locales/lt.yml +0 -17
  32. data/locales/lv.yml +0 -17
  33. data/locales/pl.yml +0 -19
  34. data/locales/ps.yml +0 -17
  35. data/locales/pt.yml +0 -17
  36. data/locales/ro.yml +0 -19
  37. data/locales/ru.yml +0 -16
  38. data/locales/si.yml +0 -17
  39. data/locales/sr.yml +0 -18
  40. data/locales/ta.yml +0 -19
  41. data/locales/th.yml +0 -16
  42. data/locales/tr.yml +0 -17
  43. data/locales/uk.yml +0 -17
  44. data/locales/ur.yml +0 -18
  45. data/locales/uz.yml +0 -17
  46. data/locales/vi.yml +0 -17
  47. data/locales/zh-hk.yml +0 -15
  48. data/locales/zh-tw.yml +0 -15
  49. data/locales/zh.yml +0 -15
  50. data/test/blockquote_extra_quote_remover_test.rb +7 -0
  51. data/test/govspeak_attachment_link_test.rb +25 -0
  52. data/test/govspeak_attachment_test.rb +40 -0
  53. metadata +33 -35
  54. data/lib/templates/attachment.html.erb +0 -57
  55. data/locales/dr.yml +0 -20
  56. data/locales/so.yml +0 -22
  57. data/locales/sq.yml +0 -21
  58. data/test/govspeak_attachments_test.rb +0 -358
@@ -1,358 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- require 'test_helper'
4
-
5
- class GovspeakAttachmentTest < Minitest::Test
6
- def build_attachment(args = {})
7
- {
8
- content_id: "2b4d92f3-f8cd-4284-aaaa-25b3a640d26c",
9
- id: 456,
10
- url: "http://example.com/attachment.pdf",
11
- title: "Attachment Title",
12
- }.merge(args)
13
- end
14
-
15
- def compress_html(html)
16
- html.gsub(/[\n\r]+[\s]*/, '')
17
- end
18
-
19
- def render_govspeak(govspeak, attachments = [], options = {})
20
- options = options.merge(attachments: attachments)
21
- Govspeak::Document.new(govspeak, options).to_html
22
- end
23
-
24
- test "wraps an attachment in a section.attachment.embedded" do
25
- rendered = render_govspeak(
26
- "[embed:attachments:3ed2]",
27
- [build_attachment(content_id: "3ed2")]
28
- )
29
- assert_match(/<section class="attachment embedded">/, rendered)
30
- end
31
-
32
- test "can convert an attachment with spaces" do
33
- rendered = render_govspeak(
34
- "[embed:attachments: 3ed2 ]",
35
- [build_attachment(content_id: "3ed2")]
36
- )
37
- assert_match(/<section class="attachment embedded">/, rendered)
38
- end
39
-
40
- test "wraps an external attachment in a section.attachment.hosted-externally" do
41
- rendered = render_govspeak(
42
- "[embed:attachments:3ed2]",
43
- [build_attachment(content_id: "3ed2", external?: true)]
44
- )
45
- assert_match(/<section class="attachment hosted-externally">/, rendered)
46
- end
47
-
48
- test "outputs a pub-cover.png thumbnail by default" do
49
- rendered = render_govspeak(
50
- "[embed:attachments:3ed2]",
51
- [build_attachment(content_id: "3ed2")]
52
- )
53
- assert_match(%r{<img src="/images/pub-cover.png"}, rendered)
54
- end
55
-
56
- test "outputs a specified thumbnail for a pdf with a thumbnail_url" do
57
- rendered = render_govspeak(
58
- "[embed:attachments:3ed2]",
59
- [build_attachment(content_id: "3ed2", file_extension: "pdf", thumbnail_url: "http://a.b/custom.png")]
60
- )
61
- assert_match(%r{<img src="http://a.b/custom.png"}, rendered)
62
- end
63
-
64
- test "outputs pub-cover.png for a pdf without thumbnail_url" do
65
- rendered = render_govspeak(
66
- "[embed:attachments:3ed2]",
67
- [build_attachment(content_id: "3ed2", file_extension: "pdf", thumbnail_url: nil)]
68
- )
69
- assert_match(%r{<img src="/images/pub-cover.png"}, rendered)
70
- end
71
-
72
- test "outputs pub-cover-html.png for a file with html file_extension" do
73
- rendered = render_govspeak(
74
- "[embed:attachments:3ed2]",
75
- [build_attachment(content_id: "3ed2", file_extension: "html")]
76
- )
77
- assert_match(%r{<img src="/images/pub-cover-html.png"}, rendered)
78
- end
79
-
80
- test "outputs pub-cover-doc.png for a file with docx file_extension" do
81
- rendered = render_govspeak(
82
- "[embed:attachments:3ed2]",
83
- [build_attachment(content_id: "3ed2", file_extension: "docx")]
84
- )
85
- assert_match(%r{<img src="/images/pub-cover-doc.png"}, rendered)
86
- end
87
-
88
- test "outputs pub-cover-spreadsheet.png for a file with xls file_extension" do
89
- rendered = render_govspeak(
90
- "[embed:attachments:3ed2]",
91
- [build_attachment(content_id: "3ed2", file_extension: "xls")]
92
- )
93
- assert_match(%r{<img src="/images/pub-cover-spreadsheet.png"}, rendered)
94
- end
95
-
96
- test "outputs no thumbnail for a previewable file" do
97
- rendered = render_govspeak(
98
- "[embed:attachments:3ed2]",
99
- [build_attachment(content_id: "3ed2", file_extension: "csv")]
100
- )
101
- assert_match(%r{<div class="attachment-thumb"></div>}, compress_html(rendered))
102
- end
103
-
104
- test "outputs a title link within a h2" do
105
- rendered = render_govspeak(
106
- "[embed:attachments:3ed2]",
107
- [build_attachment(content_id: "3ed2", id: 1, url: "http://a.b/c.pdf", title: "Attachment Title")]
108
- )
109
- assert_match(%r{<h2 class="title">\s*<a href="http://a.b/c.pdf" aria-describedby="attachment-1-accessibility-help">Attachment Title</a></h2>}, compress_html(rendered))
110
- end
111
-
112
- test "title link has rel='external' for an external link" do
113
- rendered = render_govspeak(
114
- "[embed:attachments:3ed2]",
115
- [build_attachment(content_id: "3ed2", id: 1, url: "http://a.b/c.pdf", external?: true)]
116
- )
117
- assert_match(%r{<a href="http://a.b/c.pdf" rel="external" aria-describedby="attachment-1-accessibility-help">}, rendered)
118
- end
119
-
120
- test "accessible attachment doesn't have the aria-describedby attribute" do
121
- rendered = render_govspeak(
122
- "[embed:attachments:3ed2]",
123
- [build_attachment(content_id: "3ed2", url: "http://a.b/c.pdf", accessible?: true)]
124
- )
125
- assert_match(%r{<a href="http://a.b/c.pdf">}, rendered)
126
- end
127
-
128
- test "outputs reference if isbn is present" do
129
- rendered = render_govspeak(
130
- "[embed:attachments:3ed2]",
131
- [build_attachment(content_id: "3ed2", isbn: "123")]
132
- )
133
- assert_match(%r{<span class="references">Ref: ISBN <span class="isbn">123</span></span>}, rendered)
134
- end
135
-
136
- test "outputs reference if uniuque_reference is present" do
137
- rendered = render_govspeak(
138
- "[embed:attachments:3ed2]",
139
- [build_attachment(content_id: "3ed2", unique_reference: "123")]
140
- )
141
- assert_match(%r{<span class="references">Ref: <span class="unique_reference">123</span></span>}, rendered)
142
- end
143
-
144
- test "outputs reference if command_paper_number is present" do
145
- rendered = render_govspeak(
146
- "[embed:attachments:3ed2]",
147
- [build_attachment(content_id: "3ed2", command_paper_number: "11")]
148
- )
149
- assert_match(%r{<span class="references">Ref: <span class="command_paper_number">11</span></span>}, rendered)
150
- end
151
-
152
- test "outputs reference if hoc_paper_number is present" do
153
- rendered = render_govspeak(
154
- "[embed:attachments:3ed2]",
155
- [build_attachment(content_id: "3ed2", hoc_paper_number: "15", parliamentary_session: "1")]
156
- )
157
- assert_match(%r{<span class="references">Ref: <span class="house_of_commons_paper_number">HC 15</span> <span class="parliamentary_session">1</span></span>}, rendered)
158
- end
159
-
160
- test "can have multiple references" do
161
- rendered = render_govspeak(
162
- "[embed:attachments:3ed2]",
163
- [build_attachment(content_id: "3ed2", isbn: "123", unique_reference: "55")]
164
- )
165
- assert_match(%r{<span class="references">Ref: ISBN <span class="isbn">123</span>, <span class="unique_reference">55</span></span>}, rendered)
166
- end
167
-
168
- test "can show an unnumbered command paper" do
169
- rendered = render_govspeak(
170
- "[embed:attachments:3ed2]",
171
- [build_attachment(content_id: "3ed2", unnumbered_command_paper?: true)]
172
- )
173
- assert_match(%r{<span class="unnumbered-paper">\s*Unnumbered command paper\s*</span>}, compress_html(rendered))
174
- end
175
-
176
- test "can show an unnumbered act paper" do
177
- rendered = render_govspeak(
178
- "[embed:attachments:3ed2]",
179
- [build_attachment(content_id: "3ed2", unnumbered_hoc_paper?: true)]
180
- )
181
- assert_match(%r{<span class="unnumbered-paper">\s*Unnumbered act paper\s*</span>}, compress_html(rendered))
182
- end
183
-
184
- test "unnumbered command paper takes precedence to unnumbered act paper" do
185
- rendered = render_govspeak(
186
- "[embed:attachments:3ed2]",
187
- [build_attachment(content_id: "3ed2", unnumbered_command_paper?: true, unnumbered_hoc_paper?: true)]
188
- )
189
- assert_match(%r{<span class="unnumbered-paper">\s*Unnumbered command paper\s*</span>}, compress_html(rendered))
190
- end
191
-
192
- test "shows a preview link for a previewable format" do
193
- rendered = render_govspeak(
194
- "[embed:attachments:3ed2]",
195
- [build_attachment(content_id: "3ed2", file_extension: "csv", url: "http://a.b/c.csv")]
196
- )
197
- assert_match(%r{<span class="preview"><strong>\s*<a href="http://a.b/c.csv/preview">View online</a>\s*</strong></span>}, compress_html(rendered))
198
- end
199
-
200
- test "Shows a download link for a previewable format" do
201
- rendered = render_govspeak(
202
- "[embed:attachments:3ed2]",
203
- [build_attachment(content_id: "3ed2", file_extension: "csv", url: "http://a.b/c.csv")]
204
- )
205
- assert_match(%r{<span class="download">\s*<a href="http://a.b/c.csv"><strong>Download CSV</strong></a>s*</span>}, compress_html(rendered))
206
- end
207
-
208
- test "Can show filesize for a download link for a previewable format" do
209
- rendered = render_govspeak(
210
- "[embed:attachments:3ed2]",
211
- [build_attachment(content_id: "3ed2", file_extension: "csv", url: "http://a.b/c.csv", file_size: 2048)]
212
- )
213
- assert_match(%r{<a href="http://a.b/c.csv" title="2 KB">}, rendered)
214
- end
215
-
216
- test "for a HTML format it outputs HTML" do
217
- rendered = render_govspeak(
218
- "[embed:attachments:3ed2]",
219
- [build_attachment(content_id: "3ed2", file_extension: "html")]
220
- )
221
- assert_match(%r{<span class="type">HTML</span>}, rendered)
222
- end
223
-
224
- test "for an external type it outputs the url" do
225
- rendered = render_govspeak(
226
- "[embed:attachments:3ed2]",
227
- [build_attachment(content_id: "3ed2", url: "http://a.b/c.pdf", external?: true)]
228
- )
229
- assert_match(%r{<span class="url">http://a.b/c.pdf</span>}, rendered)
230
- end
231
-
232
- test "will show a file extension in a abbr element for non html" do
233
- rendered = render_govspeak(
234
- "[embed:attachments:1fe8]",
235
- [build_attachment(content_id: "1fe8", file_extension: "pdf")]
236
- )
237
- assert_match(%r{<span class="type"><abbr title="Portable Document Format">PDF</abbr></span>}, rendered)
238
- end
239
-
240
- test "will show file size in a span" do
241
- rendered = render_govspeak(
242
- "[embed:attachments:1fe8]",
243
- [build_attachment(content_id: "1fe8", file_size: 1024)]
244
- )
245
- assert_match(%r{<span class="file-size">1 KB</span>}, rendered)
246
- end
247
-
248
- test "will show number of pages" do
249
- rendered = render_govspeak(
250
- "[embed:attachments:1fe8]",
251
- [build_attachment(content_id: "1fe8", number_of_pages: 1)]
252
- )
253
- assert_match(%r{<span class="page-length">1 page</span>}, rendered)
254
- rendered = render_govspeak(
255
- "[embed:attachments:1fe8]",
256
- [build_attachment(content_id: "1fe8", number_of_pages: 2)]
257
- )
258
- assert_match(%r{<span class="page-length">2 pages</span>}, rendered)
259
- end
260
-
261
- test "can show multiple attributes separated by a comma" do
262
- rendered = render_govspeak(
263
- "[embed:attachments:1fe8]",
264
- [build_attachment(content_id: "1fe8", file_extension: "pdf", file_size: 1024)]
265
- )
266
- pdf = %{<span class="type"><abbr title="Portable Document Format">PDF</abbr></span>}
267
- file_size = %{<span class="file-size">1 KB</span>}
268
- assert_match(%r{#{pdf}, #{file_size}}, rendered)
269
- end
270
-
271
- test "can show a link to order a copy of the attachment" do
272
- rendered = render_govspeak(
273
- "[embed:attachments:1fe8]",
274
- [build_attachment(content_id: "1fe8", order_url: "http://a.b/c")]
275
- )
276
- assert_match(%r{<a href="http://a.b/c" class="order_url" title="Order a copy of the publication">Order a copy</a>}, rendered)
277
- end
278
-
279
- test "can not show a link to order a copy of the attachment" do
280
- rendered = render_govspeak(
281
- "[embed:attachments:1fe8]",
282
- [build_attachment(content_id: "1fe8", order_url: "nil")]
283
- )
284
- refute_match(%r{<a href="http://a.b/c" class="order_url" title="Order a copy of the publication">Order a copy</a>}, rendered)
285
- end
286
-
287
- test "can show a price for ordering a copy of the attachment" do
288
- rendered = render_govspeak(
289
- "[embed:attachments:1fe8]",
290
- [build_attachment(content_id: "1fe8", order_url: "http://a.b/c", price: 10)]
291
- )
292
- assert_match(%r{(<span class="price">£10.00</span>)}, rendered)
293
- end
294
-
295
- test "can show opendocument help" do
296
- rendered = render_govspeak(
297
- "[embed:attachments:1fe8]",
298
- [build_attachment(content_id: "1fe8", opendocument?: true)]
299
- )
300
- assert_match(%r{<p class="opendocument-help">}, rendered)
301
- end
302
-
303
- test "can not show opendocument help" do
304
- rendered = render_govspeak(
305
- "[embed:attachments:1fe8]",
306
- [build_attachment(content_id: "1fe8", opendocument?: false)]
307
- )
308
- refute_match(%r{<p class="opendocument-help">}, rendered)
309
- end
310
-
311
- test "can show an accessibility warning" do
312
- rendered = render_govspeak(
313
- "[embed:attachments:1fe8]",
314
- [build_attachment(content_id: "1fe8", id: 10, accessible?: false)]
315
- )
316
- assert_match(%r{<div data-module="toggle" class="accessibility-warning" id="attachment-10-accessibility-help">}, rendered)
317
- end
318
-
319
- test "can not show an accessibility warning" do
320
- rendered = render_govspeak(
321
- "[embed:attachments:1fe8]",
322
- [build_attachment(content_id: "1fe8", id: 10, accessible?: true)]
323
- )
324
- refute_match(%r{<div data-module="toggle" class="accessibility-warning" id="attachment-10-accessibility-help">}, rendered)
325
- end
326
-
327
- test "shows accessibility mailto on a single line" do
328
- rendered = render_govspeak(
329
- "[embed:attachments:1fe8]",
330
- [build_attachment(content_id: "1fe8", id: 10, accessible?: false)]
331
- )
332
- assert_match(%r{<a href="mailto:govuk-feedback@digital.cabinet-office.gov.uk\?subject=[^\n]*&amp;body=[^\n]*">govuk-feedback@digital.cabinet-office.gov.uk</a>}, rendered)
333
- end
334
-
335
- Dir.glob("locales/*.yml") do |filename|
336
- locale = File.basename(filename, ".yml")
337
- test "can render in #{locale}" do
338
- rendered = render_govspeak(
339
- "[embed:attachments:3ed2]",
340
- [build_attachment(content_id: "3ed2")],
341
- locale: locale,
342
- )
343
- assert_match(/<section class="attachment embedded">/, rendered)
344
- end
345
- end
346
-
347
- test "attachment that isn't provided" do
348
- govspeak = "[embed:attachments:906ac8b7-850d-45c6-98e0-9525c680f891]"
349
- rendered = Govspeak::Document.new(govspeak).to_html
350
- assert_equal("\n", rendered)
351
- end
352
-
353
- test "attachment where filename is provided, rather than a content_id" do
354
- govspeak = "[embed:attachments:/path/to/file%20name.pdf]"
355
- rendered = Govspeak::Document.new(govspeak).to_html
356
- assert_equal("\n", rendered)
357
- end
358
- end