class Middleman::Cli::Article

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

@usage bundle exec middleman article –help @usage bundle exec middleman article “Article Title” @usage bundle exec middleman article “Article Title” -t “test,test1” -c “String” -d 2017-03-25 -l fr-be -b blog_fr-be -s -e

@see middlemanapp.com/basics/blogging/#generating-articles

@todo Tags should be removed from the template if they are not required

Public Class Methods

source_root() click to toggle source

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

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

Public Instance Methods

article() click to toggle source
# File lib/middleman-blog/commands/article.rb, line 64
def article
  @content = options[:content] || ''
  @date    = options[:date] ? ::Time.zone.parse(options[:date]) : Time.zone.now
  @locale  = options[:locale] || (::I18n.default_locale if defined? ::I18n)
  @slug    = safe_parameterize(title)
  @tags    = options[:tags]&.split(/\s*,\s*/) || []
  @title   = title

  app = ::Middleman::Application.new do
    config[:mode]              = :config
    config[:disable_sitemap]   = true
    config[:watcher_disable]   = true
    config[:exit_before_ready] = true
  end

  blog_inst = if options[:blog]
                app.extensions[:blog].find { |_key, instance| instance.options[:name] == options[:blog] }[ 1 ]
              else
                app.extensions[:blog].values.first
              end

  unless blog_inst
    msg = 'Could not find an active blog instance'
    msg << " named #{options[:blog]}" if options[:blog]
    throw msg
  end

  path_template         = blog_inst.data.source_template
  params                = date_to_params(@date).merge(locale: @locale.to_s, title: @slug)
  article_path          = apply_uri_template path_template, params
  absolute_article_path = File.join(app.source_dir, article_path + blog_inst.options.default_extension)

  template blog_inst.options.new_article_template, absolute_article_path

  # Edit option process
  if options[:edit]

    editor = ENV.fetch('MM_EDITOR', ENV.fetch('EDITOR', nil))

    if editor
      system("#{editor} #{absolute_article_path}")
    else
      throw 'Could not find a suitable editor. Try setting the environment variable MM_EDITOR.'
    end

  end

  # Subdirectory option process
  empty_directory extract_directory_path(File.join(app.source_dir, article_path)) if options[:subdirectory]
end

Protected Instance Methods

blog_instance(key) click to toggle source
# File lib/middleman-blog/commands/article.rb, line 117
def blog_instance(key)
  return nil unless app.extensions[:blog]

  app.extensions[:blog][key]
end