Class: Bridgetown::Reader

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site) ⇒ Reader

Returns a new instance of Reader.

Parameters:



9
10
11
# File 'lib/bridgetown-core/reader.rb', line 9

def initialize(site)
  @site = site
end

Instance Attribute Details

#siteBridgetown::Site (readonly)

Returns:



6
7
8
# File 'lib/bridgetown-core/reader.rb', line 6

def site
  @site
end

Instance Method Details

#filter_entries(entries, base_directory = nil) ⇒ Object

Filter out any files/directories that are hidden or backup files (start with "." or "#" or end with "~"), or contain site content (start with "_"), or are excluded in the site configuration, unless they are web server files such as '.htaccess'.

Returns the Array of filtered entries.

Parameters:

  • entries (Array<String>)

    file/directory entries to filter

  • base_directory (String) (defaults to: nil)

    optional base directory



131
132
133
# File 'lib/bridgetown-core/reader.rb', line 131

def filter_entries(entries, base_directory = nil)
  EntryFilter.new(site, base_directory: base_directory).filter(entries)
end

#get_entries(dir, subfolder) ⇒ Object

Read the entries from a particular directory for processing

Returns the list of entries to process

Parameters:

  • dir (String)

    parent directory

  • subfolder (String)

    the directory to read



141
142
143
144
145
146
147
# File 'lib/bridgetown-core/reader.rb', line 141

def get_entries(dir, subfolder)
  base = site.in_source_dir(dir, subfolder)
  return [] unless File.exist?(base)

  entries = Dir.chdir(base) { filter_entries(Dir["**/*"], base) }
  entries.delete_if { |e| File.directory?(site.in_source_dir(base, e)) }
end

#readvoid

This method returns an undefined value.

Read data and resources from disk and load it into internal data structures.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/bridgetown-core/reader.rb', line 15

def read
  site.defaults_reader.read
  site.data = site.collections.data.read.merge_data_resources
  read_layouts
  read_directories
  read_includes
  sort_files!
  read_collections
  site.config.source_manifests.select(&:content).each do |manifest|
    PluginContentReader.new(site, manifest).read
  end
end

#read_collectionsvoid

This method returns an undefined value.

Read in collections (other than the data collection)



37
38
39
40
41
42
43
# File 'lib/bridgetown-core/reader.rb', line 37

def read_collections
  site.collections.each_value do |collection|
    next if collection.data?

    collection.read
  end
end

#read_directories(dir = "") ⇒ void

This method returns an undefined value.

Recursively traverse directories to find pages and static files that will become part of the site according to the rules in filter_entries.

Parameters:

  • dir (String) (defaults to: "")

    relative path of the directory to read. Default: ''



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
# File 'lib/bridgetown-core/reader.rb', line 58

def read_directories(dir = "")
  base = site.in_source_dir(dir)

  return unless File.directory?(base)

  entries_dirs = []
  entries_pages = []
  entries_static_files = []

  entries = Dir.chdir(base) { filter_entries(Dir.entries("."), base) }
  entries.each do |entry|
    file_path = @site.in_source_dir(base, entry)
    if File.directory?(file_path)
      entries_dirs << entry
    elsif Utils.has_yaml_header?(file_path) || Utils.has_rbfm_header?(file_path)
      entries_pages << entry
    else
      entries_static_files << entry
    end
  end

  retrieve_dirs(dir, entries_dirs)
  retrieve_pages(dir, entries_pages)
  retrieve_static_files(dir, entries_static_files)
end

#read_layoutsvoid

This method returns an undefined value.

Read in layouts

See Also:



31
32
33
# File 'lib/bridgetown-core/reader.rb', line 31

def read_layouts
  site.layouts = LayoutReader.new(site).read
end

#retrieve_dirs(dir, entries_dirs) ⇒ void

This method returns an undefined value.

Recursively traverse directories with the read_directories function.

Parameters:

  • dir (String)

    the directory to traverse down

  • entries_dirs (Array<String>)

    subdirectories in the directory



89
90
91
92
93
94
95
# File 'lib/bridgetown-core/reader.rb', line 89

def retrieve_dirs(dir, entries_dirs)
  entries_dirs.each do |file|
    dir_path = site.in_source_dir(dir, file)
    rel_path = File.join(dir, file)
    read_directories(rel_path) unless @site.destination.chomp("/") == dir_path
  end
end

#retrieve_pages(dir, entries_pages) ⇒ void

This method returns an undefined value.

Retrieve all the pages from the current directory, add them to the site and sort them.

Parameters:

  • dir (String)

    the directory to retrieve the pages from

  • entries_pages (Array<String>)

    page paths in the directory



103
104
105
106
107
# File 'lib/bridgetown-core/reader.rb', line 103

def retrieve_pages(dir, entries_pages)
  entries_pages.each do |page_path|
    site.collections.pages.read_resource(site.in_source_dir(dir, page_path))
  end
end

#retrieve_static_files(dir, files) ⇒ Object

Retrieve all the static files from the current directory, add them to the site and sort them.

Parameters:

  • dir (String)

    The directory retrieve the static files from.

  • files (Array<String>)

    The static files in the dir.



114
115
116
117
118
119
120
# File 'lib/bridgetown-core/reader.rb', line 114

def retrieve_static_files(dir, files)
  site.static_files.concat(
    files.map do |file|
      StaticFile.new(site, site.source, dir, file)
    end
  )
end

#sort_files!void

This method returns an undefined value.

Sorts generated pages and static files.



47
48
49
50
# File 'lib/bridgetown-core/reader.rb', line 47

def sort_files!
  site.generated_pages.sort_by!(&:name)
  site.static_files.sort_by!(&:relative_path)
end