jekyll-compose 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 284c0224140f97b3f80f0041330f7e20790c24d6
4
- data.tar.gz: 5c736a3ec82eef5328aa097d4351b6c81f1a9807
3
+ metadata.gz: d24fea17086602339c5733e9f7ff462f7873d688
4
+ data.tar.gz: 760ec8a158fc02edc26483568b38e55bce6fda0e
5
5
  SHA512:
6
- metadata.gz: 38ece0d02394f8b0d5d5937cc629664d70b2b0684cd98ec4b5bab44f79fa3130b9b928c0b50efc3fd4d51dd2c368b700f1ca2678eaf3dcf88d829f309065c8cd
7
- data.tar.gz: 91bac058225f3c0811684567805db2db4c45d6d161f98cd0913a013153b192b7e74fd0687c3611574a2880273bc7e48ea2740d2d254d3e6cc7d96bab19677cbf
6
+ metadata.gz: c0c1164e65ae1eb4838a9acbe8cf03f95a95e754f28c7f8f088b8d8d5d449992f257e2bc881f32a30e6cd262ddbf37559ec310e16622f5e36854fce020388a94
7
+ data.tar.gz: 0da8db0f0c55356953d8446abd27aa4f2aa1c47eefd4a3fa61ca2857f1b52fdaac5ea8068d6f24b079958ef6362cc394dcce623c25b285dbc4fba7749ececdb1
@@ -1,12 +1,18 @@
1
1
  require "jekyll-compose/version"
2
+ require "jekyll-compose/arg_parser"
3
+ require "jekyll-compose/movement_arg_parser"
4
+ require "jekyll-compose/file_creator"
5
+ require "jekyll-compose/file_mover"
6
+ require "jekyll-compose/file_info"
2
7
 
3
8
  module Jekyll
4
9
  module Compose
5
10
  DEFAULT_TYPE = "md"
6
11
  DEFAULT_LAYOUT = "post"
12
+ DEFAULT_LAYOUT_PAGE = "page"
7
13
  end
8
14
  end
9
15
 
10
- %w{draft post publish}.each do |file|
16
+ %w{draft post publish unpublish page}.each do |file|
11
17
  require File.expand_path("jekyll/commands/#{file}.rb", File.dirname(__FILE__))
12
18
  end
@@ -0,0 +1,27 @@
1
+ class Jekyll::Compose::ArgParser
2
+ attr_reader :args, :options
3
+ def initialize(args, options)
4
+ @args = args
5
+ @options = options
6
+ end
7
+
8
+ def validate!
9
+ raise ArgumentError.new('You must specify a name.') if args.empty?
10
+ end
11
+
12
+ def type
13
+ type = options["extension"] || Jekyll::Compose::DEFAULT_TYPE
14
+ end
15
+
16
+ def layout
17
+ layout = options["layout"] || Jekyll::Compose::DEFAULT_LAYOUT
18
+ end
19
+
20
+ def title
21
+ args.join ' '
22
+ end
23
+
24
+ def force?
25
+ !!options["force"]
26
+ end
27
+ end
@@ -0,0 +1,36 @@
1
+ module Jekyll
2
+ module Compose
3
+ class FileCreator
4
+ attr_reader :file, :force
5
+ def initialize(fileInfo, force = false)
6
+ @file = fileInfo
7
+ @force = force
8
+ end
9
+
10
+ def create!
11
+ validate_should_write!
12
+ ensure_directory_exists
13
+ write_file
14
+ end
15
+
16
+ private
17
+
18
+ def validate_should_write!
19
+ raise ArgumentError.new("A #{file.resource_type} already exists at #{file.path}") if File.exist?(file.path) and !force
20
+ end
21
+
22
+ def ensure_directory_exists
23
+ dir = File.dirname file.path
24
+ Dir.mkdir(dir) unless Dir.exist?(dir)
25
+ end
26
+
27
+ def write_file
28
+ File.open(file.path, "w") do |f|
29
+ f.puts(file.content)
30
+ end
31
+
32
+ puts "New #{file.resource_type} created at #{file.path}."
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,20 @@
1
+ class Jekyll::Compose::FileInfo
2
+ attr_reader :params
3
+ def initialize(params)
4
+ @params = params
5
+ end
6
+
7
+ def file_name
8
+ name = Jekyll::Utils.slugify params.title
9
+ "#{name}.#{params.type}"
10
+ end
11
+
12
+ def content
13
+ <<-CONTENT.gsub /^\s+/, ''
14
+ ---
15
+ layout: #{params.layout}
16
+ title: #{params.title}
17
+ ---
18
+ CONTENT
19
+ end
20
+ end
@@ -0,0 +1,34 @@
1
+ module Jekyll
2
+ module Compose
3
+ class FileMover
4
+ attr_reader :movement
5
+ def initialize(movement)
6
+ @movement = movement
7
+ end
8
+
9
+ def resource_type
10
+ 'file'
11
+ end
12
+
13
+ def move
14
+ validate_source
15
+ ensure_directory_exists
16
+ move_file
17
+ end
18
+
19
+ def validate_source
20
+ raise ArgumentError.new("There was no #{resource_type} found at '#{movement.from}'.") unless File.exist? movement.from
21
+ end
22
+
23
+ def ensure_directory_exists
24
+ dir = File.dirname movement.to
25
+ Dir.mkdir(dir) unless Dir.exist?(dir)
26
+ end
27
+
28
+ def move_file
29
+ FileUtils.mv(movement.from, movement.to)
30
+ puts "#{resource_type.capitalize} #{movement.from} was moved to #{movement.to}"
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,19 @@
1
+ module Jekyll
2
+ module Compose
3
+ class MovementArgParser
4
+ attr_reader :args, :options
5
+ def initialize(args, options)
6
+ @args = args
7
+ @options = options
8
+ end
9
+
10
+ def validate!
11
+ raise ArgumentError.new("You must specify a #{resource_type} path.") if args.empty?
12
+ end
13
+
14
+ def path
15
+ args.join ' '
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,5 +1,5 @@
1
1
  module Jekyll
2
2
  module Compose
3
- VERSION = "0.2.1"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
@@ -6,48 +6,38 @@ module Jekyll
6
6
  c.syntax 'draft NAME'
7
7
  c.description 'Creates a new draft post with the given NAME'
8
8
 
9
- c.option 'type', '-t TYPE', '--type TYPE', 'Specify the content type'
10
- c.option 'layout', '-l LAYOUT', '--layout LAYOUT', 'Specify the post layout'
11
- c.option 'force', '-f', '--force', 'Overwrite a draft if it already exists'
9
+ options.each {|opt| c.option *opt }
12
10
 
13
- c.action do |args, options|
14
- Jekyll::Commands::Draft.process(args, options)
15
- end
11
+ c.action { |args, options| process args, options }
16
12
  end
17
13
  end
18
14
 
19
- def self.process(args = [], options = {})
20
- raise ArgumentError.new('You must specify a name.') if args.empty?
15
+ def self.options
16
+ [
17
+ ['extension', '-x EXTENSION', '--extension EXTENSION', 'Specify the file extension'],
18
+ ['layout', '-l LAYOUT', '--layout LAYOUT', "Specify the draft layout"],
19
+ ['force', '-f', '--force', 'Overwrite a draft if it already exists']
20
+ ]
21
+ end
21
22
 
22
- type = options["type"] || Jekyll::Compose::DEFAULT_TYPE
23
- layout = options["layout"] || Jekyll::Compose::DEFAULT_LAYOUT
24
23
 
25
- title = args.shift
26
- name = title.gsub(' ', '-').downcase
24
+ def self.process(args = [], options = {})
25
+ params = Compose::ArgParser.new args, options
26
+ params.validate!
27
27
 
28
- draft_path = draft_name(name, type)
28
+ draft = DraftFileInfo.new params
29
29
 
30
- raise ArgumentError.new("A draft already exists at ./#{draft_path}") if File.exist?(draft_path) and !options["force"]
30
+ Compose::FileCreator.new(draft, params.force?).create!
31
+ end
31
32
 
32
- Dir.mkdir("_drafts") unless Dir.exist?("_drafts")
33
- File.open(draft_path, "w") do |f|
34
- f.puts(front_matter(layout, title))
33
+ class DraftFileInfo < Compose::FileInfo
34
+ def resource_type
35
+ 'draft'
35
36
  end
36
37
 
37
- puts "New draft created at ./#{draft_path}.\n"
38
- end
39
- # Internal: Gets the filename of the draft to be created
40
- #
41
- # Returns the filename of the draft, as a String
42
- def self.draft_name(name, ext=Jekyll::Compose::DEFAULT_TYPE)
43
- "_drafts/#{name}.#{ext}"
44
- end
45
-
46
- def self.front_matter(layout, title)
47
- {
48
- "layout" => layout,
49
- "title" => title,
50
- }.to_yaml + "\n---\n"
38
+ def path
39
+ "_drafts/#{file_name}"
40
+ end
51
41
  end
52
42
  end
53
43
  end
@@ -0,0 +1,48 @@
1
+ module Jekyll
2
+ module Commands
3
+ class Page < Command
4
+ def self.init_with_program(prog)
5
+ prog.command(:page) do |c|
6
+ c.syntax 'page NAME'
7
+ c.description 'Creates a new page with the given NAME'
8
+
9
+ options.each {|opt| c.option *opt }
10
+
11
+ c.action { |args, options| process args, options }
12
+ end
13
+ end
14
+
15
+ def self.options
16
+ [
17
+ ['extension', '-x EXTENSION', '--extension EXTENSION', 'Specify the file extension'],
18
+ ['layout', '-l LAYOUT', '--layout LAYOUT', "Specify the page layout"],
19
+ ['force', '-f', '--force', 'Overwrite a page if it already exists']
20
+ ]
21
+ end
22
+
23
+ def self.process(args = [], options = {})
24
+ params = PageArgParser.new args, options
25
+ params.validate!
26
+
27
+ page = PageFileInfo.new params
28
+
29
+ Compose::FileCreator.new(page, params.force?).create!
30
+ end
31
+
32
+ class PageArgParser < Compose::ArgParser
33
+ def layout
34
+ layout = options["layout"] || Jekyll::Compose::DEFAULT_LAYOUT_PAGE
35
+ end
36
+ end
37
+
38
+ class PageFileInfo < Compose::FileInfo
39
+ def resource_type
40
+ 'page'
41
+ end
42
+
43
+ alias_method :path, :file_name
44
+
45
+ end
46
+ end
47
+ end
48
+ end
@@ -6,50 +6,53 @@ module Jekyll
6
6
  c.syntax 'post NAME'
7
7
  c.description 'Creates a new post with the given NAME'
8
8
 
9
- c.option 'type', '-t TYPE', '--type TYPE', 'Specify the content type'
10
- c.option 'layout', '-t LAYOUT', '--layout LAYOUT', 'Specify the post layout'
11
- c.option 'date', '-d DATE', '--date DATE', 'Specify the post date'
12
- c.option 'force', '-f', '--force', 'Overwrite a post if it already exists'
13
-
14
- c.action do |args, options|
15
- Jekyll::Commands::Post.process(args, options)
16
- end
9
+ options.each {|opt| c.option *opt }
10
+
11
+ c.action { |args, options| process args, options }
17
12
  end
18
13
  end
19
14
 
15
+ def self.options
16
+ [
17
+ ['extension', '-x EXTENSION', '--extension EXTENSION', 'Specify the file extension'],
18
+ ['layout', '-l LAYOUT', '--layout LAYOUT', "Specify the post layout"],
19
+ ['force', '-f', '--force', 'Overwrite a post if it already exists'],
20
+ ['date', '-d DATE', '--date DATE', 'Specify the post date']
21
+ ]
22
+ end
23
+
20
24
  def self.process(args = [], options = {})
21
- raise ArgumentError.new('You must specify a name.') if args.empty?
25
+ params = PostArgParser.new args, options
26
+ params.validate!
22
27
 
23
- type = options["type"] || Jekyll::Compose::DEFAULT_TYPE
24
- layout = options["layout"] || Jekyll::Compose::DEFAULT_LAYOUT
28
+ post = PostFileInfo.new params
25
29
 
26
- date = options["date"].nil? ? Time.now : DateTime.parse(options["date"])
30
+ Compose::FileCreator.new(post, params.force?).create!
31
+ end
27
32
 
28
- title = args.shift
29
- name = title.gsub(' ', '-').downcase
30
33
 
31
- post_path = file_name(name, type, date)
34
+ class PostArgParser < Compose::ArgParser
35
+ def date
36
+ options["date"].nil? ? Time.now : DateTime.parse(options["date"])
37
+ end
38
+ end
32
39
 
33
- raise ArgumentError.new("A post already exists at ./#{post_path}") if File.exist?(post_path) and !options["force"]
40
+ class PostFileInfo < Compose::FileInfo
41
+ def resource_type
42
+ 'post'
43
+ end
34
44
 
35
- File.open(post_path, "w") do |f|
36
- f.puts(front_matter(layout, title))
45
+ def path
46
+ "_posts/#{file_name}"
37
47
  end
38
48
 
39
- puts "New post created at ./#{post_path}.\n"
40
- end
41
- # Internal: Gets the filename of the draft to be created
42
- #
43
- # Returns the filename of the draft, as a String
44
- def self.file_name(name, ext, date)
45
- "_posts/#{date.strftime('%Y-%m-%d')}-#{name}.#{ext}"
46
- end
49
+ def file_name
50
+ "#{_date_stamp}-#{super}"
51
+ end
47
52
 
48
- def self.front_matter(layout, title)
49
- "---
50
- layout: #{layout}
51
- title: #{title}
52
- ---"
53
+ def _date_stamp
54
+ @params.date.strftime '%Y-%m-%d'
55
+ end
53
56
  end
54
57
  end
55
58
  end
@@ -15,30 +15,51 @@ module Jekyll
15
15
  end
16
16
 
17
17
  def self.process(args = [], options = {})
18
- raise ArgumentError.new('You must specify a draft path.') if args.empty?
18
+ params = PublishArgParser.new args, options
19
+ params.validate!
19
20
 
