1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
# frozen_string_literal: true
module Jekyll
module LastModifiedAt
class Determinator
attr_reader :site_source, :page_path
attr_accessor :format
def initialize(site_source, page_path, format = nil)
@site_source = site_source
@page_path = page_path
@format = format || '%d-%b-%y'
end
def git
return REPO_CACHE[site_source] unless REPO_CACHE[site_source].nil?
REPO_CACHE[site_source] = Git.new(site_source)
REPO_CACHE[site_source]
end
def formatted_last_modified_date
return PATH_CACHE[page_path] unless PATH_CACHE[page_path].nil?
last_modified = last_modified_at_time.strftime(@format)
PATH_CACHE[page_path] = last_modified
last_modified
end
def last_modified_at_time
raise Errno::ENOENT, "#{absolute_path_to_article} does not exist!" unless File.exist? absolute_path_to_article
Time.at(last_modified_at_unix.to_i)
end
def last_modified_at_unix
if git.git_repo?
last_commit_date = Executor.sh(
'git',
'--git-dir',
git.top_level_directory,
'log',
'-n',
'1',
'--format="%ct"',
'--',
relative_path_from_git_dir
)[/\d+/]
# last_commit_date can be nil iff the file was not committed.
last_commit_date.nil? || last_commit_date.empty? ? mtime(absolute_path_to_article) : last_commit_date
else
mtime(absolute_path_to_article)
end
end
def to_s
@to_s ||= formatted_last_modified_date
end
def to_liquid
@to_liquid ||= last_modified_at_time
end
private
def absolute_path_to_article
@absolute_path_to_article ||= Jekyll.sanitized_path(site_source, @page_path)
end
def relative_path_from_git_dir
return nil unless git.git_repo?
@relative_path_from_git_dir ||= Pathname.new(absolute_path_to_article)
.relative_path_from(
Pathname.new(File.dirname(git.top_level_directory))
).to_s
end
def mtime(file)
File.mtime(file).to_i.to_s
end
end
end
end
|