Last active
March 6, 2018 07:01
-
-
Save lolgear/1582506878dbe9ff776163abcdeaaab1 to your computer and use it in GitHub Desktop.
Scrivener post-processing script which rename generated file into Jekyll post naming convention.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
# This script rename blog-post to jekyll conventions naming. | |
# Suppose, that you have a file with metadata content. | |
# File.txt: | |
# --- | |
# title:'Wow, title. Rename blog-post.' | |
# date:26.01.2018 12:31:55 GMT+3 | |
# --- | |
# ruby script.rb -i File.txt | |
# And result will be: | |
# 2018-01-26-Wow-title-rename-blog-post.md | |
# which contents metadata: | |
# --- | |
# title:'Wow, title. Rename blog-post.' | |
# date:2018-01-26 12:31:55 +0300 | |
# --- | |
module Solution | |
class Work | |
class Date | |
require 'date' | |
attr_accessor :date | |
def from_scrivener_date(date) | |
DateTime.parse(date) | |
end | |
def initialize(date) | |
# date is a string | |
# formatted as | |
# DD.MM.YYYY HH:MM:SS +/-TGMT | |
@date = from_scrivener_date(date) | |
puts "to_scrivener: #{to_scrivener}" | |
end | |
def to_scrivener | |
# DD.MM.YYYY HH:MM:SS +/-TGMT | |
# do not know how to get +3 GMT. | |
# self.date.strftime('%d.%m.%Y %T %Z') | |
# self.date.to_time.zone | |
# do not know how? Oo | |
# shit. | |
"" | |
end | |
def to_jekyll | |
# YYYY-MM-DD HH:MM:SS +/-TTTT | |
self.date.strftime('%Y-%m-%d %T %z') | |
end | |
def to_jekyll_title | |
# YYYY-MM-DD | |
self.date.strftime('%Y-%m-%d') | |
end | |
end | |
require 'optparse' | |
require 'fileutils' | |
# array come! | |
def find_options_in_file(file) | |
options = { | |
date: nil, | |
title: nil | |
} | |
until file.eof | |
line = file.gets | |
case line | |
when /^title:['"](?<found>.+?)["']/ | |
options[:title] = $1 | |
when /^date:(?<found>.+?$)/ | |
options[:date] = Date.new($1) | |
end | |
end | |
options | |
end | |
def convert_date(date) | |
# jekyll format | |
# YYYY-MM-DD HH:MM:SS +/-TTTT | |
# DD.MM.YYYY HH:MM:SS +/-TGMT | |
end | |
def title_part_of_date(date) | |
# YYYY-MM-DD | |
end | |
def simplify_title(file_options) | |
title = file_options[:title] | |
simple_title = title | |
.gsub(/[\W]/, "-").gsub(/--/, "-").gsub(/-$/, "") | |
.downcase.capitalize | |
# next, append date correctly. | |
# first, parse it and put in correct format. | |
if file_options[:date] | |
date = file_options[:date] | |
result = date.to_jekyll_title # maybe find and replace date? | |
simple_title = [result, simple_title].join('-') if result | |
end | |
puts "simplify_title: #{title} -> #{simple_title}" | |
simple_title | |
end | |
def copy_file(from, to) | |
puts "Copy: #{from} -> #{to}" | |
FileUtils.copy(from, to) | |
end | |
def replace_date_in_file_for_jekyll(file, date) | |
contents = File.read(file) | |
jekyll_contents = contents.gsub(/^date:(?<found>.+?$)/, "date:#{date.to_jekyll}") | |
File.open(file, 'w') {|f| f.puts jekyll_contents} | |
end | |
def work(array = []) | |
options = parse array | |
puts "options: #{options}" | |
# retrieve name. | |
from_file = options[:input] | |
file_options = find_options_in_file(File.new(options[:input], "r")) | |
puts "file options: #{file_options}" | |
if file_options[:title] | |
simple_title = simplify_title file_options | |
unless simple_title | |
puts "Title malformed. Check file_options: #{file_options} and file: #{from_file}" | |
exit | |
end | |
to_file = [simple_title.sub(/\.+$/, ''), options[:extname]].join | |
# process. | |
copy_file(from_file, to_file) | |
# now, find in file and replace items. | |
replace_date_in_file_for_jekyll to_file, file_options[:date] | |
else | |
puts "No title found in #{from_file}" | |
end | |
end | |
def default_options | |
{ | |
extname: '.md' | |
} | |
end | |
def parse(array = []) | |
options = default_options | |
puts "array is: #{array}" | |
OptionParser.new do |opts| | |
opts.banner = "Usage: rename_to_group.rb [options]" | |
opts.on('-h', '--help', 'Show help') {puts opts.help; exit} | |
opts.on('-i', '--input_file file', String, 'Input file name') {|arg| options[:input] = arg} | |
opts.on('-e', '--extension_of_file ext', String, 'Extension of file') {|arg| options[:extname] = arg} | |
end.parse(ARGV) | |
options | |
end | |
end | |
end | |
Solution::Work.new.work(ARGV) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment