Class: Yarrow::Content::CollectionExpander

Inherits:
Object
  • Object
show all
Includes:
Tools::FrontMatter
Defined in:
lib/yarrow/content/collection_expander.rb

Instance Method Summary collapse

Methods included from Tools::FrontMatter

#extract_split_content, #read_split_content

Constructor Details

#initialize(content_types = nil) ⇒ CollectionExpander

Returns a new instance of CollectionExpander.



6
7
8
9
10
# File 'lib/yarrow/content/collection_expander.rb', line 6

def initialize(content_types=nil)
  @content_types = content_types || [
    Yarrow::Content::ContentType.from_name(:pages)
  ]
end

Instance Method Details

#build_content_nodes(graph, objects, type, parent_index) ⇒ Object



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
162
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
# File 'lib/yarrow/content/collection_expander.rb', line 126

def build_content_nodes(graph, objects, type, parent_index)
  # TODO: this may need to use a strategy that can be overriden
  content_type = Yarrow::Symbols.to_singular(type)

  # Process collected content objects and generate entity nodes
  objects.each do |name, sources|
    item_node = graph.create_node do |node|
      # TODO: Rename this to :entry and support similar fields to Atom
      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]
        # If a URL is explicitly proided in metadata then use it
        node.props[:url] = meta[:url]
      elsif meta[:permalink]
        # Support for legacy permalink attribute
        node.props[:url] = meta[:permalink]
      else
        # Default URL generation strategy when no explicit URL is provided
        # TODO: collection nodes need URL generation too
        # TODO: replace this with URL generation strategy
        # TODO: slug vs name - why do some nodes have 2 and some 3 props?
        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

      # For now, we are storing title, url, etc on the top-level item.
      node.props[:title] = meta[:title]

      # TODO: What belongs on the entity and what belongs on the item?
      entity_props = meta.merge(body: content, name: meta[:id], url: node.props[:url])


      # TODO: consider whether to provide `body` on the item/document or at
      # the custom content type level.
      begin
        content_struct = Yarrow::Symbols.to_const(content_type)
      rescue
        # No immutable struct found: fall back to slower dynamically typed open struct
        require "ostruct"
        content_struct = OpenStruct
      end

      node.props[:entity] = content_struct.new(entity_props)
    end

    # Connect entity with source content
    sources.each do |source|
      graph.create_edge do |edge|
        edge.label = :source
        edge.from = item_node
        edge.to = source.id
      end
    end

    # Connect entity with parent collection
    graph.create_edge do |edge|
      edge.label = :child
      edge.from = parent_index
      edge.to = item_node
    end
  end
end

#expand(graph) ⇒ Object



12
13
14
15
16
# File 'lib/yarrow/content/collection_expander.rb', line 12

def expand(graph)
  @content_types.each do |content_type|
    expand_nested(graph, content_type)
  end
end

#expand_nested(graph, content_type) ⇒ Object



18
19
20
21
22
23
24
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
# File 'lib/yarrow/content/collection_expander.rb', line 18

def expand_nested(graph, content_type)
  type = content_type.collection
  exts = content_type.extensions

  # If match path represents entire content dir, then include the entire
  # content dir instead of scanning from a subfolder matching the name of
  # the collection.
  start_node = if content_type.match_path == "."
    graph.n(:root)
  else
    graph.n(:root).out(name: type.to_s)
  end

  # Extract metadata from given start node
  data = (start_node, type)

  # Collects all nested collections in the subgraph for this content type
  subcollections = {}
  index = nil

  # Define alias for accessing metadata in the loop
   = data

  # Scan and collect all nested directories under the top level source
  start_node.depth_first.each do |node|
    if node.label == :directory
      # Check if this entry has metadata defined at the top level
      if data[:collections]
        item = data[:collections].find { |c| c[:slug] == node.props[:slug] }
         = item if item
      end

      # Create a collection node representing a collection of documents
      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] = [:title]

        # Override default status so that mapped index collections always show
        # up in the resulting document manifest, when they don’t have
        # associated metadata. This is the opposite of how individual pieces
        # of content behave (default to draft status if one isn’t supplied).
        collection_node.props[:status] = if data[:status]
          data[:status]
        else
          "published"
        end

        # TODO: URL generation might need to happen elsewhere
        collection_node.props[:url] = if data[:url]
          data[:url]
        else
          "#{node.props[:path].split('./content').last}/"
        end
      end

      # Add this collection id to the lookup table for edge construction
      subcollections[node.props[:path]] = index

      # Join the collection to its parent
      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

  subcollections.each do |path, index|
    # Group files matching the same slug under a common key
    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

#extract_metadata(node, type) ⇒ Object

Extract collection level configuration/metadata from the root node for this content type.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/yarrow/content/collection_expander.rb', line 104

def (node, type)
  # TODO: support _index or _slug convention as well
  meta_file = node.out(slug: type.to_s).first

  if meta_file
    # Process metadata and add it to the collection node
    # TODO: pass in content converter object
    # TODO: index/body content by default if extracted from frontmatter
    body, data = process_content(meta_file.props[:entry])
  else
    # Otherwise, assume default collection behaviour
    data = {}
  end

  # Generate a default title if not provided in metadata
  unless data.key?(:title)
    data[:title] = type.to_s.capitalize
  end

  data
end

#process_content(path) ⇒ Object

Workaround for handling meta and content source in multiple files or a single file with front matter.



205
206
207
208
209
210
211
212
213
214
215
# File 'lib/yarrow/content/collection_expander.rb', line 205

def process_content(path)
  case path.extname
  when '.htm', '.md'
    read_split_content(path.to_s, symbolize_keys: true)
  # when '.md'
  #   body, data = read_split_content(path.to_s, symbolize_keys: true)
  #   [Kramdown::Document.new(body).to_html, data]
  when '.yml'
    [nil, YAML.load(File.read(path.to_s), symbolize_names: true)]
  end
end