lib/middleman-tansu/command.rb in middleman-tansu-0.0.4 vs lib/middleman-tansu/command.rb in middleman-tansu-0.1.0

- old
+ new

@@ -1,11 +1,12 @@ -require "middleman-core/cli" -require "active_support/core_ext/time/zones" +require 'middleman-core/cli' +require 'active_support/core_ext/time/zones' module Middleman module Cli - # This class provides a "tansu" command for the middleman CLI. + # This class provides a "tansu" command for middleman CLI. + # # "tansu" command has some options: # - '-f': set file extension, default "md" # - '-d': set date(yyyy-mm-dd). Default is now. This is used in Frontmatter. # - '-a': set author name. Default "ENV['USER']". # - '-z': set timezone. @@ -17,11 +18,11 @@ namespace :tansu def initialize(*args) super - Time.zone = ENV['TZ'] || "UTC" + Time.zone = ENV['TZ'] || 'UTC' end def self.source_root ENV['MM_ROOT'] end @@ -29,49 +30,47 @@ # Tell Thor to exit with a nonzero exit code on failure def self.exit_on_failure? true end - desc "tansu path/to/TITLE", "Create a new Tansu page" - method_option "file", - aliases: "-f", - desc: "The file extension of file (default: md)", - default: "md" - method_option "timezone", - aliases: "-z", - desc: "The timezone of Frontmatter (default: ENV['TZ'])" - method_option "date", - aliases: "-d", - desc: "The date of Frontmatter (default: Time.zone.now)", + desc 'tansu path/to/TITLE', 'Create a new Tansu page' + method_option 'file', + aliases: '-f', + desc: 'The file extension of file (default: md)', + default: 'md' + method_option 'timezone', + aliases: '-z', + desc: 'The timezone of Frontmatter (default: ENV["TZ"])' + method_option 'date', + aliases: '-d', + desc: 'The date of Frontmatter (default: Time.zone.now)', default: nil - method_option "author", - aliases: "-a", - desc: "The author name of Frontmatter (default: ENV['USER'])" - method_option "frontmatter", - desc: "Additions of Frontmatter. ex: \"category: sample, tags: frontmatter\"", - default: "" + method_option 'author', + aliases: '-a', + desc: 'The author name of Frontmatter (default: ENV["USER"])' + method_option 'frontmatter', + desc: 'Additions of Frontmatter ex:"category:sample,tags:frontmatter"', + default: '' def tansu(path) - paths = path.split("/") + paths = path.split('/') title = paths.pop ext = options[:file] - Time.zone = options[:timezone] || ENV['TZ'] || "UTC" + Time.zone = options[:timezone] || ENV['TZ'] || 'UTC' date = options[:date] ? Time.zone.parse(options[:date]) : Time.zone.now - author = options[:author] || ENV["USER"] + author = options[:author] || ENV['USER'] add_frontmatter = options[:frontmatter] if Regexp.new(".html.#{ext}$") !~ title filename = "#{title}.html.#{ext}" end dir = destination_dir(paths) file = File.join(dir, filename) - if !Dir.exists?(dir) - FileUtils.mkdir_p dir - end + FileUtils.mkdir_p dir unless Dir.exist?(dir) - if File.exists?(file) + if File.exist?(file) puts "#{file} is exist" exit end File.open(file, 'w') do |f| @@ -81,48 +80,47 @@ end no_tasks do def frontmatter(title, author, date, frontmatter) data = { - :title => title, - :author => author, - :date => date + title: title, + author: author, + date: date } - if !frontmatter.empty? + unless frontmatter.empty? data = data.merge(add_frontmatter(frontmatter)) end - rows = ["---"] - data.each do |label, data| - rows << "#{label}: #{data.to_s.strip}" + rows = ['---'] + data.each do |label, val| + rows << "#{label}: #{val.to_s.strip}" end - rows << ["---"] + rows << ['---'] rows << "\n\n" rows.join("\n") end def add_frontmatter(str) {} if str.empty? - frontmatter = {} - str.split(",").each do |row| + str.split(',').each do |row| if /.+:.+/ =~ row - ary, label, data = row.split(/(.+?):(.+)$/) + _, label, data = row.split(/(.+?):(.+)$/) frontmatter[label] = data else - frontmatter[row] = "" + frontmatter[row] = '' end end frontmatter end def destination_dir(dir) app = Middleman::Application source = File.join(app.root, app.config.source) - if dir == nil || dir == '.' + if dir.nil? || dir == '.' source else File.join(source, dir) end end