Skip to content

Commit

Permalink
Generate configuration from CLI options (#76)
Browse files Browse the repository at this point in the history
Merge pull request 76
  • Loading branch information
ashmaroli authored and jekyllbot committed Oct 31, 2018
1 parent cd4a687 commit 5413e92
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 19 deletions.
6 changes: 4 additions & 2 deletions lib/jekyll-compose/arg_parser.rb
Expand Up @@ -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!
Expand Down
13 changes: 8 additions & 5 deletions lib/jekyll-compose/file_editor.rb
Expand Up @@ -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
Expand All @@ -31,11 +38,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
Expand Down
3 changes: 2 additions & 1 deletion lib/jekyll/commands/draft.rb
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/jekyll/commands/page.rb
Expand Up @@ -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
Expand Down
13 changes: 5 additions & 8 deletions lib/jekyll/commands/post.rb
Expand Up @@ -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

Expand Down Expand Up @@ -64,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
Expand Down
3 changes: 2 additions & 1 deletion lib/jekyll/commands/publish.rb
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/jekyll/commands/unpublish.rb
Expand Up @@ -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
Expand Down

0 comments on commit 5413e92

Please sign in to comment.