Skip to content

Instantly share code, notes, and snippets.

@eric-cc-su eric-cc-su/modtime.rb

Last active Aug 29, 2015
Embed
What would you like to do?
Script to manage modification time stamps of blog files
=begin
Author: eric-cc-su
This is a Ruby script to add a custom variable for modification time to each post's front matter.
Currently not set up as a plugin, so this will need to be executed manually.
As long as the file modification time of the file matches up with the date of the post, the script
can add or change the mtime variable.
This code is licensed under the MIT License (end of file) and copyrighted by Eric Su
=end
Dir.chdir("../_posts")
Dir.foreach(Dir.pwd){
|file|
if not ['.','..'].include?(file)
filetime = File.ctime(file).to_s
filedate = /(\d+-\d+-\d+)/.match(filetime)[0]
writtendate = /(\d+-\d+-\d+)/.match(file)[0]
if filedate == writtendate
filehours = /\d+:\d+/.match(filetime)[0]
mstring = "mtime: " + filehours
charlength = 0
fopen = false
fmat = ""
text = ""
File.open(File.expand_path(file), 'r+') do |file|
file.each_line do |line|
if line.include?('---') and fopen == false
fopen = true
elsif line.include?('---') and fopen == true
text = IO.read(file,File.size(file),charlength)
break
end
if line.include?("mtime: ")
fmat = IO.read(file, charlength)
end
charlength += line.length
end
if fmat == ""
fmat = IO.read(file, charlength)
end
end
File.open(File.expand_path(file), 'w') do |file|
file.puts(fmat)
file.puts(mstring)
file.puts(text)
end
end
end
}
=begin
The MIT License (MIT)
Copyright (c) 2015 Eric Su
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.