Class: Bridgetown::Builders::DocumentBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/bridgetown-builder/document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, path) ⇒ DocumentBuilder

Returns a new instance of DocumentBuilder.



8
9
10
11
12
# File 'lib/bridgetown-builder/document.rb', line 8

def initialize(site, path)
  @site = site
  @path = path
  @data = HashWithDotAccess::Hash.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(key, value = nil) ⇒ Object

rubocop:disable Style/MissingRespondToMissing



53
54
55
56
57
58
59
# File 'lib/bridgetown-builder/document.rb', line 53

def method_missing(key, value = nil)
  if respond_to?(key)
    super
  else
    @data[key] = value
  end
end

Instance Attribute Details

#siteObject (readonly)

Returns the value of attribute site.



6
7
8
# File 'lib/bridgetown-builder/document.rb', line 6

def site
  @site
end

Instance Method Details

#_add_document_to_siteObject

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



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
# File 'lib/bridgetown-builder/document.rb', line 19

def _add_document_to_site
  @collection = (@data[:collection] || :posts).to_s
  collection = @site.collections[@collection]
  unless collection
    collection = Collection.new(@site, @collection)
    collection.["output"] = true
    @site.collections[@collection] = collection
  end

  doc = Document.new(
    File.join(collection.directory, @path),
    site: @site,
    collection: collection
  )
  doc.send(:merge_defaults)
  doc.content = @data[:content]
  @data.delete(:content)

  if @path.start_with?("/")
    pathname = Pathname.new(@path)
    @data[:permalink] = File.join(
      pathname.dirname,
      pathname.basename.sub(pathname.extname, "")
    ) + "/"
  end

  doc.merge_data!(@data)
  doc.send(:read_post_data)

  collection.docs << doc
end

#front_matter(data) ⇒ Object



14
15
16
# File 'lib/bridgetown-builder/document.rb', line 14

def front_matter(data)
  @data.merge!(data)
end