From d242228daa4f8e6f02c66f8205ed2f92e2a2313c Mon Sep 17 00:00:00 2001 From: Ashwin Maroli Date: Mon, 8 Oct 2018 12:32:15 +0530 Subject: [PATCH 1/2] Generate configuration from CLI options --- lib/jekyll-compose/arg_parser.rb | 6 ++++-- lib/jekyll-compose/file_editor.rb | 13 ++++++++----- lib/jekyll/commands/draft.rb | 3 ++- lib/jekyll/commands/page.rb | 3 ++- lib/jekyll/commands/post.rb | 5 ++++- lib/jekyll/commands/publish.rb | 3 ++- lib/jekyll/commands/unpublish.rb | 3 ++- 7 files changed, 24 insertions(+), 12 deletions(-) diff --git a/lib/jekyll-compose/arg_parser.rb b/lib/jekyll-compose/arg_parser.rb index 6e0b3e2..1aa08e6 100644 --- a/lib/jekyll-compose/arg_parser.rb +++ b/lib/jekyll-compose/arg_parser.rb @@ -4,10 +4,12 @@ module Jekyll module Compose class ArgParser attr_reader :args, :options, :config - def initialize(args, options) + + # TODO: Remove `nil` parameter in v1.0 + def initialize(args, options, config = nil) @args = args @options = options - @config = Jekyll.configuration(options) + @config = config || Jekyll.configuration(options) end def validate! diff --git a/lib/jekyll-compose/file_editor.rb b/lib/jekyll-compose/file_editor.rb index 5bd5c3b..2593e55 100644 --- a/lib/jekyll-compose/file_editor.rb +++ b/lib/jekyll-compose/file_editor.rb @@ -16,6 +16,13 @@ module Jekyll module Compose class FileEditor class << self + attr_reader :compose_config + alias_method :jekyll_compose_config, :compose_config + + def bootstrap(config) + @compose_config = config["jekyll_compose"] || {} + end + def open_editor(filepath) run_editor(post_editor, File.expand_path(filepath)) if post_editor end @@ -30,11 +37,7 @@ def post_editor end def auto_open? - jekyll_compose_config && jekyll_compose_config["auto_open"] - end - - def jekyll_compose_config - @jekyll_compose_config ||= Jekyll.configuration["jekyll_compose"] + compose_config["auto_open"] end end end diff --git a/lib/jekyll/commands/draft.rb b/lib/jekyll/commands/draft.rb index d146039..01d3e41 100644 --- a/lib/jekyll/commands/draft.rb +++ b/lib/jekyll/commands/draft.rb @@ -25,7 +25,8 @@ def self.options end def self.process(args = [], options = {}) - params = Compose::ArgParser.new args, options + config = configuration_from_options(options) + params = Compose::ArgParser.new args, options, config params.validate! draft = DraftFileInfo.new params diff --git a/lib/jekyll/commands/page.rb b/lib/jekyll/commands/page.rb index 2b18149..cd60396 100644 --- a/lib/jekyll/commands/page.rb +++ b/lib/jekyll/commands/page.rb @@ -25,7 +25,8 @@ def self.options end def self.process(args = [], options = {}) - params = PageArgParser.new args, options + config = configuration_from_options(options) + params = PageArgParser.new args, options, config params.validate! page = PageFileInfo.new params diff --git a/lib/jekyll/commands/post.rb b/lib/jekyll/commands/post.rb index 18a88d2..31a8d15 100644 --- a/lib/jekyll/commands/post.rb +++ b/lib/jekyll/commands/post.rb @@ -26,13 +26,16 @@ def self.options end def self.process(args = [], options = {}) - params = PostArgParser.new args, options + config = configuration_from_options(options) + params = PostArgParser.new args, options, config params.validate! post = PostFileInfo.new params file_creator = Compose::FileCreator.new(post, params.force?, params.source) file_creator.create! + + Compose::FileEditor.bootstrap(config) Compose::FileEditor.open_editor(file_creator.file_path) end diff --git a/lib/jekyll/commands/publish.rb b/lib/jekyll/commands/publish.rb index 2ddedff..3aa7ec7 100644 --- a/lib/jekyll/commands/publish.rb +++ b/lib/jekyll/commands/publish.rb @@ -20,7 +20,8 @@ def self.init_with_program(prog) end def self.process(args = [], options = {}) - params = PublishArgParser.new args, options + config = configuration_from_options(options) + params = PublishArgParser.new args, options, config params.validate! movement = DraftMovementInfo.new params diff --git a/lib/jekyll/commands/unpublish.rb b/lib/jekyll/commands/unpublish.rb index fb67993..e5c7416 100644 --- a/lib/jekyll/commands/unpublish.rb +++ b/lib/jekyll/commands/unpublish.rb @@ -19,7 +19,8 @@ def self.init_with_program(prog) end def self.process(args = [], options = {}) - params = UnpublishArgParser.new args, options + config = configuration_from_options(options) + params = UnpublishArgParser.new args, options, config params.validate! movement = PostMovementInfo.new params From 66c1dd574d0dc34c9fa7a57f1d2e3e72134179e8 Mon Sep 17 00:00:00 2001 From: Ashwin Maroli Date: Tue, 9 Oct 2018 17:27:38 +0530 Subject: [PATCH 2/2] Read :config attribute of PostArgParser --- lib/jekyll/commands/post.rb | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/lib/jekyll/commands/post.rb b/lib/jekyll/commands/post.rb index 04a6c02..06d6b63 100644 --- a/lib/jekyll/commands/post.rb +++ b/lib/jekyll/commands/post.rb @@ -67,17 +67,11 @@ def _time_stamp end def content(custom_front_matter = {}) - default_front_matter = compose_config["post_default_front_matter"] + default_front_matter = params.config.dig("jekyll_compose", "post_default_front_matter") custom_front_matter.merge!(default_front_matter) if default_front_matter.is_a?(Hash) super({ "date" => _time_stamp }.merge(custom_front_matter)) end - - private - - def compose_config - @compose_config ||= Jekyll.configuration["jekyll_compose"] || {} - end end end end