20
- date = options["date"].nil? ? Date.today : Date.parse(options["date"])
21
- draft_path = args.shift
21
+ movement = DraftMovementInfo.new params
22
22
 
23
- raise ArgumentError.new("There was no draft found at '#{draft_path}'.") unless File.exist? draft_path
23
+ mover = DraftMover.new movement
24
+ mover.move
25
+ end
26
+
27
+ end
24
28
 
25
- post_path = post_name(date, draft_name(draft_path))
26
- FileUtils.mv(draft_path, post_path)
29
+ class PublishArgParser < Compose::MovementArgParser
30
+ def resource_type
31
+ "draft"
32
+ end
27
33
 
28
- puts "Draft #{draft_path} was published to ./#{post_path}"
34
+ def date
35
+ options["date"].nil? ? Date.today : Date.parse(options["date"])
29
36
  end
30
37
 
31
- # Internal: Gets the filename of the post to be created
32
- #
33
- # Returns the filename of the post, as a String
34
- def self.post_name(date, name)
35
- "_posts/#{date.strftime('%Y-%m-%d')}-#{name}"
38
+ def name
39
+ File.basename path
36
40
  end
41
+ end
37
42
 
38
- def self.draft_name(path)
39
- File.basename(path)
43
+ class DraftMovementInfo
44
+ attr_reader :params
45
+ def initialize(params)
46
+ @params = params
40
47
  end
41
48
 
49
+ def from
50
+ params.path
51
+ end
52
+
53
+ def to
54
+ date_stamp = params.date.strftime '%Y-%m-%d'
55
+ "_posts/#{date_stamp}-#{params.name}"
56
+ end
57
+ end
58
+
59
+ class DraftMover < Compose::FileMover
60
+ def resource_type
61
+ 'draft'
62
+ end
42
63
  end
43
64
  end
44
65
  end
@@ -0,0 +1,58 @@
1
+ module Jekyll
2
+ module Commands
3
+ class Unpublish < Command
4
+ def self.init_with_program(prog)
5
+ prog.command(:unpublish) do |c|
6
+ c.syntax 'unpublish POST_PATH'
7
+ c.description 'Moves a post back into the _drafts directory'
8
+
9
+ c.action do |args, options|
10
+ process(args, options)
11
+ end
12
+ end
13
+ end
14
+
15
+ def self.process(args = [], options = {})
16
+ params = UnpublishArgParser.new args, options
17
+ params.validate!
18
+
19
+ movement = PostMovementInfo.new params
20
+
21
+ mover = PostMover.new movement
22
+ mover.move
23
+ end
24
+
25
+ end
26
+
27
+ class UnpublishArgParser < Compose::MovementArgParser
28
+ def resource_type
29
+ 'post'
30
+ end
31
+
32
+ def name
33
+ File.basename(path).sub /\d{4}-\d{2}-\d{2}-/, ''
34
+ end
35
+ end
36
+
37
+ class PostMovementInfo
38
+ attr_reader :params
39
+ def initialize(params)
40
+ @params = params
41
+ end
42
+
43
+ def from
44
+ params.path
45
+ end
46
+
47
+ def to
48
+ "_drafts/#{params.name}"
49
+ end
50
+ end
51
+
52
+ class PostMover < Compose::FileMover
53
+ def resource_type
54
+ 'post'
55
+ end
56
+ end
57
+ end
58
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-compose
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Parker Moore
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-17 00:00:00.000000000 Z
11
+ date: 2015-08-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -74,10 +74,17 @@ extensions: []
74
74
  extra_rdoc_files: []
75
75
  files:
76
76
  - lib/jekyll-compose.rb
77
+ - lib/jekyll-compose/arg_parser.rb
78
+ - lib/jekyll-compose/file_creator.rb
79
+ - lib/jekyll-compose/file_info.rb
80
+ - lib/jekyll-compose/file_mover.rb
81
+ - lib/jekyll-compose/movement_arg_parser.rb
77
82
  - lib/jekyll-compose/version.rb
78
83
  - lib/jekyll/commands/draft.rb
84
+ - lib/jekyll/commands/page.rb
79
85
  - lib/jekyll/commands/post.rb
80
86
  - lib/jekyll/commands/publish.rb
87
+ - lib/jekyll/commands/unpublish.rb
81
88
  homepage: https://github.com/jekyll/jekyll-compose
82
89
  licenses:
83
90
  - MIT
@@ -98,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
98
105
  version: '0'
99
106
  requirements: []
100
107
  rubyforge_project:
101
- rubygems_version: 2.2.2
108
+ rubygems_version: 2.2.3
102
109
  signing_key:
103
110
  specification_version: 4
104
111
  summary: Streamline your writing in Jekyll with these commands.