Class: Bridgetown::Reader
- Inherits:
-
Object
- Object
- Bridgetown::Reader
- Defined in:
- 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 'lib/bridgetown-core/reader.rb', line 9 def initialize(site) @site = site end |
Instance Attribute Details
#site ⇒ Bridgetown::Site (readonly)
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.
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
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 |
#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 '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 '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.
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_layouts ⇒ void
This method returns an undefined value.
Read in layouts
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.
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.
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.
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 |