nanoc-conref-fs 0.2.1 → 0.3.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
  SHA1:
3
- metadata.gz: cb197585dc561db562827e176ec5e27d9df500c4
4
- data.tar.gz: 8d990f45b2470c99d3894a40d6e0542ef43bf467
3
+ metadata.gz: 8cf69ee6e85496e24ff25128d9013457aba33741
4
+ data.tar.gz: dbed8da256b719eb52d1bda8640919060b3ab3e0
5
5
  SHA512:
6
- metadata.gz: 3072389a547de1101eca906624333c9d75184a2247bc07c3d97313924da6fa0109d2c9de38824918fbde11f56f372c38f14edec215a12e6fe3568172b9920f68
7
- data.tar.gz: 9ac746f06c7256cb58bc0ec16f3f02ebe1ed93b648e9c15719c5ba1f754d899af8acdb84562662dd30e65b5da241e6ff165355b1769531a59bfcdad9b397988b
6
+ metadata.gz: ab9faf957d43bc03944a47499989ae4f3fca7bcefe1a0976ad0c81a9776bc4315736a5d60fec3eac23203888a592882617f6b767dedb974845313bc7c7688c20
7
+ data.tar.gz: 751bdef5e71b825daffc335964334be1e14c237cbd5be52055ca83fcc5fcd2d8aa69c971614aac9bede21efffd8df655d2e184747dc609727f3a1d4cc1559f8b
data/README.md CHANGED
@@ -76,8 +76,15 @@ See the tests for further usage of these conditionals. In both cases, `path` is
76
76
 
77
77
  ### Associating files with data
78
78
 
79
- If you have a special `data_association` value, additional metadata to items will be applied. Right now, this only adds a `parent` type to each item.
79
+ If you have a special `data_association` value, additional metadata to items will be applied:
80
+
81
+ * An attribute called `:parents`, which adds the parent "map topic" to an item.
82
+ * An attribute called `:children`, which adds any children of a "map topic."
80
83
 
81
84
  ### Retrieving variables
82
85
 
83
86
  You can retrieve the stored data at any time (for example, in a layout) by calling `VariableMixin.variables`.
87
+
88
+ ### Retrieving data files
89
+
90
+ You can fetch anything in the *data* folder by passing in a string, demarcated with `.`s, to `VariableMixin.fetch_data_file`. For example, `VariableMixin.fetch_data_file('reusables.intro')` will fetch the file in *data/reusables/intro.yml*.
@@ -9,6 +9,16 @@ module VariableMixin
9
9
  def self.variables=(variables)
10
10
  @variables = variables
11
11
  end
12
+
13
+ def self.fetch_data_file(association)
14
+ reference = association.split('.')
15
+ variables = VariableMixin.variables
16
+ data = variables['site']['data']
17
+ while key = reference.shift
18
+ data = data[key]
19
+ end
20
+ data
21
+ end
12
22
  end
13
23
 
14
24
  class ConrefFS < Nanoc::DataSource
@@ -28,28 +38,47 @@ class ConrefFS < Nanoc::DataSource
28
38
  super
29
39
  end
30
40
 
31
- def self.fetch_variables
32
- @variables
33
- end
34
-
35
41
  # This function calls the parent super, then adds additional metadata to the item.
36
42
  def parse(content_filename, meta_filename, _kind)
37
43
  meta, content = super
38
44
  page_vars = Conrefifier.file_variables(@site_config[:page_variables], content_filename)
39
45
  unless page_vars[:data_association].nil?
40
46
  association = page_vars[:data_association]
41
- reference = association.split('.')
42
- toc = @variables['site']['data']
43
- while key = reference.shift
44
- toc = toc[key]
45
- end
46
- meta['parents'] = find_parents(toc, meta['title'])
47
+ toc = VariableMixin.fetch_data_file(association)
48
+ meta['parents'] = if toc.is_a?(Array)
49
+ find_array_parents(toc, meta['title'])
50
+ else
51
+ find_hash_parents(toc, meta['title'])
52
+ end
53
+
54
+ meta['children'] = if toc.is_a?(Array)
55
+ find_array_children(toc, meta['title'])
56
+ else
57
+ find_hash_children(toc, meta['title'])
58
+ end
59
+ end
60
+ page_vars.each_pair do |name, value|
61
+ meta[name.to_s] = value
47
62
  end
48
63
  [meta, content]
49
64
  end
50
65
 
51
- # Given a category file, this method finds its parent.
52
- def find_parents(toc, title)
66
+ # Given a category file that's an array, this method finds
67
+ # the parent of an item
68
+ def find_array_parents(toc, title)
69
+ parents = ''
70
+ toc.each do |item|
71
+ if item.is_a?(Hash)
72
+ parents = find_hash_parents(item, title)
73
+ break unless parents.empty?
74
+ end
75
+ end
76
+ parents
77
+ end
78
+
79
+ # Given a category file that's a hash, this method finds
80
+ # the parent of an item
81
+ def find_hash_parents(toc, title)
53
82
  parents = ''
54
83
  toc.keys.each do |key|
55
84
  toc[key].each do |item|
@@ -73,6 +102,41 @@ class ConrefFS < Nanoc::DataSource
73
102
  parents
74
103
  end
75
104
 
105
+
106
+ # Given a category file that's an array, this method finds
107
+ # the children of an item, probably a map topic
108
+ def find_array_children(toc, title)
109
+ children = ''
110
+ toc.each do |item|
111
+ next unless item.is_a?(Hash)
112
+ item.each_pair do |key, values|
113
+ if key == title
114
+ children = values.flatten
115
+ break
116
+ end
117
+ end
118
+ break unless children.empty?
119
+ end
120
+ children
121
+ end
122
+
123
+ # Given a category file that's a hash, this method finds
124
+ # the children of an item, probably a map topic
125
+ def find_hash_children(toc, title)
126
+ children = ''
127
+ toc.keys.each do |key|
128
+ toc[key].each do |item|
129
+ next unless item.is_a?(Hash)
130
+ unless item[title].nil?
131
+ children = item.values.flatten
132
+ break
133
+ end
134
+ end
135
+ break unless children.empty?
136
+ end
137
+ children
138
+ end
139
+
76
140
  # This file reads each piece of content as it comes in. It also applies the conref variables
77
141
  # (demarcated by Liquid's {{ }} tags) using both the data/ folder and any variables defined
78
142
  # within the nanoc.yaml config file
@@ -3,7 +3,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
3
 
4
4
  Gem::Specification.new do |spec|
5
5
  spec.name = 'nanoc-conref-fs'
6
- spec.version = '0.2.1'
6
+ spec.version = '0.3.0'
7
7
  spec.authors = ['Garen Torikian']
8
8
  spec.email = ['gjtorikian@gmail.com']
9
9
  spec.summary = 'A Nanoc filesystem to permit using conrefs/reusables in your content.'
@@ -86,7 +86,19 @@ class DatafilesTest < MiniTest::Test
86
86
  end
87
87
  end
88
88
 
89
- def test_missing_cateogory_title_does_not_blow_up_parents
89
+ def test_it_renders_array_parents
90
+ with_site(name: FIXTURES_DIR) do |site|
91
+
92
+ site = Nanoc::Int::SiteLoader.new.new_from_cwd
93
+ site.compile
94
+
95
+ output_file = read_output_file('parents', 'array_parents')
96
+ test_file = read_test_file('parents', 'array_parents')
97
+ assert_equal output_file, test_file
98
+ end
99
+ end
100
+
101
+ def test_missing_category_title_does_not_blow_up_parents
90
102
  with_site(name: FIXTURES_DIR) do |site|
91
103
 
92
104
  site = Nanoc::Int::SiteLoader.new.new_from_cwd
@@ -98,6 +110,18 @@ class DatafilesTest < MiniTest::Test
98
110
  end
99
111
  end
100
112
 
113
+ def test_it_applies_any_attribute
114
+ with_site(name: FIXTURES_DIR) do |site|
115
+
116
+ site = Nanoc::Int::SiteLoader.new.new_from_cwd
117
+ site.compile
118
+
119
+ output_file = read_output_file('attributes', 'attribute')
120
+ test_file = read_test_file('attributes', 'attribute')
121
+ assert_equal output_file, test_file
122
+ end
123
+ end
124
+
101
125
  def test_it_obfuscates_content
102
126
  with_site(name: FIXTURES_DIR) do |site|
103
127
 
@@ -124,4 +148,32 @@ class DatafilesTest < MiniTest::Test
124
148
  assert_equal output_file, test_file
125
149
  end
126
150
  end
151
+
152
+ def test_it_renders_hash_children
153
+ with_site(name: FIXTURES_DIR) do |site|
154
+
155
+ site = Nanoc::Int::SiteLoader.new.new_from_cwd
156
+ site.compile
157
+
158
+ output_file = read_output_file('children', 'hash_children')
159
+ test_file = read_test_file('children', 'hash_children')
160
+ assert_equal output_file, test_file
161
+
162
+ output_file = read_output_file('children', 'later_hash_children')
163
+ test_file = read_test_file('children', 'later_hash_children')
164
+ assert_equal output_file, test_file
165
+ end
166
+ end
167
+
168
+ def test_it_renders_array_children
169
+ with_site(name: FIXTURES_DIR) do |site|
170
+
171
+ site = Nanoc::Int::SiteLoader.new.new_from_cwd
172
+ site.compile
173
+
174
+ output_file = read_output_file('children', 'array_children')
175
+ test_file = read_test_file('children', 'array_children')
176
+ assert_equal output_file, test_file
177
+ end
178
+ end
127
179
  end
@@ -7,7 +7,7 @@ class DatafilesTest < MiniTest::Test
7
7
 
8
8
  def test_it_collects_the_files
9
9
  files = Datafiles.collect_data(File.join(FIXTURES_DIR, 'data'))
10
- names = %w(categories/category reusables/intro reusables/names variables/empty variables/product)
10
+ names = %w(categories/category categories/simple reusables/intro reusables/names variables/empty variables/product)
11
11
  names.map! { |name| File.join(FIXTURES_DIR, 'data', "#{name}.yml") }
12
12
  assert_equal files, names
13
13
  end
data/test/fixtures/Rules CHANGED
@@ -7,6 +7,8 @@ end
7
7
  compile '/**/*.md' do
8
8
  if item[:filename] =~ /retrieve/
9
9
  layout '/retrieve.*'
10
+ elsif item[:filename] =~ /_children/
11
+ layout '/children.*'
10
12
  else
11
13
  layout '/default.*'
12
14
  end
@@ -0,0 +1,13 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>
5
+ Testing attributes
6
+ </title>
7
+ </head>
8
+
9
+ <body>
10
+ wow!
11
+
12
+ </body>
13
+ </html>
@@ -0,0 +1,3 @@
1
+ ---
2
+ title: Testing attributes
3
+ ---
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>
5
+ Creating a new organization account
6
+ </title>
7
+ </head>
8
+
9
+ <body>
10
+
11
+ ["About organizations", "Creating a new organization from scratch"]
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,3 @@
1
+ ---
2
+ title: Creating a new organization account
3
+ ---
@@ -0,0 +1,18 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>
5
+ Managing branches in your repository
6
+ </title>
7
+ </head>
8
+
9
+ <body>
10
+
11
+
12
+ Here they are!
13
+
14
+
15
+ ["About branches", "Setting the default branch", "Viewing branches in your repository", "Creating and deleting branches within your repository"]
16
+
17
+ </body>
18
+ </html>
@@ -0,0 +1,5 @@
1
+ ---
2
+ title: Managing branches in your repository
3
+ ---
4
+
5
+ Here they are!
@@ -0,0 +1,16 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>
5
+ Some other things
6
+ </title>
7
+ </head>
8
+
9
+ <body>
10
+
11
+
12
+
13
+ ["Blah1", "Blah2"]
14
+
15
+ </body>
16
+ </html>
@@ -0,0 +1,3 @@
1
+ ---
2
+ title: Some other things
3
+ ---
@@ -0,0 +1,16 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>
5
+ Creating a new organization from scratch
6
+ </title>
7
+ </head>
8
+
9
+ <body>
10
+
11
+ Creating a new organization account
12
+
13
+ Array parent.
14
+
15
+ </body>
16
+ </html>
@@ -0,0 +1,5 @@
1
+ ---
2
+ title: Creating a new organization from scratch
3
+ ---
4
+
5
+ Array parent.
@@ -7,3 +7,7 @@ Collaborating:
7
7
  - Setting the default branch
