Class: Bridgetown::PluginContentReader

Inherits:
Object
  • Object
show all
Defined in:
bridgetown-core/lib/bridgetown-core/readers/plugin_content_reader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, manifest) ⇒ PluginContentReader

Returns a new instance of PluginContentReader.

Parameters:



9
10
11
12
13
14
15
16
17
18
# File 'bridgetown-core/lib/bridgetown-core/readers/plugin_content_reader.rb', line 9

def initialize(site, manifest)
  @site = site
  @manifest = manifest
  @content_dirs = if manifest.contents
                    manifest.contents
                  elsif manifest.content
                    { pages: manifest.content }
                  end
  @content_files = Set.new
end

Instance Attribute Details

#content_dirsObject (readonly)

Returns the value of attribute content_dirs.



5
6
7
# File 'bridgetown-core/lib/bridgetown-core/readers/plugin_content_reader.rb', line 5

def content_dirs
  @content_dirs
end

#manifestObject (readonly)

Returns the value of attribute manifest.



5
6
7
# File 'bridgetown-core/lib/bridgetown-core/readers/plugin_content_reader.rb', line 5

def manifest
  @manifest
end

#siteObject (readonly)

Returns the value of attribute site.



5
6
7
# File 'bridgetown-core/lib/bridgetown-core/readers/plugin_content_reader.rb', line 5

def site
  @site
end

Instance Method Details

#add_to(content_type, klass) ⇒ Object



61
62
63
64
65
66
# File 'bridgetown-core/lib/bridgetown-core/readers/plugin_content_reader.rb', line 61

def add_to(content_type, klass)
  existing_paths = content_type.filter_map(&:relative_path)
  @content_files.select { |item| item.is_a?(klass) }.each do |item|
    content_type << item unless existing_paths.include?(item.relative_path)
  end
end

#readObject



20
21
22
23
24
25
26
# File 'bridgetown-core/lib/bridgetown-core/readers/plugin_content_reader.rb', line 20

def read
  return if content_dirs.empty?

  content_dirs.each do |collection_name, root|
    read_content_root collection_name, root
  end
end

#read_content_file(content_dir, path, collection) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'bridgetown-core/lib/bridgetown-core/readers/plugin_content_reader.rb', line 49

def read_content_file(content_dir, path, collection)
  dir = File.dirname(path.sub("#{content_dir}/", ""))
  name = File.basename(path)

  @content_files << if FrontMatter::Loaders.front_matter?(path)
                      collection.read_resource(path, manifest:)
                    else
                      Bridgetown::StaticFile.new(site, content_dir, "/#{dir}", name)
                    end
  add_to(site.static_files, Bridgetown::StaticFile)
end

#read_content_root(collection_name, content_dir) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'bridgetown-core/lib/bridgetown-core/readers/plugin_content_reader.rb', line 28

def read_content_root(collection_name, content_dir)
  collection = site.collections[collection_name]
  unless collection
    Bridgetown.logger.warn(
      "Reading",
      "Plugin requested missing collection #{collection_name}, cannot continue"
    )
    return
  end

  Find.find(content_dir) do |path|
    next if File.directory?(path)

    if File.symlink?(path)
      Bridgetown.logger.warn "Plugin content reader:", "Ignored symlinked asset: #{path}"
    else
      read_content_file(content_dir, path, collection)
    end
  end
end