Class: Jekyll::EntryFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll/entry_filter.rb

Constant Summary collapse

SPECIAL_LEADING_CHARACTERS =
[
  ".", "_", "#", "~",
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, base_directory = nil) ⇒ EntryFilter

Returns a new instance of EntryFilter.



10
11
12
13
14
15
# File 'lib/jekyll/entry_filter.rb', line 10

def initialize(site, base_directory = nil)
  @site = site
  @base_directory = derive_base_directory(
    @site, base_directory.to_s.dup
  )
end

Instance Attribute Details

#siteObject (readonly)

Returns the value of attribute site.



5
6
7
# File 'lib/jekyll/entry_filter.rb', line 5

def site
  @site
end

Instance Method Details

#backup?(entry) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/jekyll/entry_filter.rb', line 53

def backup?(entry)
  entry[-1..-1] == "~"
end

#base_directoryObject



17
18
19
# File 'lib/jekyll/entry_filter.rb', line 17

def base_directory
  @base_directory.to_s
end

#derive_base_directory(site, base_dir) ⇒ Object



21
22
23
24
# File 'lib/jekyll/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) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
65
66
# File 'lib/jekyll/entry_filter.rb', line 57

def excluded?(entry)
  glob_include?(site.exclude, relative_to_source(entry)).tap do |excluded|
    if excluded
      Jekyll.logger.debug(
        "EntryFilter:",
        "excluded #{relative_to_source(entry)}"
      )
    end
  end
end

#filter(entries) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/jekyll/entry_filter.rb', line 32

def filter(entries)
  entries.reject do |e|
    # Reject this entry if it is a symlink.
    next true if symlink?(e)
    # 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?(enum, entry) ⇒ Boolean

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

Returns:

  • (Boolean)


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/jekyll/entry_filter.rb', line 92

def glob_include?(enum, entry)
  entry_path = Pathutil.new(site.in_source_dir).join(entry)
  enum.any? do |exp|
    # Users who send a Regexp knows what they want to
    # exclude, so let them send a Regexp to exclude files,
    # we will not bother caring if it works or not, it's
    # on them at this point.

    if exp.is_a?(Regexp)
      entry_path =~ exp

    else
      item = Pathutil.new(site.in_source_dir).join(exp)

      # If it's a directory they want to exclude, AKA
      # ends with a "/" then we will go on to check and
      # see if the entry falls within that path and
      # exclude it if that's the case.

      if entry.end_with?("/")
        entry_path.in_path?(
          item
        )

      else
        File.fnmatch?(item, entry_path) ||
          entry_path.to_path.start_with?(
            item
          )
      end
    end
  end
end

#included?(entry) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
# File 'lib/jekyll/entry_filter.rb', line 43

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

#relative_to_source(entry) ⇒ Object



26
27
28
29
30
# File 'lib/jekyll/entry_filter.rb', line 26

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

#special?(entry) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
# File 'lib/jekyll/entry_filter.rb', line 48

def special?(entry)
  SPECIAL_LEADING_CHARACTERS.include?(entry[0..0]) ||
    SPECIAL_LEADING_CHARACTERS.include?(File.basename(entry)[0..0])
end

#symlink?(entry) ⇒ Boolean

– Check if a file is a symlink. NOTE: This can be converted to allowing even in safe,

since we use Pathutil#in_path? now.

Returns:

  • (Boolean)


73
74
75
# File 'lib/jekyll/entry_filter.rb', line 73

def symlink?(entry)
  site.safe && File.symlink?(entry) && symlink_outside_site_source?(entry)
end

– NOTE: Pathutil#in_path? gets the realpath. Check if a path is outside of our given root. –

Parameters:

  • entry (<Anything>)

    the entry you want to validate.

Returns:

  • (Boolean)


82
83
84
85
86
# File 'lib/jekyll/entry_filter.rb', line 82

def symlink_outside_site_source?(entry)
  !Pathutil.new(entry).in_path?(
    site.in_source_dir
  )
end