8
8
  - Viewing branches in your repository
9
9
  - Creating and deleting branches within your repository
10
+
11
+ - Some other things:
12
+ - Blah1
13
+ - Blah2
@@ -0,0 +1,5 @@
1
+ - Set Up Git
2
+ - Create A Repo
3
+ - Creating a new organization account:
4
+ - About organizations
5
+ - Creating a new organization from scratch
@@ -0,0 +1,16 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>
5
+ <%= @item[:title] %>
6
+ </title>
7
+ </head>
8
+
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ <%= @item[:children] %>
14
+
15
+ </body>
16
+ </html>
@@ -7,7 +7,7 @@
7
7
  </head>
8
8
 
9
9
  <body>
10
-
10
+ <% if @item[:title] =~ /Testing attributes/ %><%= @item[:just_a_key] %><% end %>
11
11
  <%= @item[:intro] %>
12
12
  <%= @item[:parents] %>
13
13
  <%= yield %>
@@ -13,6 +13,19 @@ page_variables:
13
13
  values:
14
14
  version: "dotcom"
15
15
  data_association: "categories.category"
16
+ just_a_key: wow!
17
+ -
18
+ scope:
19
+ path: "array_parents"
20
+ values:
21
+ version: "dotcom"
22
+ data_association: "categories.simple"
23
+ -
24
+ scope:
25
+ path: "array_children"
26
+ values:
27
+ version: "dotcom"
28
+ data_association: "categories.simple"
16
29
  -
17
30
  scope:
18
31
  path: "different"
@@ -0,0 +1,20 @@
1
+ require 'test_helper'
2
+
3
+ class VariableMixinTest < MiniTest::Test
4
+
5
+ def test_it_fetches_variables
6
+ with_site(name: FIXTURES_DIR) do |site|
7
+
8
+ site = Nanoc::Int::SiteLoader.new.new_from_cwd
9
+ site.compile
10
+
11
+ assert_equal VariableMixin.variables.keys, ['site']
12
+ assert_equal VariableMixin.variables['site']['data'].keys, %w(categories reusables variables)
13
+ end
14
+ end
15
+
16
+ def test_it_fetches_datafiles
17
+ file = VariableMixin.fetch_data_file('reusables.intro')
18
+ assert_equal file['frontmatter_intro'], 'Here I am, in the front.'
19
+ end
20
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nanoc-conref-fs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Garen Torikian
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-22 00:00:00.000000000 Z
11
+ date: 2015-10-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nanoc
@@ -102,6 +102,14 @@ files:
102
102
  - test/conref_fs_test.rb
103
103
  - test/datafiles_test.rb
104
104
  - test/fixtures/Rules
105
+ - test/fixtures/content/attributes/attribute.html
106
+ - test/fixtures/content/attributes/attribute.md
107
+ - test/fixtures/content/children/array_children.html
108
+ - test/fixtures/content/children/array_children.md
109
+ - test/fixtures/content/children/hash_children.html
110
+ - test/fixtures/content/children/hash_children.md
111
+ - test/fixtures/content/children/later_hash_children.html
112
+ - test/fixtures/content/children/later_hash_children.md
105
113
  - test/fixtures/content/datafiles/deep.html
