guides_style_18f 0.1.11 → 0.1.12

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
  SHA1:
3
- metadata.gz: b865432024e19d9efd28bbd172864cbd05617067
4
- data.tar.gz: 5c90ae1698a6598a898aa00af9660913d9a14784
3
+ metadata.gz: 7ed8884311059c0f0419dea17f6622f5e56b357a
4
+ data.tar.gz: 8bac760301892a270a60742dc76e28247159c01c
5
5
  SHA512:
6
- metadata.gz: 421affe591c3bbc4b6551b4362281aa5abdd1cbc44989c55e909f6dd1a8ac071a5726b00706f45e336fee7a4e270fd08134325505dcaf491ea836baa19a19abb
7
- data.tar.gz: 33cf1a0b7ffe68fbcc3a2dabb0da5853024a3e5e596c4dfe52b3f5ff84305a6e141f6dc4d25ea5eb9ce1d0925a6cfa4a9be0d030c1d70602aff05c2a4eba7ab0
6
+ metadata.gz: 8c29860082cc18fdd88eaf8305fb408ed839612a43959dfb13d1189866287955debe83b0186387a5c9b6ca8cfc091181a39f28a9355b8a7c279c03c657eccd6e
7
+ data.tar.gz: 0f955271cc7d346e506b41d05bccdf9714dd4d50b8929154785f29f4a0b9426fbefcb4ade3621b3f06ebcb3fc3460a48bc540f736650454b0c65e239c12016d5
@@ -119,46 +119,33 @@ module GuidesStyle18F
119
119
  config_data = SafeYAML.load_file config_path, safe: true
120
120
  return unless config_data
121
121
  nav_data = config_data['navigation'] || []
122
- nav_data = update_navigation_data nav_data, basedir
123
- write_navigation_data_to_config_file config_path, nav_data
122
+ update_navigation_data(nav_data, basedir)
123
+ write_navigation_data_to_config_file(config_path, nav_data)
124
124
  end
125
125
 
126
126
  private
127
127
 
128
128
  def self.update_navigation_data(nav_data, basedir)
129
- pages_data = pages_front_matter basedir
130
- children = pages_data['children'].map { |child| child['title'].downcase }
131
- nav_data.reject! { |nav| children.include? nav['text'].downcase }
132
- update_parent_nav_data nav_data, pages_data['parents']
133
- add_children_to_parents nav_data, pages_data['children']
129
+ original = map_nav_items_by_url('/', nav_data).to_h
130
+ updated = updated_nav_data(basedir)
131
+ remove_stale_nav_entries(nav_data, original, updated)
132
+ updated.map { |url, nav| apply_nav_update(url, nav, nav_data, original) }
133
+ check_for_orphaned_items(nav_data)
134
134
  end
135
135
 
136
- def self.pages_front_matter(basedir)
137
- front_matter = FrontMatter.load basedir
138
- errors = FrontMatter.validate_with_message_upon_error front_matter
139
- abort errors + "\n_config.yml not updated" if errors
140
- pages_data = front_matter.values.group_by do |fm|
141
- fm['parent'].nil? ? 'parents' : 'children'
142
- end
143
- %w(parents children).each { |category| pages_data[category] ||= [] }
144
- pages_data
145
- end
146
-
147
- def self.update_parent_nav_data(nav_data, parents)
148
- nav_by_title = nav_data_by_title nav_data
149
- parents.each do |page|
150
- page_nav = page_nav page
151
- title = page_nav['text'].downcase
152
- if nav_by_title.member? title
153
- nav_by_title[title].merge! page_nav
154
- else
155
- nav_data << page_nav
156
- end
136
+ def self.map_nav_items_by_url(parent_url, nav_data)
137
+ nav_data.flat_map do |nav|
138
+ url = File.join('', parent_url, nav['url'] || '')
139
+ [[url, nav]].concat(map_nav_items_by_url(url, nav['children'] || []))
157
140
  end
158
141
  end
159
142
 
160
- def self.nav_data_by_title(nav_data)
161
- nav_data.map { |nav| [nav['text'].downcase, nav] }.to_h
143
+ def self.updated_nav_data(basedir)
144
+ front_matter = FrontMatter.load basedir
145
+ errors = FrontMatter.validate_with_message_upon_error front_matter
146
+ abort errors + "\n_config.yml not updated" if errors
147
+ front_matter.values.sort_by { |fm| fm['permalink'] }
148
+ .map { |fm| [fm['permalink'], page_nav(fm)] }.to_h
162
149
  end
163
150
 
164
151
  def self.page_nav(front_matter)
@@ -174,31 +161,42 @@ module GuidesStyle18F
174
161
  result
175
162
  end
176
163
 
177
- def self.add_children_to_parents(nav_data, children)
178
- parents_by_title = nav_data_by_title nav_data
179
- children.each { |child| add_child_to_parent child, parents_by_title }
180
- nav_data
164
+ def self.remove_stale_nav_entries(nav_data, original, updated)
165
+ # Remove old entries whose pages have been deleted
166
+ original.each do |url, nav|
167
+ nav['delete'] = true if !updated.member?(url) && nav['internal']
168
+ end
169
+ original.delete_if { |_url, nav| nav['delete'] }
170
+ nav_data.delete_if { |nav| nav['delete'] }
181
171
  end
182
172
 
183
- def self.add_child_to_parent(child, parents_by_title)
184
- child_nav_data = page_nav child
185
- title = child_nav_data['text'].downcase
186
- parent = parent child, parents_by_title
187
- children = parent['children'] ||= []
188
- children_by_title = children.map { |c| [c['text'].downcase, c] }.to_h
173
+ def self.apply_nav_update(url, nav, nav_data, original)
174
+ orig = original[url]
175
+ if orig.nil?
176
+ apply_new_nav_item(url, nav, nav_data, original)
177
+ else
178
+ orig['text'] = nav['text']
179
+ end
180
+ end
189
181
 
190
- if children_by_title.member? title
191
- children_by_title[title].merge! child_nav_data
182
+ def self.apply_new_nav_item(url, nav, nav_data, original)
183
+ parent_url = File.dirname(url || '/')
184
+ parent = original["#{parent_url}/"]
185
+ if parent_url == '/'
186
+ nav_data << (original[url] = nav)
187
+ elsif parent.nil?
188
+ nav_data << { orphan_url: url }
192
189
  else
193
- children << child_nav_data
190
+ (parent['children'] ||= []) << nav
194
191
  end
195
192
  end
196
193
 
197
- def self.parent(child, parents_by_title)
198
- parent = parents_by_title[child['parent'].downcase]
199
- return parent unless parent.nil?
200
- fail StandardError, 'Parent page not present in existing ' \
201
- "config: \"#{child['parent']}\" needed by: \"#{child['title']}\""
194
+ def self.check_for_orphaned_items(nav_data)
195
+ orphans = nav_data.map { |nav| nav[:orphan_url] }.compact
196
+ unless orphans.empty?
197
+ fail(StandardError, "Parent pages missing for the following:\n " +
198
+ orphans.join("\n "))
199
+ end
202
200
  end
203
201
 
204
202
  def self.write_navigation_data_to_config_file(config_path, nav_data)
@@ -212,7 +210,7 @@ module GuidesStyle18F
212
210
 
213
211
  def self.process_line(line, lines, nav_data, in_navigation = false)
214
212
  if !in_navigation && line.start_with?('navigation:')
215
- lines << line << nav_data.to_yaml["---\n".size..-1]
213
+ lines << line << format_navigation_section(nav_data)
216
214
  in_navigation = true
217
215
  elsif in_navigation
218
216
  in_navigation = line.start_with?(' ') || line.start_with?('-')
@@ -222,4 +220,10 @@ module GuidesStyle18F
222
220
  end
223
221
  in_navigation
224
222
  end
223
+
224
+ YAML_PREFIX = "---\n"
225
+
226
+ def self.format_navigation_section(nav_data)
227
+ nav_data.empty? ? '' : nav_data.to_yaml[YAML_PREFIX.size..-1]
228
+ end
225
229
  end
@@ -1,5 +1,5 @@
1
1
  # @author Mike Bland (michael.bland@gsa.gov)
2
2
 
3
3
  module GuidesStyle18F
4
- VERSION = '0.1.11'
4
+ VERSION = '0.1.12'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: guides_style_18f
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.11
4
+ version: 0.1.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Bland
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-06 00:00:00.000000000 Z
11
+ date: 2016-01-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll