Class: Yarrow::Content::CollectionExpander
- Inherits:
-
Object
- Object
- Yarrow::Content::CollectionExpander
show all
- Includes:
- Tools::FrontMatter
- Defined in:
- lib/yarrow/content/collection_expander.rb
Instance Method Summary
collapse
#extract_split_content, #read_split_content
Constructor Details
If a list of object types is not provided, a default pages type is created.
8
9
10
11
12
|
# File 'lib/yarrow/content/collection_expander.rb', line 8
def initialize(object_types=nil)
@object_types = object_types || [
Yarrow::Content::ObjectType.from_name(:pages)
]
end
|
Instance Method Details
#build_content_nodes(graph, objects, type, parent_index) ⇒ Object
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
# File 'lib/yarrow/content/collection_expander.rb', line 163
def build_content_nodes(graph, objects, type, parent_index)
content_type = Yarrow::Symbols.to_singular(type)
objects.each do |name, sources|
item_node = graph.create_node do |node|
node.label = :item
node.props[:name] = name
node.props[:type] = content_type
meta = {}
content = ""
sources.each do |source|
body, data = process_content(source.props[:entry])
meta.merge!(data) unless data.nil?
content << body unless body.nil?
end
if meta[:url]
node.props[:url] = meta[:url]
elsif meta[:permalink]
node.props[:url] = meta[:permalink]
else
node.props[:url] = if parent_index.props[:name].to_sym == parent_index.props[:type]
"/#{parent_index.props[:type]}/#{name}"
else
"/#{parent_index.props[:type]}/#{parent_index.props[:slug]}/#{name}"
end
end
node.props[:title] = meta[:title]
entity_props = meta.merge(body: content, name: meta[:id], url: node.props[:url])
begin
content_struct = Yarrow::Symbols.to_const(content_type)
rescue
require "ostruct"
content_struct = OpenStruct
end
node.props[:entity] = content_struct.new(entity_props)
end
sources.each do |source|
graph.create_edge do |edge|
edge.label = :source
edge.from = item_node
edge.to = source.id
end
end
graph.create_edge do |edge|
edge.label = :child
edge.from = parent_index
edge.to = item_node
end
end
end
|
#expand(graph) ⇒ Object
14
15
16
17
18
|
# File 'lib/yarrow/content/collection_expander.rb', line 14
def expand(graph)
@object_types.each do |object_type|
expand_nested(graph, object_type)
end
end
|
#expand_nested(graph, content_type) ⇒ Object
20
21
22
23
|
# File 'lib/yarrow/content/collection_expander.rb', line 20
def expand_nested(graph, content_type)
strategy = TreeExpansion.new(graph)
strategy.expand(content_type)
end
|
#expand_nested_legacy(graph, content_type) ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
# File 'lib/yarrow/content/collection_expander.rb', line 25
def expand_nested_legacy(graph, content_type)
type = content_type.collection
exts = content_type.extensions
start_node = if content_type.match_path == "."
graph.n(:root)
else
graph.n(:root).out(name: type.to_s)
end
data = (start_node, type)
subcollections = {}
index = nil
metadata = data
start_node.depth_first.each do |node|
if node.label == :directory
if data[:collections]
item = data[:collections].find { |c| c[:slug] == node.props[:slug] }
metadata = item if item
end
index = graph.create_node do |collection_node|
collection_node.label = :collection
collection_node.props[:type] = type
collection_node.props[:name] = node.props[:name]
collection_node.props[:slug] = node.props[:slug]
collection_node.props[:title] = metadata[:title]
collection_node.props[:status] = if data[:status]
data[:status]
else
"published"
end
collection_node.props[:url] = if data[:url]
data[:url]
else
"#{node.props[:path].split('./content').last}/"
end
end
subcollections[node.props[:path]] = index
unless node.props[:slug] == type.to_s || !subcollections.key?(node.props[:entry].parent.to_s)
graph.create_edge do |edge|
edge.label = :child
edge.from = subcollections[node.props[:entry].parent.to_s].id
edge.to = index.id
end
end
end
end
if subcollections.empty?
objects = start_node.out(:file).all.select do |file|
file.props[:name].end_with?(*exts)
end.group_by do |file|
file.props[:slug]
end
if index.nil?
index = graph.create_node do |collection_node|
collection_node.label = :collection
collection_node.props[:type] = type
collection_node.props[:name] = type
collection_node.props[:slug] = type.to_s
collection_node.props[:title] = metadata[:title]
collection_node.props[:status] = if data[:status]
data[:status]
else
"published"
end
collection_node.props[:url] = if data[:url]
data[:url]
else
"/#{type}/"
end
end
end
build_content_nodes(graph, objects, type, index)
end
subcollections.each do |path, index|
objects = graph.n(path: path).out(:file).all.select do |file|
file.props[:name].end_with?(*exts)
end.group_by do |file|
file.props[:slug]
end
build_content_nodes(graph, objects, type, index)
end
end
|