Class: Bridgetown::Reader
- Inherits:
-
Object
- Object
- Bridgetown::Reader
- Defined in:
- bridgetown-core/lib/bridgetown-core/reader.rb
Instance Attribute Summary collapse
-
#site ⇒ Bridgetown::Site
readonly
Instance Method Summary collapse
-
#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’.
-
#get_entries(dir, subfolder) ⇒ Object
Read the entries from a particular directory for processing.
-
#initialize(site) ⇒ Reader
constructor
A new instance of Reader.
-
#read ⇒ void
Read data and resources from disk and load it into internal data structures.
-
#read_collections ⇒ void
Read in collections (other than the data collection).
-
#read_directories(dir = "") ⇒ void
Recursively traverse directories to find pages and static files that will become part of the site according to the rules in filter_entries.
-
#read_layouts ⇒ void
Read in layouts.
-
#retrieve_dirs(dir, entries_dirs) ⇒ void
Recursively traverse directories with the read_directories function.
-
#retrieve_pages(dir, entries_pages) ⇒ void
Retrieve all the pages from the current directory, add them to the site and sort them.
-
#retrieve_static_files(dir, files) ⇒ Object
Retrieve all the static files from the current directory, add them to the site and sort them.
-
#sort_files! ⇒ void
Sorts generated pages and static files.
Constructor Details
#initialize(site) ⇒ Reader
Returns a new instance of Reader.
9 10 11 |
# File 'bridgetown-core/lib/bridgetown-core/reader.rb', line 9 def initialize(site) @site = site end |
Instance Attribute Details
#site ⇒ Bridgetown::Site (readonly)
6 7 8 |
# File 'bridgetown-core/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.
133 134 135 |
# File 'bridgetown-core/lib/bridgetown-core/reader.rb', line 133 def filter_entries(entries, base_directory = nil) EntryFilter.new(site, 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
143 144 145 146 147 148 149 |
# File 'bridgetown-core/lib/bridgetown-core/reader.rb', line 143 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 |
#read ⇒ void
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 'bridgetown-core/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_collections ⇒ void
This method returns an undefined value.
Read in collections (other than the data collection)
37 38 39 40 41 42 43 |
# File 'bridgetown-core/lib/bridgetown-core/reader.rb', line 37 def read_collections site.collections.each_value do |collection| next if collection.data? collection.read unless site.ssr? && collection..skip_for_ssr 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.
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 'bridgetown-core/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 FrontMatter::Loaders.front_matter?(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) unless site.ssr? end |
#read_layouts ⇒ void
This method returns an undefined value.
Read in layouts
31 32 33 |
# File 'bridgetown-core/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.
89 90 91 92 93 94 95 |
# File 'bridgetown-core/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.
103 104 105 106 107 108 109 |
# File 'bridgetown-core/lib/bridgetown-core/reader.rb', line 103 def retrieve_pages(dir, entries_pages) return if site.ssr? && site.collections.pages..skip_for_ssr 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.
116 117 118 119 120 121 122 |
# File 'bridgetown-core/lib/bridgetown-core/reader.rb', line 116 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 'bridgetown-core/lib/bridgetown-core/reader.rb', line 47 def sort_files! site.generated_pages.sort_by!(&:name) site.static_files.sort_by!(&:relative_path) end |