Class: Middleman::Cli::Tansu

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/middleman-tansu/command.rb

Overview

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Tansu

Returns a new instance of Tansu.



21
22
23
24
# File 'lib/middleman-tansu/command.rb', line 21

def initialize(*args)
  super
  Time.zone = ENV['TZ'] || 'UTC'
end

Class Method Details

.exit_on_failure?Boolean

Tell Thor to exit with a nonzero exit code on failure

Returns:

  • (Boolean)


31
32
33
# File 'lib/middleman-tansu/command.rb', line 31

def self.exit_on_failure?
  true
end

.source_rootObject



26
27
28
# File 'lib/middleman-tansu/command.rb', line 26

def self.source_root
  ENV['MM_ROOT']
end

Instance Method Details

#tansu(path) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/middleman-tansu/command.rb', line 53

def tansu(path)
  paths     = path.split('/')
  title     = paths.pop
  ext       = options[:file]
  Time.zone = options[:timezone] || ENV['TZ'] || 'UTC'
  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