jekyll-last-modified-at 0.3.0 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8d65391571949b52e7eec6fb8bf72c0d9521a3e3
4
- data.tar.gz: 3ddc241f21d396aef61b0aba58f4645ecea5a724
3
+ metadata.gz: 65e24940002184996cba7d2ac55bcd9f23615164
4
+ data.tar.gz: 0754e42cf000393d9e76b1e02b8da51256e7453e
5
5
  SHA512:
6
- metadata.gz: 5a0f5c1c6cf795d22316bb3feaedff95669cbc8cb29159384c135d9f1dbf59df0c8504ff8723747a6032e0fad2cb5b8e603372c619a48f24a401d26400650192
7
- data.tar.gz: c71f91292fe2d2ea8050506c85ebbcae03aa9efbfe9148ff2591dc9c29e4e82837ecf87be68c7b69bf9d16c46b448f9f83640b3c47f5adb1bab365d900d8a6c8
6
+ metadata.gz: de86cb53ccc5e6d1aa9dd8442108c99c3a8f30aeacea3b80b0283dedf8c30cd83da5ce865d8592537accc2e57f0fc2f475395a07018a79c3ec3608be0e8129ee
7
+ data.tar.gz: e1f29afcc63909e78f701a9c349a76c58268083ceea1adcf601c9ec72ec6c611cc857f7c5dc35e2948ab815309027d922c3fe53badfd27ebd3eb7128689f0281
@@ -0,0 +1,89 @@
1
+ module Jekyll
2
+ module LastModifiedAt
3
+ class Determinator
4
+ attr_reader :site_source, :opts
5
+
6
+ def initialize(site_source, page_path, opts = {})
7
+ @site_source = site_source
8
+ @page_path = page_path
9
+ @opts = opts
10
+ end
11
+
12
+ def last_modified_at_date
13
+ unless File.exists? absolute_path_to_article
14
+ raise Errno::ENOENT, "#{absolute_path_to_article} does not exist!"
15
+ end
16
+
17
+ Time.at(last_modified_time.to_i).strftime(format)
18
+ end
19
+
20
+ def last_modified_time
21
+ if is_git_repo?(site_source)
22
+ last_commit_date = Executor.sh(
23
+ 'git',
24
+ '--git-dir',
25
+ top_level_git_directory,
26
+ 'log',
27
+ '--format="%ct"',
28
+ '--',
29
+ relative_path_from_git_dir
30
+ )[/\d+/]
31
+ # last_commit_date can be nil iff the file was not committed.
32
+ (last_commit_date.nil? || last_commit_date.empty?) ? mtime(absolute_path_to_article) : last_commit_date
33
+ else
34
+ mtime(absolute_path_to_article)
35
+ end
36
+ end
37
+
38
+ def to_liquid
39
+ @to_liquid ||= last_modified_at_date
40
+ end
41
+
42
+ def format
43
+ opts['format'] ||= "%d-%b-%y"
44
+ end
45
+
46
+ def format=(new_format)
47
+ opts['format'] = new_format
48
+ end
49
+
50
+ private
51
+
52
+ def absolute_path_to_article
53
+ @article_file_path ||= Jekyll.sanitized_path(site_source, @page_path)
54
+ end
55
+
56
+ def relative_path_from_git_dir
57
+ return nil unless is_git_repo?(site_source)
58
+ @relative_path_from_git_dir ||= Pathname.new(absolute_path_to_article)
59
+ .relative_path_from(
60
+ Pathname.new(File.dirname(top_level_git_directory))
61
+ ).to_s
62
+ end
63
+
64
+ def top_level_git_directory
65
+ @top_level_git_directory ||= begin
66
+ Dir.chdir(site_source) do
67
+ top_level_git_directory = File.join(Executor.sh("git", "rev-parse", "--show-toplevel"), ".git")
68
+ end
69
+ rescue
70
+ ""
71
+ end
72
+ end
73
+
74
+ def is_git_repo?(site_source)
75
+ @is_git_repo ||= begin
76
+ Dir.chdir(site_source) do
77
+ Executor.sh("git", "rev-parse", "--is-inside-work-tree").eql? "true"
78
+ end
79
+ rescue
80
+ false
81
+ end
82
+ end
83
+
84
+ def mtime(file)
85
+ File.mtime(file)
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,15 @@
1
+ require 'open3'
2
+
3
+ module Jekyll
4
+ module LastModifiedAt
5
+ module Executor
6
+ def self.sh(*args)
7
+ Open3.popen2e(*args) do |stdin, stdout_stderr, wait_thr|
8
+ exit_status = wait_thr.value # wait for it...
9
+ output = stdout_stderr.read
10
+ output ? output.strip : nil
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module Jekyll
2
+ module LastModifiedAt
3
+ class Generator < Jekyll::Generator
4
+
5
+ def generate(site)
6
+ %w(posts pages docs_to_write).each do |type|
7
+ site.send(type).each do |item|
8
+ item.data['last_modified_at'] = Determinator.new(site.source, item.path)
9
+ end
10
+ end
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,21 @@
1
+ module Jekyll
2
+ module LastModifiedAt
3
+ class Tag < Liquid::Tag
4
+ def initialize(tag_name, format, tokens)
5
+ super
6
+ @format = format.empty? ? nil : format.strip
7
+ end
8
+
9
+ def render(context)
10
+ site_source = context.registers[:site].source
11
+ article_file = context.environments.first["page"]["path"]
12
+
13
+ Determinator.new(site_source, article_file, {
14
+ "format" => @format
15
+ }).last_modified_at_date
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ Liquid::Template.register_tag('last_modified_at', Jekyll::LastModifiedAt::Tag)
@@ -0,0 +1,5 @@
1
+ module Jekyll
2
+ module LastModifiedAt
3
+ VERSION = "0.3.2"
4
+ end
5
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-last-modified-at
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Garen J. Torikian
@@ -73,6 +73,11 @@ extensions: []
73
73
  extra_rdoc_files: []
74
74
  files:
75
75
  - lib/jekyll-last-modified-at.rb
76
+ - lib/jekyll-last-modified-at/determinator.rb
77
+ - lib/jekyll-last-modified-at/executor.rb
78
+ - lib/jekyll-last-modified-at/generator.rb
79
+ - lib/jekyll-last-modified-at/tag.rb
80
+ - lib/jekyll-last-modified-at/version.rb
76
81
  homepage: https://github.com/gjtorikian/jekyll-last-modified-at
77
82
  licenses:
78
83
  - MIT