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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add static_file.name and .basename Liquid attributes #5264

Merged
merged 4 commits into from Sep 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 3 additions & 1 deletion lib/jekyll/static_file.rb
@@ -1,6 +1,6 @@
module Jekyll
class StaticFile
attr_reader :relative_path, :extname
attr_reader :relative_path, :extname, :name
Copy link
Member

Choose a reason for hiding this comment

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

This is a short way of defining:

def name
  @name
end

Therefore, the best thing to do here is to set @name in the initialize method using the code you have on line 100 and then, when you are constructing to_liquid below, you can just call the name method instead of re-calculating its value. Does that make sense?

Copy link
Member Author

@DirtyF DirtyF Aug 22, 2016

Choose a reason for hiding this comment

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

@parkr yes, but then it breaks the tests as name used to be the whole name of the file including the extension. 馃槙


class << self
# The cache of last modification times [path] -> mtime.
Expand Down Expand Up @@ -97,6 +97,8 @@ def write(dest)

def to_liquid
{
"basename" => File.basename(name, extname),
"name" => name,
"extname" => extname,
"modified_time" => modified_time,
"path" => File.join("", relative_path)
Expand Down
20 changes: 18 additions & 2 deletions site/_docs/static_files.md
Expand Up @@ -26,15 +26,31 @@ following metadata:
<td><p><code>file.path</code></p></td>
<td><p>

The relative path to the file.
The relative path to the file, e.g <code>/assets/img/image.jpg</code>

</p></td>
</tr>
<tr>
<td><p><code>file.modified_time</code></p></td>
<td><p>

The `Time` the file was last modified.
The `Time` the file was last modified, e.g <code>2016-04-01 16:35:26 +0200</code>

</p></td>
</tr>
<tr>
<td><p><code>file.name</code></p></td>
<td><p>

The string name of the file e.g. <code>image.jpg</code> for <code>image.jpg</code>

</p></td>
</tr>
<tr>
<td><p><code>file.basename</code></p></td>
<td><p>

The string basename of the file e.g. <code>image</code> for <code>image.jpg</code>

</p></td>
</tr>
Expand Down
2 changes: 2 additions & 0 deletions test/test_static_file.rb
Expand Up @@ -134,6 +134,8 @@ def setup_static_file_with_defaults(base, dir, name, defaults)

should "be able to convert to liquid" do
expected = {
"basename" => "static_file",
"name" => "static_file.txt",
"extname" => ".txt",
"modified_time" => @static_file.modified_time,
"path" => "/static_file.txt"
Expand Down