This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Jekyll
|
||||
class CollectionReader
|
||||
SPECIAL_COLLECTIONS = %w(posts data).freeze
|
||||
|
||||
attr_reader :site, :content
|
||||
|
||||
def initialize(site)
|
||||
@site = site
|
||||
@content = {}
|
||||
end
|
||||
|
||||
# Read in all collections specified in the configuration
|
||||
#
|
||||
# Returns nothing.
|
||||
def read
|
||||
site.collections.each_value do |collection|
|
||||
collection.read unless SPECIAL_COLLECTIONS.include?(collection.label)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,113 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Jekyll
|
||||
class DataReader
|
||||
attr_reader :site, :content
|
||||
|
||||
def initialize(site, in_source_dir: nil)
|
||||
@site = site
|
||||
@content = {}
|
||||
@entry_filter = EntryFilter.new(site)
|
||||
@in_source_dir = in_source_dir || @site.method(:in_source_dir)
|
||||
@source_dir = @in_source_dir.call("/")
|
||||
end
|
||||
|
||||
# Read all the files in <dir> and adds them to @content
|
||||
#
|
||||
# dir - The String relative path of the directory to read.
|
||||
#
|
||||
# Returns @content, a Hash of the .yaml, .yml,
|
||||
# .json, and .csv files in the base directory
|
||||
def read(dir)
|
||||
base = @in_source_dir.call(dir)
|
||||
read_data_to(base, @content)
|
||||
@content
|
||||
end
|
||||
|
||||
# Read and parse all .yaml, .yml, .json, .csv and .tsv
|
||||
# files under <dir> and add them to the <data> variable.
|
||||
#
|
||||
# dir - The string absolute path of the directory to read.
|
||||
# data - The variable to which data will be added.
|
||||
#
|
||||
# Returns nothing
|
||||
def read_data_to(dir, data)
|
||||
return unless File.directory?(dir) && !@entry_filter.symlink?(dir)
|
||||
|
||||
entries = Dir.chdir(dir) do
|
||||
Dir["*.{yaml,yml,json,csv,tsv}"] + Dir["*"].select { |fn| File.directory?(fn) }
|
||||
end
|
||||
|
||||
entries.each do |entry|
|
||||
path = @in_source_dir.call(dir, entry)
|
||||
next if @entry_filter.symlink?(path)
|
||||
|
||||
if File.directory?(path)
|
||||
read_data_to(path, data[sanitize_filename(entry)] = {})
|
||||
else
|
||||
key = sanitize_filename(File.basename(entry, ".*"))
|
||||
data[key] = read_data_file(path)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Determines how to read a data file.
|
||||
#
|
||||
# Returns the contents of the data file.
|
||||
def read_data_file(path)
|
||||
Jekyll.logger.debug "Reading:", path.sub(@source_dir, "")
|
||||
|
||||
case File.extname(path).downcase
|
||||
when ".csv"
|
||||
CSV.read(path, **csv_config).map { |row| convert_row(row) }
|
||||
when ".tsv"
|
||||
CSV.read(path, **tsv_config).map { |row| convert_row(row) }
|
||||
else
|
||||
SafeYAML.load_file(path)
|
||||
end
|
||||
end
|
||||
|
||||
def sanitize_filename(name)
|
||||
name.gsub(%r![^\w\s-]+|(?<=^|\b\s)\s+(?=$|\s?\b)!, "")
|
||||
.gsub(%r!\s+!, "_")
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# @return [Hash]
|
||||
def csv_config
|
||||
@csv_config ||= read_config("csv_reader")
|
||||
end
|
||||
|
||||
# @return [Hash]
|
||||
def tsv_config
|
||||
@tsv_config ||= read_config("tsv_reader", { :col_sep => "\t" })
|
||||
end
|
||||
|
||||
# @param config_key [String]
|
||||
# @param overrides [Hash]
|
||||
# @return [Hash]
|
||||
# @see https://ruby-doc.org/stdlib-2.5.0/libdoc/csv/rdoc/CSV.html#Converters
|
||||
def read_config(config_key, overrides = {})
|
||||
reader_config = config[config_key] || {}
|
||||
|
||||
defaults = {
|
||||
:converters => reader_config.fetch("csv_converters", []).map(&:to_sym),
|
||||
:headers => reader_config.fetch("headers", true),
|
||||
:encoding => reader_config.fetch("encoding", config["encoding"]),
|
||||
}
|
||||
|
||||
defaults.merge(overrides)
|
||||
end
|
||||
|
||||
def config
|
||||
@config ||= site.config
|
||||
end
|
||||
|
||||
# @param row [Array, CSV::Row]
|
||||
# @return [Array, Hash]
|
||||
def convert_row(row)
|
||||
row.instance_of?(CSV::Row) ? row.to_hash : row
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,62 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Jekyll
|
||||
class LayoutReader
|
||||
attr_reader :site
|
||||
|
||||
def initialize(site)
|
||||
@site = site
|
||||
@layouts = {}
|
||||
end
|
||||
|
||||
def read
|
||||
layout_entries.each do |layout_file|
|
||||
@layouts[layout_name(layout_file)] = \
|
||||
Layout.new(site, layout_directory, layout_file)
|
||||
end
|
||||
|
||||
theme_layout_entries.each do |layout_file|
|
||||
@layouts[layout_name(layout_file)] ||= \
|
||||
Layout.new(site, theme_layout_directory, layout_file)
|
||||
end
|
||||
|
||||
@layouts
|
||||
end
|
||||
|
||||
def layout_directory
|
||||
@layout_directory ||= site.in_source_dir(site.config["layouts_dir"])
|
||||
end
|
||||
|
||||
def theme_layout_directory
|
||||
@theme_layout_directory ||= site.theme.layouts_path if site.theme
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def layout_entries
|
||||
entries_in layout_directory
|
||||
end
|
||||
|
||||
def theme_layout_entries
|
||||
theme_layout_directory ? entries_in(theme_layout_directory) : []
|
||||
end
|
||||
|
||||
def entries_in(dir)
|
||||
entries = []
|
||||
within(dir) do
|
||||
entries = EntryFilter.new(site).filter(Dir["**/*.*"])
|
||||
end
|
||||
entries
|
||||
end
|
||||
|
||||
def layout_name(file)
|
||||
file.split(".")[0..-2].join(".")
|
||||
end
|
||||
|
||||
def within(directory)
|
||||
return unless File.exist?(directory)
|
||||
|
||||
Dir.chdir(directory) { yield }
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,25 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Jekyll
|
||||
class PageReader
|
||||
attr_reader :site, :dir, :unfiltered_content
|
||||
|
||||
def initialize(site, dir)
|
||||
@site = site
|
||||
@dir = dir
|
||||
@unfiltered_content = []
|
||||
end
|
||||
|
||||
# Create a new `Jekyll::Page` object for each entry in a given array.
|
||||
#
|
||||
# files - An array of file names inside `@dir`
|
||||
#
|
||||
# Returns an array of publishable `Jekyll::Page` objects.
|
||||
def read(files)
|
||||
files.each do |page|
|
||||
@unfiltered_content << Page.new(@site, @site.source, @dir, page)
|
||||
end
|
||||
@unfiltered_content.select { |page| site.publisher.publish?(page) }
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,85 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Jekyll
|
||||
class PostReader
|
||||
attr_reader :site, :unfiltered_content
|
||||
|
||||
def initialize(site)
|
||||
@site = site
|
||||
end
|
||||
|
||||
# Read all the files in <source>/<dir>/_drafts and create a new
|
||||
# Document object with each one.
|
||||
#
|
||||
# dir - The String relative path of the directory to read.
|
||||
#
|
||||
# Returns nothing.
|
||||
def read_drafts(dir)
|
||||
read_publishable(dir, "_drafts", Document::DATELESS_FILENAME_MATCHER)
|
||||
end
|
||||
|
||||
# Read all the files in <source>/<dir>/_posts and create a new Document
|
||||
# object with each one.
|
||||
#
|
||||
# dir - The String relative path of the directory to read.
|
||||
#
|
||||
# Returns nothing.
|
||||
def read_posts(dir)
|
||||
read_publishable(dir, "_posts", Document::DATE_FILENAME_MATCHER)
|
||||
end
|
||||
|
||||
# Read all the files in <source>/<dir>/<magic_dir> and create a new
|
||||
# Document object with each one insofar as it matches the regexp matcher.
|
||||
#
|
||||
# dir - The String relative path of the directory to read.
|
||||
#
|
||||
# Returns nothing.
|
||||
def read_publishable(dir, magic_dir, matcher)
|
||||
read_content(dir, magic_dir, matcher)
|
||||
.tap { |docs| docs.each(&:read) }
|
||||
.select { |doc| processable?(doc) }
|
||||
end
|
||||
|
||||
# Read all the content files from <source>/<dir>/magic_dir
|
||||
# and return them with the type klass.
|
||||
#
|
||||
# dir - The String relative path of the directory to read.
|
||||
# magic_dir - The String relative directory to <dir>,
|
||||
# looks for content here.
|
||||
# klass - The return type of the content.
|
||||
#
|
||||
# Returns klass type of content files
|
||||
def read_content(dir, magic_dir, matcher)
|
||||
@site.reader.get_entries(dir, magic_dir).map do |entry|
|
||||
next unless matcher.match?(entry)
|
||||
|
||||
path = @site.in_source_dir(File.join(dir, magic_dir, entry))
|
||||
Document.new(path,
|
||||
:site => @site,
|
||||
:collection => @site.posts)
|
||||
end.tap(&:compact!)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def processable?(doc)
|
||||
if doc.content.nil?
|
||||
Jekyll.logger.debug "Skipping:", "Content in #{doc.relative_path} is nil"
|
||||
false
|
||||
elsif !doc.content.valid_encoding?
|
||||
Jekyll.logger.debug "Skipping:", "#{doc.relative_path} is not valid UTF-8"
|
||||
false
|
||||
else
|
||||
publishable?(doc)
|
||||
end
|
||||
end
|
||||
|
||||
def publishable?(doc)
|
||||
site.publisher.publish?(doc).tap do |will_publish|
|
||||
if !will_publish && site.publisher.hidden_in_the_future?(doc)
|
||||
Jekyll.logger.warn "Skipping:", "#{doc.relative_path} has a future date"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,25 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Jekyll
|
||||
class StaticFileReader
|
||||
attr_reader :site, :dir, :unfiltered_content
|
||||
|
||||
def initialize(site, dir)
|
||||
@site = site
|
||||
@dir = dir
|
||||
@unfiltered_content = []
|
||||
end
|
||||
|
||||
# Create a new StaticFile object for every entry in a given list of basenames.
|
||||
#
|
||||
# files - an array of file basenames.
|
||||
#
|
||||
# Returns an array of static files.
|
||||
def read(files)
|
||||
files.each do |file|
|
||||
@unfiltered_content << StaticFile.new(@site, @site.source, @dir, file)
|
||||
end
|
||||
@unfiltered_content
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,52 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Jekyll
|
||||
class ThemeAssetsReader
|
||||
attr_reader :site
|
||||
|
||||
def initialize(site)
|
||||
@site = site
|
||||
end
|
||||
|
||||
def read
|
||||
return unless site.theme&.assets_path
|
||||
|
||||
Find.find(site.theme.assets_path) do |path|
|
||||
next if File.directory?(path)
|
||||
|
||||
if File.symlink?(path)
|
||||
Jekyll.logger.warn "Theme reader:", "Ignored symlinked asset: #{path}"
|
||||
else
|
||||
read_theme_asset(path)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def read_theme_asset(path)
|
||||
base = site.theme.root
|
||||
dir = File.dirname(path.sub("#{site.theme.root}/", ""))
|
||||
name = File.basename(path)
|
||||
|
||||
if Utils.has_yaml_header?(path)
|
||||
append_unless_exists site.pages,
|
||||
Jekyll::Page.new(site, base, dir, name)
|
||||
else
|
||||
append_unless_exists site.static_files,
|
||||
Jekyll::StaticFile.new(site, base, "/#{dir}", name)
|
||||
end
|
||||
end
|
||||
|
||||
def append_unless_exists(haystack, new_item)
|
||||
if haystack.any? { |file| file.relative_path == new_item.relative_path }
|
||||
Jekyll.logger.debug "Theme:",
|
||||
"Ignoring #{new_item.relative_path} in theme due to existing file " \
|
||||
"with that path in site."
|
||||
return
|
||||
end
|
||||
|
||||
haystack << new_item
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user