Class: Middleman::Cli::Article

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

Overview

TODO:

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

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, extract_directory_path, extract_params, safe_parameterize, uri_template

Class Method Details

.source_rootString

Template files are relative to this file

Returns:

  • (String)


28
29
30
# File 'lib/middleman-blog/commands/article.rb', line 28

def self.source_root
  File.dirname(__FILE__)
end

Instance Method Details

#articleObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/middleman-blog/commands/article.rb', line 66

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 = "#{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