Class: Middleman::Cli::Article
- Inherits:
-
Thor::Group
- Object
- Thor::Group
- Middleman::Cli::Article
- 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
-
.source_root ⇒ String
Template files are relative to this file.
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_root ⇒ String
Template files are relative to this file
28 29 30 |
# File 'lib/middleman-blog/commands/article.rb', line 28 def self.source_root File.dirname(__FILE__) end |
Instance Method Details
#article ⇒ Object
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 = [:content] || '' @date = [:date] ? ::Time.zone.parse([:date]) : Time.zone.now @locale = [:locale] || (::I18n.default_locale if defined? ::I18n) @slug = safe_parameterize(title) @tags = [: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 [:blog] app.extensions[:blog].find { |_key, instance| instance.[:name] == [:blog] }[ 1 ] else app.extensions[:blog].values.first end unless blog_inst msg = 'Could not find an active blog instance' msg = "#{msg} named #{[:blog]}" if [: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..default_extension) template blog_inst..new_article_template, absolute_article_path # Edit option process if [: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 [:subdirectory] end |