govuk_publishing_components 21.41.1 → 21.41.2

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: 44c358a971112138fa21afe594e6f8ec097a2ae39db754ffb277ea28df23456a
4
- data.tar.gz: 9bf94f62e7ceec5cceb760f43709a8fcd19253179cf8f4be6f5e34d34763b609
3
+ metadata.gz: a5c65e3a8bdade54b8a91ed68376eec0044c3d6d170f308e2b840033fc6869e8
4
+ data.tar.gz: adcfea00c085644145561da1186bbd6fe1b79377f4b88860de9ee4f5d509a0fe
5
5
  SHA512:
6
- metadata.gz: 15cce849af88de4b406a751bc403b8251afbd4412ffae71dd35ff192cf12d2419c5ccd582040c41279c4b973739b760c610aad5611c50883df6946b590ec37f0
7
- data.tar.gz: 9a24df4d9f7799416a33b7979c842a4555df2f23df9069f9f5a9477297bbfb4f525caecc1b7d62424035e806ba9f0e6d17b7766659f68c46eb2b4648cad264a2
6
+ metadata.gz: af40024d3f31625a02ab376c334f12b530b39b7103815f664032d16b88f06dba85ab59cb457a0b8f39e6512ec59af3a1d92fabd654dd446236a7fc8c19995ed4
7
+ data.tar.gz: 974e433acf0cf715994401e07ac0a2a2d861bea247b9f163caf30cb9ca1eae6eb524de4020c1457555719ffb28fbf1280ee04fef543413d8901d5f6c8ce458c5
@@ -57,22 +57,42 @@ module GovukPublishingComponents
57
57
 
58
58
  question = page.title
59
59
 
60
- # rubocop:disable Style/IfInsideElse
61
60
  doc.xpath("html/body").children.each_with_object({}) do |element, q_and_as|
62
- if question_element?(element)
63
- question = element.text
64
- q_and_as[question] = { anchor: element["id"] }
65
- else
66
- if question_hash_is_unset?(q_and_as, question)
67
- q_and_as[question] = { answer: element.to_s }
68
- elsif answer_is_unset?(q_and_as, question)
69
- q_and_as[question][:answer] = element.to_s
70
- else
71
- q_and_as[question][:answer] << element.to_s
61
+ _q_and_as, question = recursive_question_and_answers(element, question, q_and_as)
62
+ end
63
+ end
64
+
65
+ def recursive_question_and_answers(element, question, q_and_as)
66
+ if is_a_question?(element)
67
+ question = element.text
68
+ q_and_as[question] = { anchor: element["id"] }
69
+ else
70
+ q_and_as = add_answer_to_question(q_and_as, element, question)
71
+ element.children.each do |child_element|
72
+ if child_element.element?
73
+ q_and_as, question = recursive_question_and_answers(child_element, question, q_and_as)
72
74
  end
73
75
  end
74
76
  end
75
- # rubocop:enable Style/IfInsideElse
77
+
78
+ [q_and_as, question]
79
+ end
80
+
81
+ def add_answer_to_question(q_and_as, element, question)
82
+ if no_questions_in_subtree?(element)
83
+ if question_hash_is_unset?(q_and_as, question)
84
+ q_and_as[question] = { answer: element.to_s }
85
+ elsif answer_is_unset?(q_and_as, question)
86
+ q_and_as[question][:answer] = element.to_s
87
+ else
88
+ q_and_as[question][:answer] << element.to_s
89
+ end
90
+ end
91
+ q_and_as
92
+ end
93
+
94
+ def no_questions_in_subtree?(element)
95
+ element.search(QUESTION_TAG).none?
76
96
  end
77
97
 
78
98
  def question_hash_is_unset?(q_and_as, question)
@@ -86,7 +106,7 @@ module GovukPublishingComponents
86
106
  # we use H2 tags as the "question" and the html between them as the "answer"
87
107
  QUESTION_TAG = "h2".freeze
88
108
 
89
- def question_element?(element)
109
+ def is_a_question?(element)
90
110
  element.name == QUESTION_TAG
91
111
  end
92
112
 
@@ -1,3 +1,3 @@
1
1
  module GovukPublishingComponents
2
- VERSION = "21.41.1".freeze
2
+ VERSION = "21.41.2".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: 21.41.1
4
+ version: 21.41.2
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: 2020-04-10 00:00:00.000000000 Z
11
+ date: 2020-04-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gds-api-adapters