class Bridgetown::EntryFilter

Constants

SPECIAL_LEADING_CHAR_NO_UNDERSCORES_REGEX
SPECIAL_LEADING_CHAR_REGEX

Attributes

site[R]

Public Class Methods

new(site, base_directory: nil, include_underscores: false) click to toggle source
# File lib/bridgetown-core/entry_filter.rb, line 9
def initialize(site, base_directory: nil, include_underscores: false)
  @site = site
  @base_directory = derive_base_directory(
    @site, base_directory.to_s.dup
  )
  @include_underscores = include_underscores
end

Public Instance Methods

backup?(entry) click to toggle source
# File lib/bridgetown-core/entry_filter.rb, line 60
def backup?(entry)
  entry.end_with?("~")
end
base_directory() click to toggle source
# File lib/bridgetown-core/entry_filter.rb, line 17
def base_directory
  @base_directory.to_s
end
derive_base_directory(site, base_dir) click to toggle source
# File lib/bridgetown-core/entry_filter.rb, line 21
def derive_base_directory(site, base_dir)
  base_dir[site.source] = "" if base_dir.start_with?(site.source)
  base_dir
end
excluded?(entry) click to toggle source
# File lib/bridgetown-core/entry_filter.rb, line 64
def excluded?(entry)
  glob_include?(
    site.config.exclude - site.config.include, relative_to_source(entry)
  ).tap do |excluded|
    if excluded
      Bridgetown.logger.debug(
        "EntryFilter:",
        "excluded #{relative_to_source(entry)}"
      )
    end
  end
end
filter(entries) click to toggle source
# File lib/bridgetown-core/entry_filter.rb, line 32
def filter(entries)
  entries.reject do |e|
    # Reject this entry if it is just a "dot" representation.
    #   e.g.: '.', '..', '_movies/.', 'music/..', etc
    next true if e.end_with?(".")
    # Do not reject this entry if it is included.
    next false if included?(e)

    # Reject this entry if it is special, a backup file, or excluded.
    special?(e) || backup?(e) || excluded?(e)
  end
end
glob_include?(enumerator, entry) click to toggle source

Check if an entry matches a specific pattern. Returns true if path matches against any glob pattern, else false.

# File lib/bridgetown-core/entry_filter.rb, line 79
def glob_include?(enumerator, entry)
  entry_with_source = File.join(site.source, entry)

  enumerator.any? do |pattern|
    case pattern
    when String
      pattern_with_source = File.join(site.source, pattern)

      File.fnmatch?(pattern_with_source, entry_with_source) ||
        entry_with_source.start_with?(pattern_with_source)
    when Regexp
      pattern.match?(entry_with_source)
    else
      false
    end
  end
end
included?(entry) click to toggle source
# File lib/bridgetown-core/entry_filter.rb, line 45
def included?(entry)
  glob_include?(site.config.include, entry) ||
    glob_include?(site.config.include, File.basename(entry))
end
relative_to_source(entry) click to toggle source
# File lib/bridgetown-core/entry_filter.rb, line 26
def relative_to_source(entry)
  File.join(
    base_directory, entry
  )
end
special?(entry) click to toggle source
# File lib/bridgetown-core/entry_filter.rb, line 50
def special?(entry)
  use_regex = if @include_underscores
                SPECIAL_LEADING_CHAR_NO_UNDERSCORES_REGEX
              else
                SPECIAL_LEADING_CHAR_REGEX
              end

  use_regex.match?(entry) || use_regex.match?(File.basename(entry))
end