class Bridgetown::Reader
Attributes
@return [Bridgetown::Site]
Public Class Methods
# File lib/bridgetown-core/reader.rb, line 8 def initialize(site) @site = site end
Public Instance Methods
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'.
entries - The Array of String file/directory entries to filter. base_directory - The string representing the optional base directory.
Returns the Array of filtered entries.
# File lib/bridgetown-core/reader.rb, line 150 def filter_entries(entries, base_directory = nil) EntryFilter.new(site, base_directory: base_directory).filter(entries) end
Read the entries from a particular directory for processing
dir - The String representing the relative path of the directory to read. subfolder - The String representing the directory to read.
Returns the list of entries to process
# File lib/bridgetown-core/reader.rb, line 160 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 Site
data from disk and load it into internal data structures.
Returns nothing.
# File lib/bridgetown-core/reader.rb, line 15 def read # rubocop:todo Metrics/AbcSize site.defaults_reader.read site.layouts = LayoutReader.new(site).read read_directories read_included_excludes sort_files! site.data = if site.uses_resource? site.collections.data.read site.collections.data.merge_data_resources else DataReader.new(site).read end read_collections Bridgetown::PluginManager.source_manifests.map(&:content).compact.each do |plugin_content_dir| PluginContentReader.new(site, plugin_content_dir).read end end
# File lib/bridgetown-core/reader.rb, line 33 def read_collections site.collections.each_value do |collection| next if site.uses_resource? && collection.data? next if !site.uses_resource? && collection.legacy_reader? collection.read end end
Recursively traverse directories to find pages and static files that will become part of the site according to the rules in filter_entries.
dir - The String relative path of the directory to read. Default: ''.
Returns nothing.
# File lib/bridgetown-core/reader.rb, line 56 def read_directories(dir = "") base = site.in_source_dir(dir) return unless File.directory?(base) dot_dirs = [] dot_pages = [] dot_static_files = [] dot = Dir.chdir(base) { filter_entries(Dir.entries("."), base) } dot.each do |entry| file_path = @site.in_source_dir(base, entry) if File.directory?(file_path) dot_dirs << entry elsif Utils.has_yaml_header?(file_path) || Utils.has_rbfm_header?(file_path) dot_pages << entry else dot_static_files << entry end end retrieve_posts(dir) unless site.uses_resource? retrieve_dirs(base, dir, dot_dirs) retrieve_pages(dir, dot_pages) retrieve_static_files(dir, dot_static_files) end
Recursively traverse directories with the read_directories
function.
base - The String representing the site's base directory. dir - The String representing the directory to traverse down. dot_dirs - The Array of subdirectories in the dir.
Returns nothing.
# File lib/bridgetown-core/reader.rb, line 102 def retrieve_dirs(_base, dir, dot_dirs) dot_dirs.each do |file| dir_path = site.in_source_dir(dir, file) rel_path = File.join(dir, file) @site.reader.read_directories(rel_path) unless @site.dest.chomp("/") == dir_path end end
Retrieve all the pages from the current directory, add them to the site and sort them.
dir - The String representing the directory retrieve the pages from. dot_pages - The Array of pages in the dir.
Returns nothing.
# File lib/bridgetown-core/reader.rb, line 117 def retrieve_pages(dir, dot_pages) if site.uses_resource? dot_pages.each do |page_path| site.collections.pages.read_resource(site.in_source_dir(dir, page_path)) end return end site.pages.concat(PageReader.new(site, dir).read(dot_pages)) end
Retrieves all the posts(posts) from the given directory and add them to the site and sort them.
dir - The String representing the directory to retrieve the posts from.
Returns nothing.
# File lib/bridgetown-core/reader.rb, line 89 def retrieve_posts(dir) return if outside_configured_directory?(dir) post_reader.read_posts(dir) end
Retrieve all the static files from the current directory, add them to the site and sort them.
@param dir [String] The directory retrieve the static files from. @param files [Array<String>] The static files in the dir.
# File lib/bridgetown-core/reader.rb, line 133 def retrieve_static_files(dir, files) site.static_files.concat( files.map do |file| StaticFile.new(site, site.source, dir, file) end ) end
Sorts posts, pages, and static files.
# File lib/bridgetown-core/reader.rb, line 43 def sort_files! site.collections.posts.docs.sort! unless site.uses_resource? site.generated_pages.sort_by!(&:name) site.static_files.sort_by!(&:relative_path) end
Private Instance Methods
Internal
Determine if the directory is supposed to contain posts. If the user has defined a custom collections_dir, then attempt to read posts only from within that directory.
Returns true if a custom collections_dir has been set but current directory lies outside that directory.
# File lib/bridgetown-core/reader.rb, line 178 def outside_configured_directory?(dir) collections_dir = site.config["collections_dir"] !collections_dir.empty? && !dir.start_with?("/#{collections_dir}") end
Create a single PostReader
instance to retrieve posts from all valid directories in current site.
# File lib/bridgetown-core/reader.rb, line 185 def post_reader @post_reader ||= PostReader.new(site) end
# File lib/bridgetown-core/reader.rb, line 189 def read_included_excludes site.config.include.each do |entry| next if entry == ".htaccess" entry_path = site.in_source_dir(entry) next if File.directory?(entry_path) read_included_file(entry_path) if File.file?(entry_path) end end
# File lib/bridgetown-core/reader.rb, line 200 def read_included_file(entry_path) dir = File.dirname(entry_path).sub(site.source, "") file = Array(File.basename(entry_path)) if Utils.has_yaml_header?(entry_path) # TODO: does this need to get incorporated into the resource engine? site.pages.concat(PageReader.new(site, dir).read(file)) else retrieve_static_files(dir, file) end end