Class: Middleman::Cli::Publish

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/middleman-blog-drafts/commands/publish.rb

Overview

This class provides a “publish” command for the middleman CLI.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Tell Thor to exit with a nonzero exit code on failure

Returns:

  • (Boolean)


20
21
22
# File 'lib/middleman-blog-drafts/commands/publish.rb', line 20

def self.exit_on_failure?
  true
end

.source_rootString

Template files are relative to this file

Returns:

  • (String)


15
16
17
# File 'lib/middleman-blog-drafts/commands/publish.rb', line 15

def self.source_root
  File.dirname(__FILE__)
end

Instance Method Details

#publish(draft_path) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/middleman-blog-drafts/commands/publish.rb', line 28

def publish(draft_path)
  shared_instance = ::Middleman::Application.server.inst

  # This only exists when the config.rb sets it!
  if shared_instance.blog.respond_to? :drafts
    @slug = draft_path.split('/').last.split('.').first.parameterize
    @date = options[:date] ? DateTime.parse(options[:date]) : DateTime.now

    draft_path    = File.expand_path draft_path
    extension     = File.extname draft_path

    article_path = shared_instance.blog.options.sources.
      sub(':year', @date.year.to_s).
      sub(':month', @date.month.to_s.rjust(2,'0')).
      sub(':day', @date.day.to_s.rjust(2,'0')).
      sub(':title', @slug)
    article_path = File.join(shared_instance.source_dir, article_path + extension)

    data, content = shared_instance.extensions[:frontmatter].data(draft_path)
    data = data.dup.tap { |d| d[:date] = Date.parse @date.strftime('%F') }

    create_file article_path, "#{YAML::dump(data).sub(/^--- !ruby.*$/, '---')}---\n#{content}"
    remove_file draft_path
  else
    raise Thor::Error.new "You need to activate the drafts extension in config.rb before you can publish an article"
  end
end