class Middleman::Cli::Tansu
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”.
-
‘-z’: set timezone.
-
‘–frontmatter’: add data to Frontmatter
Public Class Methods
exit_on_failure?()
click to toggle source
Tell Thor to exit with a nonzero exit code on failure
# File lib/middleman-tansu/command.rb, line 31 def self.exit_on_failure? true end
new(*args)
click to toggle source
Calls superclass method
# File lib/middleman-tansu/command.rb, line 21 def initialize(*args) super Time.zone = ENV['TZ'] || 'UTC' end
source_root()
click to toggle source
# File lib/middleman-tansu/command.rb, line 26 def self.source_root ENV['MM_ROOT'] end
Public Instance Methods
add_frontmatter(str)
click to toggle source
# File lib/middleman-tansu/command.rb, line 105 def add_frontmatter(str) {} if str.empty? frontmatter = {} str.split(',').each do |row| if /.+:.+/ =~ row _, label, data = row.split(/(.+?):(.+)$/) frontmatter[label] = data else frontmatter[row] = '' end end frontmatter end
destination_dir(dir)
click to toggle source
# File lib/middleman-tansu/command.rb, line 119 def destination_dir(dir) if dir.nil? || dir == '.' source else File.join(source, dir) end end
display_path(path)
click to toggle source
# File lib/middleman-tansu/command.rb, line 132 def display_path(path) path.sub(Regexp.new("^#{source}/"), "") end
frontmatter(title, author, date, frontmatter)
click to toggle source
# File lib/middleman-tansu/command.rb, line 84 def frontmatter(title, author, date, frontmatter) data = { title: title, author: author, date: date } unless frontmatter.empty? data = data.merge(add_frontmatter(frontmatter)) end rows = ['---'] data.each do |label, val| rows << "#{label}: #{val.to_s.strip}" end rows << ['---'] rows << "\n\n" rows.join("\n") end
source()
click to toggle source
# File lib/middleman-tansu/command.rb, line 127 def source app = Middleman::Application File.join(app.root, app.config.source) end
tansu(path)
click to toggle source
# File lib/middleman-tansu/command.rb, line 53 def tansu(path) paths = path.split('/') title = paths.pop ext = options[:file] zone = options[:timezone] || ENV['TZ'] || 'UTC' Time.zone = (zone != 'UTC') ? zone.downcase.capitalize : zone date = options[:date] ? Time.zone.parse(options[:date]) : Time.zone.now 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) FileUtils.mkdir_p dir unless Dir.exist?(dir) if File.exist?(file) say "#{display_path(file)} is exist" exit end File.open(file, 'w') do |f| f.puts frontmatter(title, author, date, add_frontmatter) end say "create tansu page: #{display_path(file)}" end