class Middleman::Cli::Publish

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

Public Class Methods

exit_on_failure?() click to toggle source

Tell Thor to exit with a nonzero exit code on failure

# File lib/middleman-blog-drafts/commands/publish.rb, line 23
def self.exit_on_failure?
  true
end
source_root() click to toggle source

Template files are relative to this file @return [String]

# File lib/middleman-blog-drafts/commands/publish.rb, line 18
def self.source_root
  File.dirname(__FILE__)
end

Public Instance Methods

publish(draft_path) click to toggle source
# File lib/middleman-blog-drafts/commands/publish.rb, line 31
def publish(draft_path)
  @shared_instance = ::Middleman::Application.server.inst

  # This only exists when the config.rb sets it!
  unless @shared_instance.blog.respond_to? :drafts
    raise Thor::Error.new "You need to activate the drafts extension in config.rb before you can publish an article"
  end

  @slug = safe_parameterize draft_path.split('/').last.split('.').first
  @date = options[:date] ? Time.zone.parse(options[:date]) : Time.zone.now
  @draft_path = File.expand_path draft_path

  create_file article_path, article_content
  remove_file draft_path
end

Private Instance Methods

article_content() click to toggle source
# File lib/middleman-blog-drafts/commands/publish.rb, line 57
def article_content
  data, content = @shared_instance.extensions[:frontmatter].data(@draft_path)
  data = data.dup.tap { |d| d[:date] = Date.parse @date.strftime('%F') }

  "#{YAML::dump(data).sub(/^--- !ruby.*$/, '---')}---\n#{content}"
end
article_path() click to toggle source
# File lib/middleman-blog-drafts/commands/publish.rb, line 49
def article_path
  extension  = File.extname @draft_path
  path_template = @shared_instance.blog.source_template
  params = date_to_params(@date).merge(title: @slug)
  article_path = apply_uri_template path_template, params
  article_path = File.join(@shared_instance.source_dir, article_path + extension)
end