govuk_publishing_components 20.5.2 → 21.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 38f751bcc662ac5194c82e962eddca8edd4b0fa27c1af17f1cb59dc2d9c19e4a
4
- data.tar.gz: 4c87626b816dd2bebc14ab205f8cb5752c6010b07ebc1e08dd80b7e4c9465039
3
+ metadata.gz: 2ef4a6044ee14caf38ad63acffb0762e4cebc06fa412beeff1df1e07a6c021bc
4
+ data.tar.gz: a57304c3266b108045c6afd4305e972ba1441e7f7150b24796c585c55c5fd802
5
5
  SHA512:
6
- metadata.gz: b53285bb7fe7369a290e2cb0d40ca82edf0e54c5131dbec6611b14e548026f34f1baf39cdd2b7dc20b3a60f6a122cae9eae202ecca70411d6039038ae6ac64b6
7
- data.tar.gz: 502a6b3a0650938848b4cd46cb16f6de1e43157e215073297578ce1400b21533e4e735ca6ac0a983a53ac091216fc7b47a3031383d07a969693a62488dac5260
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" => questions_and_answers
22
+ "mainEntity" => questions_and_answers_markup
23
23
  }
24
24
  end
25
25
 
26
- def questions_and_answers
27
- page.parts.each_with_index.map do |part, index|
28
- part_url = part_url(part, index)
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" => part['title'],
33
- "url" => part_url,
32
+ "name" => question,
33
+ "url" => q_and_a_url,
34
34
  "acceptedAnswer" => {
35
35
  "@type" => "Answer",
36
- "url" => part_url,
37
- "text" => part['body']
36
+ "url" => q_and_a_url,
37
+ "text" => value[:answer]
38
38
  }
39
39
  }
40
40
  end
41
41
  end
42
42
 
43
- def part_url(part, index)
44
- if index.zero?
45
- guide_url
46
- else
47
- guide_url + "/" + part["slug"]
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 guide_url
97
+ def page_url
52
98
  Plek.new.website_root + page.base_path
53
99
  end
54
100
  end
@@ -59,8 +59,8 @@ module GovukPublishingComponents
59
59
  local_assigns[:logo_url]
60
60
  end
61
61
 
62
- def parts
63
- content_item.dig("details", "parts") || []
62
+ def requested_path
63
+ local_assigns[:request_path]
64
64
  end
65
65
  end
66
66
  end
@@ -1,3 +1,3 @@
1
1
  module GovukPublishingComponents
2
- VERSION = '20.5.2'.freeze
2
+ VERSION = '21.0.0'.freeze
3
3
  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: 20.5.2
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-17 00:00:00.000000000 Z
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