lib/middleman-blog-drafts/commands/publish.rb in middleman-blog-drafts-0.1.0 vs lib/middleman-blog-drafts/commands/publish.rb in middleman-blog-drafts-0.2.0
- old
+ new
@@ -1,12 +1,15 @@
require 'middleman-core/cli'
+require 'date'
+require 'middleman-blog/uri_templates'
module Middleman
module Cli
# This class provides a "publish" command for the middleman CLI.
class Publish < Thor
include Thor::Actions
+ include Blog::UriTemplates
check_unknown_options!
namespace :publish
@@ -24,34 +27,39 @@
desc "publish DRAFT_PATH", "Publish a draft"
method_option "date",
:aliases => "-d",
:desc => "The date to create the post with (defaults to now)"
def publish(draft_path)
- shared_instance = ::Middleman::Application.server.inst
+ @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
+ 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
- draft_path = File.expand_path draft_path
- extension = File.extname draft_path
+ @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
- 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)
+ create_file article_path, article_content
+ remove_file draft_path
+ end
- data, content = shared_instance.extensions[:frontmatter].data(draft_path)
- data = data.dup.tap { |d| d[:date] = Date.parse @date.strftime('%F') }
+ private
- 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
+ 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
+
+ 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
end
end
end