jekyll-last-modified 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f47e0601cefb896369bac29cb594fadd2647f9c953bffd3c4b3b3bbc64e9b770
4
+ data.tar.gz: 7d97a4177607ff4360d21559ace39bfc59e0205895e6ad6570e21aca0143aa9d
5
+ SHA512:
6
+ metadata.gz: cc8b09475d9272ee8336429619fcc811e443a40273ef8c3ce2f7dbfb0d304d703f9780ebc72e373fe6cf383b1f43b5bce7246ca315344790c43b544c303d8455
7
+ data.tar.gz: 17e32abb23844638dc404325ee64834a61dcc71f1343dbe1ced216c83d3605f47fff0059f54e6ad10ed9dfce681ad3dfbd07d14eb90b1fdbf1cb860f0fe9a3e9
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ sudo: required
3
+ dist: trusty
4
+ script: bundle exec rake test
5
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in jekyll-last-modified.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Ivan Tse
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,3 @@
1
+ # Jekyll::LastModified
2
+
3
+ This is a fork of [ivantsepp/jekyll-git_metadata](https://github.com/ivantsepp/jekyll-git_metadata), with the addition that it populates `page.date` (if unset) and `page.last_modified_at` from retrieved Git metadata.
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "jekyll-last-modified"
7
+ spec.version = '1.0.0'
8
+ spec.authors = ["iBug"]
9
+ spec.email = ["i@ibugone.com"]
10
+ spec.summary = %q{Set page date from Git database}
11
+ spec.description = %q{Populate page date and last modified date from Git database}
12
+ spec.homepage = "https://github.com/iBug/jekyll-last-modified"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split "\0"
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "jekyll", '>= 3.0'
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.6"
23
+ spec.add_development_dependency "shoulda"
24
+ spec.add_development_dependency "mocha"
25
+ end
@@ -0,0 +1,6 @@
1
+ require 'jekyll/last_modified'
2
+
3
+ module Jekyll
4
+ module LastModified
5
+ end
6
+ end
@@ -0,0 +1,149 @@
1
+ require 'rbconfig'
2
+
3
+ module Jekyll
4
+ module LastModified
5
+ class Generator < Jekyll::Generator
6
+
7
+ safe true
8
+
9
+ def generate(site)
10
+ raise "Git is not installed" unless git_installed?
11
+
12
+ Dir.chdir(site.source) do
13
+ data = load_git_metadata(site)
14
+ site.config['git'] = data['site_data']
15
+ jekyll_items(site).each do |page|
16
+ if page.is_a?(Jekyll::Page)
17
+ path = page.path
18
+ else
19
+ path = page.relative_path
20
+ end
21
+ page.data['git'] = data['pages_data'][path]
22
+ unless page.data['git'].nil?
23
+ page.data['last_modified_at'] = page.data['git']['last_commit']['commit_date']
24
+ if page.data['date'] == site.time
25
+ page.data['date'] = page.data['git']['first_commit']['commit_date']
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ def load_git_metadata(site)
33
+ current_sha = %x{ git rev-parse HEAD }.strip
34
+
35
+ cache_dir = site.source + '/.git-metadata'
36
+ FileUtils.mkdir_p(cache_dir) unless File.directory?(cache_dir)
37
+ cache_file = cache_dir + "/#{current_sha}.json"
38
+
39
+ if File.exist?(cache_file)
40
+ return JSON.parse(IO.read(cache_file))
41
+ end
42
+
43
+ pages_data = {}
44
+ jekyll_items(site).each do |page|
45
+ if page.is_a?(Jekyll::Page)
46
+ path = page.path
47
+ else
48
+ path = page.relative_path
49
+ end
50
+ pages_data[path] = page_data(path)
51
+ end
52
+ data = { 'site_data' => site_data, 'pages_data' => pages_data }
53
+
54
+ File.open(cache_file, 'w') { |f| f.write(data.to_json) }
55
+
56
+ data
57
+ end
58
+
59
+ def site_data
60
+ {
61
+ 'project_name' => project_name,
62
+ 'files_count' => files_count,
63
+ }.merge!(page_data)
64
+ end
65
+
66
+ def page_data(relative_path = nil)
67
+ return if relative_path && !tracked_files.include?(relative_path)
68
+
69
+ authors = self.authors(relative_path)
70
+ lines = self.lines(relative_path)
71
+ {
72
+ 'authors' => authors,
73
+ 'total_commits' => authors.inject(0) { |sum, h| sum += h['commits'] },
74
+ 'total_additions' => lines.inject(0) { |sum, h| sum += h['additions'] },
75
+ 'total_subtractions' => lines.inject(0) { |sum, h| sum += h['subtractions'] },
76
+ 'first_commit' => commit(lines.last['sha']),
77
+ 'last_commit' => commit(lines.first['sha'])
78
+ }
79
+ end
80
+
81
+ def authors(file = nil)
82
+ results = []
83
+ cmd = 'git shortlog -se HEAD'
84
+ cmd << " -- #{file}" if file
85
+ result = %x{ #{cmd} }
86
+ result.lines.each do |line|
87
+ commits, name, email = line.scan(/(.*)\t(.*)<(.*)>/).first.map(&:strip)
88
+ results << { 'commits' => commits.to_i, 'name' => name, 'email' => email }
89
+ end
90
+ results
91
+ end
92
+
93
+ def lines(file = nil)
94
+ cmd = "git log --numstat --format=%h"
95
+ cmd << " -- #{file}" if file
96
+ result = %x{ #{cmd} }
97
+ results = result.scan(/(.*)\n\n((?:.*\t.*\t.*\n)*)/)
98
+ results.map do |line|
99
+ files = line[1].scan(/(.*)\t(.*)\t(.*)\n/)
100
+ line[1] = files.inject(0){|s,a| s+=a[0].to_i}
101
+ line[2] = files.inject(0){|s,a| s+=a[1].to_i}
102
+ end
103
+ results.map do |line|
104
+ { 'sha' => line[0], 'additions' => line[1], 'subtractions' => line[2] }
105
+ end
106
+ end
107
+
108
+ def commit(sha)
109
+ result = %x{ git show --format=fuller --name-only #{sha} }
110
+ long_sha, author_name, author_email, author_date, commit_name, commit_email, commit_date, message, changed_files = result.scan(/commit (.*)\nAuthor:(.*)<(.*)>\nAuthorDate:(.*)\nCommit:(.*)<(.*)>\nCommitDate:(.*)\n\n((?:\s\s\s\s[^\r\n]*\n)*)\n(.*)/m).first.map(&:strip)
111
+ {
112
+ 'short_sha' => sha,
113
+ 'long_sha' => long_sha,
114
+ 'author_name' => author_name,
115
+ 'author_email' => author_email,
116
+ 'author_date' => author_date,
117
+ 'commit_name' => commit_name,
118
+ 'commit_email' => commit_email,
119
+ 'commit_date' => commit_date,
120
+ 'message' => message.gsub(/ /, ''),
121
+ 'changed_files' => changed_files.split("\n")
122
+ }
123
+ end
124
+
125
+ def tracked_files
126
+ @tracked_files ||= %x{ git ls-tree --full-tree -r --name-only HEAD }.split("\n")
127
+ end
128
+
129
+ def project_name
130
+ File.basename(%x{ git rev-parse --show-toplevel }.strip)
131
+ end
132
+
133
+ def files_count
134
+ %x{ git ls-tree -r HEAD }.lines.count
135
+ end
136
+
137
+ def git_installed?
138
+ null = RbConfig::CONFIG['host_os'] =~ /msdos|mswin|djgpp|mingw/ ? 'NUL' : '/dev/null'
139
+ system "git --version >>#{null} 2>&1"
140
+ end
141
+
142
+ private
143
+
144
+ def jekyll_items(site)
145
+ site.pages + site.collections.values.map(&:docs).flatten
146
+ end
147
+ end
148
+ end
149
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-last-modified
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - iBug
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-10-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jekyll
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: shoulda
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mocha
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Populate page date and last modified date from Git database
70
+ email:
71
+ - i@ibugone.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - jekyll-last-modified.gemspec
82
+ - lib/jekyll-last-modified.rb
83
+ - lib/jekyll/last_modified.rb
84
+ homepage: https://github.com/iBug/jekyll-last-modified
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubygems_version: 3.0.4
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: Set page date from Git database
107
+ test_files: []