Class: Bridgetown::EntryFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/bridgetown-core/entry_filter.rb

Constant Summary collapse

SPECIAL_LEADING_CHAR_REGEX =
%r!\A#{Regexp.union([".", "_", "#", "~"])}!o
SPECIAL_LEADING_CHAR_NO_UNDERSCORES_REGEX =
%r!\A#{Regexp.union([".", "#", "~"])}!o

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, base_directory: nil, include_underscores: false) ⇒ EntryFilter

Returns a new instance of EntryFilter.



10
11
12
13
14
15
16
# File 'lib/bridgetown-core/entry_filter.rb', line 10

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

Instance Attribute Details

#siteObject (readonly)

Returns the value of attribute site.



5
6
7
# File 'lib/bridgetown-core/entry_filter.rb', line 5

def site
  @site
end

Instance Method Details

#backup?(entry) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/bridgetown-core/entry_filter.rb', line 59

def backup?(entry)
  entry.end_with?("~")
end

#base_directoryObject



18
19
20
# File 'lib/bridgetown-core/entry_filter.rb', line 18

def base_directory
  @base_directory.to_s
end

#derive_base_directory(site, base_dir) ⇒ Object



22
23
24
25
# File 'lib/bridgetown-core/entry_filter.rb', line 22

def derive_base_directory(site, base_dir)
  base_dir[site.source] = "" if base_dir.start_with?(site.source)
  base_dir
end

#excluded?(entry) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/bridgetown-core/entry_filter.rb', line 63

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) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/bridgetown-core/entry_filter.rb', line 33

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) ⇒ Boolean

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

Returns:

  • (Boolean)


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/bridgetown-core/entry_filter.rb', line 78

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) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
# File 'lib/bridgetown-core/entry_filter.rb', line 46

def included?(entry)
  glob_include?(site.config.include, entry) ||
    glob_include?(site.config.include, File.basename(entry))
end

#relative_to_source(entry) ⇒ Object



27
28
29
30
31
# File 'lib/bridgetown-core/entry_filter.rb', line 27

def relative_to_source(entry)
  File.join(
    base_directory, entry
  )
end

#special?(entry) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
57
# File 'lib/bridgetown-core/entry_filter.rb', line 51

def special?(entry)
  use_regex = @include_underscores ?
                SPECIAL_LEADING_CHAR_NO_UNDERSCORES_REGEX :
                SPECIAL_LEADING_CHAR_REGEX

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