Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Glob scope path only if configured with a pattern #6692

Merged
merged 6 commits into from Jan 19, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 14 additions & 5 deletions lib/jekyll/frontmatter_defaults.rb
Expand Up @@ -101,13 +101,15 @@ def applies_path?(scope, path)
return true if !scope.key?("path") || scope["path"].empty?

sanitized_path = Pathname.new(sanitize_path(path))

site_path = Pathname.new(@site.source)
site_path = Pathname.new(@site.source)
rel_scope_path = Pathname.new(scope["path"])
abs_scope_path = File.join(@site.source, rel_scope_path)
Dir.glob(abs_scope_path).each do |scope_path|
scope_path = Pathname.new(scope_path).relative_path_from site_path
return true if path_is_subpath?(sanitized_path, scope_path)

if glob_pattern?(rel_scope_path)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So instead of this, we do if scope["path"].to_s.include?("*"). Or you can change the implementation of glob_pattern? to just do path.to_s.include?("*"). What do you think?

Dir.glob(abs_scope_path).each do |scope_path|
scope_path = Pathname.new(scope_path).relative_path_from site_path
return true if path_is_subpath?(sanitized_path, scope_path)
end
end

path_is_subpath?(sanitized_path, rel_scope_path)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we make this an else for the previous block (if scope["path"].to_s.include?("*"))?
Multiple exits can get confusing.

Copy link
Member Author

@ashmaroli ashmaroli Jan 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't make this an else branch because if diff-ln#113 evaluates to false even at the end of looping the globbed array, diff-ln#117 acts as a fallback..

That said, it does not make sense to me why that fallback's needed..

Expand All @@ -123,6 +125,13 @@ def path_is_subpath?(path, parent_path)
false
end

def glob_pattern?(path)
path.each_filename do |str|
return true if str =~ %r!\*+|\?|\[.*\]|{.*}!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does path.include?(“*”) not work for this case?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to test the path component string for specific glob patterns. and regex seems better suited for it than multiple String#include? calls

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ashmaroli Why do we need to test each filename separately? AFAIK, the presence of * in scope["path"] is enough to know it's a glob pattern.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@parkr

defaults:
  - scope:
      path: "_puppies/arrears.?"
      type: "puppies"
    values:
      layout: arrears

is also a glob pattern to catch all kinds of files with basename arrears, if you ask me..

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't seen or used ? before. Is that a Windows-ism?

Let's decide which glob patterns we should support and write individual tests using them so we know they work and are supported. The original PR only mentioned * which is why I mention that one only.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, for your example, I would have used "_puppets/arrears.*".

end
false
end

# Determines whether the scope applies to type.
# The scope applies to the type if:
# 1. no 'type' is specified
Expand Down