106
114
  - test/fixtures/content/datafiles/deep.md
107
115
  - test/fixtures/content/datafiles/retrieve.html
@@ -116,6 +124,8 @@ files:
116
124
  - test/fixtures/content/obfuscation/admonitions.md
117
125
  - test/fixtures/content/obfuscation/octicon.html
118
126
  - test/fixtures/content/obfuscation/octicon.md
127
+ - test/fixtures/content/parents/array_parents.html
128
+ - test/fixtures/content/parents/array_parents.md
119
129
  - test/fixtures/content/parents/missing_title.html
120
130
  - test/fixtures/content/parents/missing_title.md
121
131
  - test/fixtures/content/parents/single_parent.html
@@ -123,14 +133,17 @@ files:
123
133
  - test/fixtures/content/parents/two_parents.html
124
134
  - test/fixtures/content/parents/two_parents.md
125
135
  - test/fixtures/data/categories/category.yml
136
+ - test/fixtures/data/categories/simple.yml
126
137
  - test/fixtures/data/reusables/intro.yml
127
138
  - test/fixtures/data/reusables/names.yml
128
139
  - test/fixtures/data/variables/empty.yml
129
140
  - test/fixtures/data/variables/product.yml
141
+ - test/fixtures/layouts/children.html
130
142
  - test/fixtures/layouts/default.html
131
143
  - test/fixtures/layouts/retrieve.html
132
144
  - test/fixtures/nanoc.yaml
133
145
  - test/test_helper.rb
146
+ - test/variable_mixin_test.rb
134
147
  homepage: https://github.com/gjtorikian/nanoc-conref-fs
135
148
  licenses:
136
149
  - MIT
@@ -159,6 +172,14 @@ test_files:
159
172
  - test/conref_fs_test.rb
160
173
  - test/datafiles_test.rb
161
174
  - test/fixtures/Rules
175
+ - test/fixtures/content/attributes/attribute.html
176
+ - test/fixtures/content/attributes/attribute.md
177
+ - test/fixtures/content/children/array_children.html
178
+ - test/fixtures/content/children/array_children.md
179
+ - test/fixtures/content/children/hash_children.html
180
+ - test/fixtures/content/children/hash_children.md
181
+ - test/fixtures/content/children/later_hash_children.html
182
+ - test/fixtures/content/children/later_hash_children.md
162
183
  - test/fixtures/content/datafiles/deep.html
163
184
  - test/fixtures/content/datafiles/deep.md
164
185
  - test/fixtures/content/datafiles/retrieve.html
@@ -173,6 +194,8 @@ test_files:
173
194
  - test/fixtures/content/obfuscation/admonitions.md
174
195
  - test/fixtures/content/obfuscation/octicon.html
175
196
  - test/fixtures/content/obfuscation/octicon.md
197
+ - test/fixtures/content/parents/array_parents.html
198
+ - test/fixtures/content/parents/array_parents.md
176
199
  - test/fixtures/content/parents/missing_title.html
177
200
  - test/fixtures/content/parents/missing_title.md
178
201
  - test/fixtures/content/parents/single_parent.html
@@ -180,11 +203,14 @@ test_files:
180
203
  - test/fixtures/content/parents/two_parents.html
181
204
  - test/fixtures/content/parents/two_parents.md
182
205
  - test/fixtures/data/categories/category.yml
206
+ - test/fixtures/data/categories/simple.yml
183
207
  - test/fixtures/data/reusables/intro.yml
184
208
  - test/fixtures/data/reusables/names.yml
185
209
  - test/fixtures/data/variables/empty.yml
186
210
  - test/fixtures/data/variables/product.yml
211
+ - test/fixtures/layouts/children.html
187
212
  - test/fixtures/layouts/default.html
188
213
  - test/fixtures/layouts/retrieve.html
189
214
  - test/fixtures/nanoc.yaml
190
215
  - test/test_helper.rb
216
+ - test/variable_mixin_test.rb