govuk_publishing_components 20.5.2 → 21.0.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 +4 -4
- data/app/views/govuk_publishing_components/components/_machine_readable_metadata.html.erb +1 -0
- data/app/views/govuk_publishing_components/components/docs/machine_readable_metadata.yml +6 -0
- data/lib/govuk_publishing_components/presenters/machine_readable/faq_page_schema.rb +60 -14
- data/lib/govuk_publishing_components/presenters/machine_readable/page.rb +2 -2
- data/lib/govuk_publishing_components/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2ef4a6044ee14caf38ad63acffb0762e4cebc06fa412beeff1df1e07a6c021bc
|
4
|
+
data.tar.gz: a57304c3266b108045c6afd4305e972ba1441e7f7150b24796c585c55c5fd802
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9a79245895e9ad222d86d6977d4c0a08db90a4a034c1650ef84d86919a051fbcec76fb40f780f249b0feaa4108f8b05609eccf5d188f9aae28b345bd78791a90
|
7
|
+
data.tar.gz: 7da229230ec92c542edebca0921fd85bf234c126c883fb89c0005d24e12ed4d0aade2a9c507722edf59248a077b9c408abb54ce5b12e61511a36c14abe7ef5ef
|
@@ -4,6 +4,7 @@
|
|
4
4
|
image_url("govuk_publishing_components/govuk-schema-placeholder-4x3.png"),
|
5
5
|
image_url("govuk_publishing_components/govuk-schema-placeholder-16x9.png"),
|
6
6
|
] %>
|
7
|
+
<% local_assigns[:request_path] = request.path %>
|
7
8
|
<% page = GovukPublishingComponents::Presenters::Page.new(local_assigns) %>
|
8
9
|
<% structured_data = GovukPublishingComponents::Presenters::SchemaOrg.new(page).structured_data %>
|
9
10
|
|
@@ -54,6 +54,12 @@ examples:
|
|
54
54
|
- slug: treating-injuries
|
55
55
|
body: Get to know a good blacksmith
|
56
56
|
title: Treating injuries
|
57
|
+
body: >
|
58
|
+
<p>Training scaly beasts can be tricky</p>
|
59
|
+
<h2 id='preparation'>Preparation</h2>
|
60
|
+
<p>Prepare by meditating and writing a will</p>
|
61
|
+
<h2 id='afterwards'>Afterwards<h2>
|
62
|
+
<p>Your next of kin may need a strong stomach</p>
|
57
63
|
schema: :faq
|
58
64
|
canonical_url: https://www.gov.uk/how-to-train-your-dragon
|
59
65
|
person_schema:
|
@@ -19,36 +19,82 @@ module GovukPublishingComponents
|
|
19
19
|
|
20
20
|
def main_entity
|
21
21
|
{
|
22
|
-
"mainEntity" =>
|
22
|
+
"mainEntity" => questions_and_answers_markup
|
23
23
|
}
|
24
24
|
end
|
25
25
|
|
26
|
-
def
|
27
|
-
page.
|
28
|
-
|
26
|
+
def questions_and_answers_markup
|
27
|
+
question_and_answers(page.body).map do |question, value|
|
28
|
+
q_and_a_url = section_url(value[:anchor])
|
29
29
|
|
30
30
|
{
|
31
31
|
"@type" => "Question",
|
32
|
-
"name" =>
|
33
|
-
"url" =>
|
32
|
+
"name" => question,
|
33
|
+
"url" => q_and_a_url,
|
34
34
|
"acceptedAnswer" => {
|
35
35
|
"@type" => "Answer",
|
36
|
-
"url" =>
|
37
|
-
"text" =>
|
36
|
+
"url" => q_and_a_url,
|
37
|
+
"text" => value[:answer]
|
38
38
|
}
|
39
39
|
}
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
43
|
+
# Generates a hash of questions and associated information:
|
44
|
+
# - question: the text in the h2 tag preceding other markup. Questions are
|
45
|
+
# used to key the hash. "Summary" is set as the default, as
|
46
|
+
# there is often a preamble in guides before any h2 is set.
|
47
|
+
#
|
48
|
+
# - :answer: the markup that is not an h2 tag. It is associated with the
|
49
|
+
# preceding h2 header.
|
50
|
+
#
|
51
|
+
# - :anchor: the id of the h2 (autogenerated by the markdown converter).
|
52
|
+
# This is used to build links directly to the section in question
|
53
|
+
def question_and_answers(html)
|
54
|
+
doc = Nokogiri::HTML(html)
|
55
|
+
|
56
|
+
question = "Summary"
|
57
|
+
|
58
|
+
# rubocop:disable Style/IfInsideElse
|
59
|
+
doc.xpath("html/body").children.each_with_object({}) do |element, q_and_as|
|
60
|
+
if question_element?(element)
|
61
|
+
question = element.text
|
62
|
+
q_and_as[question] = { anchor: element["id"] }
|
63
|
+
else
|
64
|
+
if question_hash_is_unset?(q_and_as, question)
|
65
|
+
q_and_as[question] = { answer: element.to_s }
|
66
|
+
elsif answer_is_unset?(q_and_as, question)
|
67
|
+
q_and_as[question][:answer] = element.to_s
|
68
|
+
else
|
69
|
+
q_and_as[question][:answer] << element.to_s
|
70
|
+
end
|
71
|
+
end
|
48
72
|
end
|
73
|
+
# rubocop:enable Style/IfInsideElse
|
74
|
+
end
|
75
|
+
|
76
|
+
def question_hash_is_unset?(q_and_as, question)
|
77
|
+
q_and_as[question].nil?
|
78
|
+
end
|
79
|
+
|
80
|
+
def answer_is_unset?(q_and_as, question)
|
81
|
+
!q_and_as[question].has_key?(:answer)
|
82
|
+
end
|
83
|
+
|
84
|
+
# we use H2 tags as the "question" and the html between them as the "answer"
|
85
|
+
QUESTION_TAG = "h2".freeze
|
86
|
+
|
87
|
+
def question_element?(element)
|
88
|
+
element.name == QUESTION_TAG
|
89
|
+
end
|
90
|
+
|
91
|
+
def section_url(anchor)
|
92
|
+
return page_url + "#" + anchor if anchor.present?
|
93
|
+
|
94
|
+
page_url
|
49
95
|
end
|
50
96
|
|
51
|
-
def
|
97
|
+
def page_url
|
52
98
|
Plek.new.website_root + page.base_path
|
53
99
|
end
|
54
100
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: govuk_publishing_components
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 21.0.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: 2019-09-
|
11
|
+
date: 2019-09-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: gds-api-adapters
|