class Bridgetown::PostReader

TODO: to be retired once the Resource engine is made official

Attributes

site[R]
unfiltered_content[R]

Public Class Methods

new(site) click to toggle source
# File lib/bridgetown-core/readers/post_reader.rb, line 8
def initialize(site)
  @site = site
end

Public Instance Methods

read_content(dir, magic_dir, matcher) click to toggle source

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

# File lib/bridgetown-core/readers/post_reader.rb, line 43
def read_content(dir, magic_dir, matcher)
  @site.reader.get_entries(dir, magic_dir).map do |entry|
    path = @site.in_source_dir(File.join(dir, magic_dir, entry))

    if matcher.match?(entry) || Utils.has_yaml_header?(path)
      # Process as Document
      Document.new(path,
                   site: @site,
                   collection: @site.collections.posts)
    else
      # Process as Static File
      read_static_file(
        path,
        File.join(dir, magic_dir, File.dirname(entry).sub(".", ""))
      )
    end
  end.reject(&:nil?)
end
read_posts(dir) click to toggle source

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.

# File lib/bridgetown-core/readers/post_reader.rb, line 18
def read_posts(dir)
  read_publishable(dir, "_posts", Document::DATE_FILENAME_MATCHER)
end
read_publishable(dir, magic_dir, matcher) click to toggle source

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.

@param dir [String] relative path of the directory to read.

@return [Array<Document, StaticFile>]

# File lib/bridgetown-core/readers/post_reader.rb, line 28
def read_publishable(dir, magic_dir, matcher)
  read_content(dir, magic_dir, matcher)
    .tap { |items| items.select { |item| item.respond_to?(:read) }.each(&:read) }
    .select { |item| item_added_to_site?(item) }
end

Private Instance Methods

item_added_to_site?(item) click to toggle source
# File lib/bridgetown-core/readers/post_reader.rb, line 96
def item_added_to_site?(item)
  return false unless processable?(item)

  if item.is_a?(Document)
    site.collections.posts.docs << item
  elsif item.is_a?(StaticFile)
    site.collections.posts.static_files << item
    site.static_files << item
  end

  true
end
processable?(item) click to toggle source
# File lib/bridgetown-core/readers/post_reader.rb, line 74
def processable?(item)
  return true if item.is_a?(StaticFile)

  if item.content.nil?
    Bridgetown.logger.debug "Skipping:", "Content in #{item.relative_path} is nil"
    false
  elsif !item.content.valid_encoding?
    Bridgetown.logger.debug "Skipping:", "#{item.relative_path} is not valid UTF-8"
    false
  else
    publishable?(item)
  end
end
publishable?(item) click to toggle source
# File lib/bridgetown-core/readers/post_reader.rb, line 88
def publishable?(item)
  site.publisher.publish?(item).tap do |will_publish|
    if !will_publish && site.publisher.hidden_in_the_future?(item)
      Bridgetown.logger.warn "Skipping:", "#{item.relative_path} has a future date"
    end
  end
end
read_static_file(full_path, relative_dir) click to toggle source
# File lib/bridgetown-core/readers/post_reader.rb, line 64
def read_static_file(full_path, relative_dir)
  StaticFile.new(
    site,
    site.source,
    relative_dir,
    File.basename(full_path),
    @site.collections.posts
  )
end