govspeak 5.7.1 → 5.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +31 -1
- data/lib/govspeak.rb +18 -13
- data/lib/govspeak/presenters/attachment_image_presenter.rb +25 -0
- data/lib/govspeak/presenters/image_presenter.rb +25 -0
- data/lib/govspeak/version.rb +1 -1
- data/test/govspeak_images_bang_test.rb +69 -0
- data/test/govspeak_images_test.rb +61 -0
- data/test/govspeak_test.rb +1 -53
- data/test/govspeak_test_helper.rb +6 -7
- metadata +8 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ac38fd3a1d6da3054f961380d76d362ad637aab0dd9593667a7657777ff10339
|
4
|
+
data.tar.gz: 6e8e0430a8a6ee72e67374b90e70a695ff8f1c710d10ce61849b5af10e9a4db9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b3727ecd02a709c0db38b5881de8cb34846d627fbae0c0543da05462b250dfd1fb21bd3f5b07351be73dcc5bd8e447e7215e9a3693c59bce001559a978e7d6b5
|
7
|
+
data.tar.gz: 7a190b06732cb113838ddff93eec9c34e5c3785568f8025781b8d8da9d6100a9da6bef64a190669217867dc6a55ed143757316b7a9538434dca19abda4dfb55f
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -432,7 +432,7 @@ will output an attachment within a block of text
|
|
432
432
|
<p>Details referenced in <span class="attachment-inline"><a href="http://example.com/my-thorough-study.pdf">My Thorough Study</a></span></p>
|
433
433
|
```
|
434
434
|
|
435
|
-
### Image Attachments
|
435
|
+
### Image Attachments (DEPRECATED: use `Image:image-id` instead)
|
436
436
|
|
437
437
|
Attachments can be used to embed an image within the document
|
438
438
|
|
@@ -460,6 +460,36 @@ will output a image section
|
|
460
460
|
</figure>
|
461
461
|
```
|
462
462
|
|
463
|
+
### Images
|
464
|
+
|
465
|
+
Images can be embedded as a figure with optional caption.
|
466
|
+
|
467
|
+
[Image:filename.png]
|
468
|
+
|
469
|
+
with options provided
|
470
|
+
|
471
|
+
{
|
472
|
+
images: [
|
473
|
+
{
|
474
|
+
alt_text: "Some alt text",
|
475
|
+
caption: "An optional caption",
|
476
|
+
url: "http://example.com/lovely-landscape.jpg",
|
477
|
+
id: "filename.png",
|
478
|
+
}
|
479
|
+
]
|
480
|
+
}
|
481
|
+
|
482
|
+
will output a image section
|
483
|
+
|
484
|
+
```html
|
485
|
+
<figure class="image embedded">
|
486
|
+
<div class="img">
|
487
|
+
<img src="http://example.com/lovely-landscape.jpg" alt="Some alt text">
|
488
|
+
<figcaption>An optional caption</figcaption>
|
489
|
+
</div>
|
490
|
+
</figure>
|
491
|
+
```
|
492
|
+
|
463
493
|
### Link
|
464
494
|
|
465
495
|
Links to different documents can be embedded so they change when the documents
|
data/lib/govspeak.rb
CHANGED
@@ -16,6 +16,8 @@ require 'govspeak/link_extractor'
|
|
16
16
|
require 'govspeak/presenters/attachment_presenter'
|
17
17
|
require 'govspeak/presenters/contact_presenter'
|
18
18
|
require 'govspeak/presenters/h_card_presenter'
|
19
|
+
require 'govspeak/presenters/image_presenter'
|
20
|
+
require 'govspeak/presenters/attachment_image_presenter'
|
19
21
|
|
20
22
|
|
21
23
|
module Govspeak
|
@@ -217,12 +219,9 @@ module Govspeak
|
|
217
219
|
|
218
220
|
extension('attached-image', /^!!([0-9]+)/) do |image_number|
|
219
221
|
image = images[image_number.to_i - 1]
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
else
|
224
|
-
""
|
225
|
-
end
|
222
|
+
next "" unless image
|
223
|
+
|
224
|
+
render_image(ImagePresenter.new(image))
|
226
225
|
end
|
227
226
|
|
228
227
|
extension('attachment', /\[embed:attachments:(?!inline:|image:)\s*(.*?)\s*\]/) do |content_id, body|
|
@@ -247,13 +246,12 @@ module Govspeak
|
|
247
246
|
%{<span#{span_id} class="attachment-inline">#{link}#{attributes}</span>}
|
248
247
|
end
|
249
248
|
|
249
|
+
# DEPRECATED: use 'Image:image-id' instead
|
250
250
|
extension('attachment image', /\[embed:attachments:image:\s*(.*?)\s*\]/) do |content_id|
|
251
251
|
attachment = attachments.detect { |a| a[:content_id] == content_id }
|
252
252
|
next "" unless attachment
|
253
253
|
|
254
|
-
|
255
|
-
title = (attachment.title || "").tr("\n", " ")
|
256
|
-
render_image(attachment.url, title, nil, attachment.id)
|
254
|
+
render_image(AttachmentImagePresenter.new(attachment))
|
257
255
|
end
|
258
256
|
|
259
257
|
# As of version 1.12.0 of Kramdown the block elements (div & figcaption)
|
@@ -266,12 +264,12 @@ module Govspeak
|
|
266
264
|
# out div and figcaption
|
267
265
|
#
|
268
266
|
# This issue is not considered a bug by kramdown: https://github.com/gettalong/kramdown/issues/191
|
269
|
-
def render_image(
|
270
|
-
id_attr = id ? %{ id="attachment_#{id}"} : ""
|
267
|
+
def render_image(image)
|
268
|
+
id_attr = image.id ? %{ id="attachment_#{image.id}"} : ""
|
271
269
|
lines = []
|
272
270
|
lines << %{<figure#{id_attr} class="image embedded">}
|
273
|
-
lines << %{<div class="img"><img src="#{encode(url)}" alt="#{encode(alt_text)}"></div>}
|
274
|
-
lines << %{<figcaption>#{caption
|
271
|
+
lines << %{<div class="img"><img src="#{encode(image.url)}" alt="#{encode(image.alt_text)}"></div>}
|
272
|
+
lines << %{<figcaption>#{encode(image.caption)}</figcaption>} if image.caption
|
275
273
|
lines << '</figure>'
|
276
274
|
lines.join
|
277
275
|
end
|
@@ -359,6 +357,13 @@ module Govspeak
|
|
359
357
|
@renderer.result(binding)
|
360
358
|
end
|
361
359
|
|
360
|
+
extension('Image', /\[Image:\s*(.*?)\s*\]/) do |image_id|
|
361
|
+
image = images.detect { |c| c.is_a?(Hash) && c[:id] == image_id }
|
362
|
+
next "" unless image
|
363
|
+
|
364
|
+
render_image(ImagePresenter.new(image))
|
365
|
+
end
|
366
|
+
|
362
367
|
private
|
363
368
|
|
364
369
|
def kramdown_doc
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Govspeak
|
2
|
+
class AttachmentImagePresenter
|
3
|
+
attr_reader :attachment
|
4
|
+
|
5
|
+
def initialize(attachment)
|
6
|
+
@attachment = AttachmentPresenter.new(attachment)
|
7
|
+
end
|
8
|
+
|
9
|
+
def url
|
10
|
+
attachment.url
|
11
|
+
end
|
12
|
+
|
13
|
+
def alt_text
|
14
|
+
(attachment.title || "").tr("\n", " ")
|
15
|
+
end
|
16
|
+
|
17
|
+
def caption
|
18
|
+
nil
|
19
|
+
end
|
20
|
+
|
21
|
+
def id
|
22
|
+
attachment.id
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Govspeak
|
2
|
+
class ImagePresenter
|
3
|
+
attr_reader :image
|
4
|
+
|
5
|
+
def initialize(image)
|
6
|
+
@image = image
|
7
|
+
end
|
8
|
+
|
9
|
+
def url
|
10
|
+
image.respond_to?(:url) ? image.url : image[:url]
|
11
|
+
end
|
12
|
+
|
13
|
+
def alt_text
|
14
|
+
image.respond_to?(:alt_text) ? image.alt_text : image[:alt_text]
|
15
|
+
end
|
16
|
+
|
17
|
+
def caption
|
18
|
+
(image.respond_to?(:caption) ? image.caption : image[:caption]).to_s.strip.presence
|
19
|
+
end
|
20
|
+
|
21
|
+
def id
|
22
|
+
nil
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/govspeak/version.rb
CHANGED
@@ -0,0 +1,69 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'test_helper'
|
4
|
+
require 'govspeak_test_helper'
|
5
|
+
|
6
|
+
class GovspeakImagesBangTest < Minitest::Test
|
7
|
+
include GovspeakTestHelper
|
8
|
+
|
9
|
+
class Image
|
10
|
+
attr_reader :alt_text, :url, :caption
|
11
|
+
|
12
|
+
def initialize(attrs = {})
|
13
|
+
@alt_text = attrs[:alt_text] || "my alt"
|
14
|
+
@url = attrs[:url] || "http://example.com/image.jpg"
|
15
|
+
@caption = attrs[:caption]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
test "!!n syntax renders an image in options[:images]" do
|
20
|
+
given_govspeak "!!1", images: [Image.new] do
|
21
|
+
assert_html_output(
|
22
|
+
%{<figure class="image embedded">} +
|
23
|
+
%{<div class="img"><img src="http://example.com/image.jpg" alt="my alt"></div>} +
|
24
|
+
%{</figure>}
|
25
|
+
)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
test "!!n syntax escapes alt text" do
|
30
|
+
given_govspeak "!!1", images: [Image.new(alt_text: %{my alt '&"<>})] do
|
31
|
+
assert_html_output(
|
32
|
+
%{<figure class="image embedded">} +
|
33
|
+
%{<div class="img"><img src="http://example.com/image.jpg" alt="my alt '&"<>"></div>} +
|
34
|
+
%{</figure>}
|
35
|
+
)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
test "!!n syntax renders nothing if not found" do
|
40
|
+
doc = Govspeak::Document.new("!!1")
|
41
|
+
assert_equal %{\n}, doc.to_html
|
42
|
+
end
|
43
|
+
|
44
|
+
test "Image:image-id syntax renders nothing" do
|
45
|
+
doc = Govspeak::Document.new("[Image:another-id]", images: [Image.new])
|
46
|
+
assert_equal %{\n}, doc.to_html
|
47
|
+
end
|
48
|
+
|
49
|
+
test "!!n syntax adds image caption if given" do
|
50
|
+
given_govspeak "!!1", images: [Image.new(caption: 'My Caption & so on')] do
|
51
|
+
assert_html_output(
|
52
|
+
%{<figure class="image embedded">} +
|
53
|
+
%{<div class="img"><img src="http://example.com/image.jpg" alt="my alt"></div>\n} +
|
54
|
+
%{<figcaption>My Caption & so on</figcaption>} +
|
55
|
+
%{</figure>}
|
56
|
+
)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
test "!!n syntax ignores a blank caption" do
|
61
|
+
given_govspeak "!!1", images: [Image.new(caption: ' ')] do
|
62
|
+
assert_html_output(
|
63
|
+
%{<figure class="image embedded">} +
|
64
|
+
%{<div class="img"><img src="http://example.com/image.jpg" alt="my alt"></div>} +
|
65
|
+
%{</figure>}
|
66
|
+
)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'test_helper'
|
4
|
+
require 'govspeak_test_helper'
|
5
|
+
|
6
|
+
class GovspeakImagesTest < Minitest::Test
|
7
|
+
include GovspeakTestHelper
|
8
|
+
|
9
|
+
def build_image(attrs = {})
|
10
|
+
attrs[:alt_text] ||= "my alt"
|
11
|
+
attrs[:url] ||= "http://example.com/image.jpg"
|
12
|
+
attrs[:id] ||= "image-id"
|
13
|
+
attrs
|
14
|
+
end
|
15
|
+
|
16
|
+
test "Image:image-id syntax renders an image in options[:images]" do
|
17
|
+
given_govspeak "[Image:image-id]", images: [build_image] do
|
18
|
+
assert_html_output(
|
19
|
+
%{<figure class="image embedded">} +
|
20
|
+
%{<div class="img"><img src="http://example.com/image.jpg" alt="my alt"></div>} +
|
21
|
+
%{</figure>}
|
22
|
+
)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
test "Image:image-id syntax escapes alt text" do
|
27
|
+
given_govspeak "[Image:image-id]", images: [build_image(alt_text: %{my alt '&"<>})] do
|
28
|
+
assert_html_output(
|
29
|
+
%{<figure class="image embedded">} +
|
30
|
+
%{<div class="img"><img src="http://example.com/image.jpg" alt="my alt '&"<>"></div>} +
|
31
|
+
%{</figure>}
|
32
|
+
)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
test "Image:image-id syntax renders nothing if not found" do
|
37
|
+
doc = Govspeak::Document.new("[Image:another-id]")
|
38
|
+
assert_equal %{\n}, doc.to_html
|
39
|
+
end
|
40
|
+
|
41
|
+
test "Image:image-id syntax adds image caption if given" do
|
42
|
+
given_govspeak "[Image:image-id]", images: [build_image(caption: 'My Caption & so on')] do
|
43
|
+
assert_html_output(
|
44
|
+
%{<figure class="image embedded">} +
|
45
|
+
%{<div class="img"><img src="http://example.com/image.jpg" alt="my alt"></div>\n} +
|
46
|
+
%{<figcaption>My Caption & so on</figcaption>} +
|
47
|
+
%{</figure>}
|
48
|
+
)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
test "Image:image-id syntax ignores a blank caption" do
|
53
|
+
given_govspeak "[Image:image-id]", images: [build_image(caption: ' ')] do
|
54
|
+
assert_html_output(
|
55
|
+
%{<figure class="image embedded">} +
|
56
|
+
%{<div class="img"><img src="http://example.com/image.jpg" alt="my alt"></div>} +
|
57
|
+
%{</figure>}
|
58
|
+
)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/test/govspeak_test.rb
CHANGED
@@ -440,7 +440,7 @@ Teston
|
|
440
440
|
|
441
441
|
$CTA
|
442
442
|
Click here to start the tool
|
443
|
-
$CTA",
|
443
|
+
$CTA", document_domains: %w(www.not-external.com) do
|
444
444
|
assert_html_output %{
|
445
445
|
<p><a href="http://www.not-external.com">internal link</a></p>
|
446
446
|
|
@@ -632,58 +632,6 @@ Teston
|
|
632
632
|
}
|
633
633
|
end
|
634
634
|
|
635
|
-
test "can reference attached images using !!n" do
|
636
|
-
images = [OpenStruct.new(alt_text: 'my alt', url: "http://example.com/image.jpg")]
|
637
|
-
given_govspeak "!!1", images do
|
638
|
-
assert_html_output(
|
639
|
-
%{<figure class="image embedded">} +
|
640
|
-
%{<div class="img"><img src="http://example.com/image.jpg" alt="my alt"></div>} +
|
641
|
-
%{</figure>}
|
642
|
-
)
|
643
|
-
end
|
644
|
-
end
|
645
|
-
|
646
|
-
test "alt text of referenced images is escaped" do
|
647
|
-
images = [OpenStruct.new(alt_text: %{my alt '&"<>}, url: "http://example.com/image.jpg")]
|
648
|
-
given_govspeak "!!1", images do
|
649
|
-
assert_html_output(
|
650
|
-
%{<figure class="image embedded">} +
|
651
|
-
%{<div class="img"><img src="http://example.com/image.jpg" alt="my alt '&"<>"></div>} +
|
652
|
-
%{</figure>}
|
653
|
-
)
|
654
|
-
end
|
655
|
-
end
|
656
|
-
|
657
|
-
test "silently ignores an image attachment if the referenced image is missing" do
|
658
|
-
doc = Govspeak::Document.new("!!1")
|
659
|
-
doc.images = []
|
660
|
-
|
661
|
-
assert_equal %{\n}, doc.to_html
|
662
|
-
end
|
663
|
-
|
664
|
-
test "adds image caption if given" do
|
665
|
-
images = [OpenStruct.new(alt_text: "my alt", url: "http://example.com/image.jpg", caption: 'My Caption & so on')]
|
666
|
-
given_govspeak "!!1", images do
|
667
|
-
assert_html_output(
|
668
|
-
%{<figure class="image embedded">} +
|
669
|
-
%{<div class="img"><img src="http://example.com/image.jpg" alt="my alt"></div>\n} +
|
670
|
-
%{<figcaption>My Caption & so on</figcaption>} +
|
671
|
-
%{</figure>}
|
672
|
-
)
|
673
|
-
end
|
674
|
-
end
|
675
|
-
|
676
|
-
test "ignores a blank caption" do
|
677
|
-
images = [OpenStruct.new(alt_text: "my alt", url: "http://example.com/image.jpg", caption: ' ')]
|
678
|
-
given_govspeak "!!1", images do
|
679
|
-
assert_html_output(
|
680
|
-
%{<figure class="image embedded">} +
|
681
|
-
%{<div class="img"><img src="http://example.com/image.jpg" alt="my alt"></div>} +
|
682
|
-
%{</figure>}
|
683
|
-
)
|
684
|
-
end
|
685
|
-
end
|
686
|
-
|
687
635
|
test "can sanitize a document" do
|
688
636
|
document = Govspeak::Document.new("<script>doBadThings();</script>")
|
689
637
|
assert_equal "doBadThings();", document.to_sanitized_html.strip
|
@@ -4,15 +4,14 @@ module GovspeakTestHelper
|
|
4
4
|
end
|
5
5
|
|
6
6
|
class GovspeakAsserter
|
7
|
-
def initialize(testcase, govspeak,
|
7
|
+
def initialize(testcase, govspeak, options = {})
|
8
8
|
@testcase = testcase
|
9
9
|
@govspeak = remove_indentation(govspeak)
|
10
|
-
@images = images
|
11
10
|
@options = options
|
12
11
|
end
|
13
12
|
|
14
13
|
def document
|
15
|
-
Govspeak::Document.new(@govspeak, @options
|
14
|
+
Govspeak::Document.new(@govspeak, @options)
|
16
15
|
end
|
17
16
|
|
18
17
|
def assert_text_output(raw_expected)
|
@@ -55,8 +54,8 @@ module GovspeakTestHelper
|
|
55
54
|
end
|
56
55
|
end
|
57
56
|
|
58
|
-
def given_govspeak(govspeak,
|
59
|
-
asserter = GovspeakAsserter.new(self, govspeak,
|
57
|
+
def given_govspeak(govspeak, options = {}, &block)
|
58
|
+
asserter = GovspeakAsserter.new(self, govspeak, options)
|
60
59
|
asserter.instance_eval(&block)
|
61
60
|
end
|
62
61
|
|
@@ -69,9 +68,9 @@ module GovspeakTestHelper
|
|
69
68
|
end
|
70
69
|
|
71
70
|
module ClassMethods
|
72
|
-
def test_given_govspeak(govspeak,
|
71
|
+
def test_given_govspeak(govspeak, options = {}, &block)
|
73
72
|
test "Given #{govspeak}" do
|
74
|
-
given_govspeak(govspeak,
|
73
|
+
given_govspeak(govspeak, options, &block)
|
75
74
|
end
|
76
75
|
end
|
77
76
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: govspeak
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 5.
|
4
|
+
version: 5.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- GOV.UK Dev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-12-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionview
|
@@ -269,9 +269,11 @@ files:
|
|
269
269
|
- lib/govspeak/kramdown_overrides.rb
|
270
270
|
- lib/govspeak/link_extractor.rb
|
271
271
|
- lib/govspeak/post_processor.rb
|
272
|
+
- lib/govspeak/presenters/attachment_image_presenter.rb
|
272
273
|
- lib/govspeak/presenters/attachment_presenter.rb
|
273
274
|
- lib/govspeak/presenters/contact_presenter.rb
|
274
275
|
- lib/govspeak/presenters/h_card_presenter.rb
|
276
|
+
- lib/govspeak/presenters/image_presenter.rb
|
275
277
|
- lib/govspeak/structured_header_extractor.rb
|
276
278
|
- lib/govspeak/version.rb
|
277
279
|
- lib/kramdown/parser/kramdown_with_automatic_external_links.rb
|
@@ -328,6 +330,8 @@ files:
|
|
328
330
|
- test/govspeak_button_test.rb
|
329
331
|
- test/govspeak_contacts_test.rb
|
330
332
|
- test/govspeak_extract_contact_content_ids_test.rb
|
333
|
+
- test/govspeak_images_bang_test.rb
|
334
|
+
- test/govspeak_images_test.rb
|
331
335
|
- test/govspeak_link_extractor_test.rb
|
332
336
|
- test/govspeak_link_test.rb
|
333
337
|
- test/govspeak_structured_headers_test.rb
|
@@ -373,7 +377,9 @@ test_files:
|
|
373
377
|
- test/govspeak_link_test.rb
|
374
378
|
- test/govspeak_extract_contact_content_ids_test.rb
|
375
379
|
- test/govspeak_structured_headers_test.rb
|
380
|
+
- test/govspeak_images_test.rb
|
376
381
|
- test/presenters/h_card_presenter_test.rb
|
382
|
+
- test/govspeak_images_bang_test.rb
|
377
383
|
- test/govspeak_link_extractor_test.rb
|
378
384
|
- test/govspeak_attachments_test.rb
|
379
385
|
- test/govspeak_attachments_inline_test.rb
|