Class: Middleman::Cli::Article

Inherits:
Thor
  • Object
show all
Includes:
Blog::UriTemplates, Thor::Actions
Defined in:
lib/middleman-blog/commands/article.rb

Overview

This class provides an “article” command for the middleman CLI.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Blog::UriTemplates

apply_uri_template, date_to_params, safe_parameterize, uri_template

Class Method Details

.exit_on_failure?Boolean

Tell Thor to exit with a nonzero exit code on failure

Returns:

  • (Boolean)


21
22
23
# File 'lib/middleman-blog/commands/article.rb', line 21

def self.exit_on_failure?
  true
end

.source_rootObject



16
17
18
# File 'lib/middleman-blog/commands/article.rb', line 16

def self.source_root
  ENV['MM_ROOT']
end

Instance Method Details

#article(title) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/middleman-blog/commands/article.rb', line 35

def article(title)
  shared_instance = ::Middleman::Application.server.inst

  # This only exists when the config.rb sets it!
  if shared_instance.respond_to? :blog
    @title = title
    @slug = safe_parameterize(title)
    @date = options[:date] ? Time.zone.parse(options[:date]) : Time.zone.now
    @lang = options[:lang] || ( I18n.default_locale if defined? I18n )

    blog_inst = shared_instance.blog(options[:blog])

    path_template = blog_inst.source_template
    params = date_to_params(@date).merge(lang: @lang.to_s, title: @slug)
    article_path = apply_uri_template path_template, params

    template blog_inst.options.new_article_template, File.join(shared_instance.source_dir, article_path + blog_inst.options.default_extension)
  else
    raise Thor::Error.new "You need to activate the blog extension in config.rb before you can create an article"
  end
end