Skip to content

Commit 8920930

Browse files
alzeihjekyllbot
authored andcommitted
Add rename command to change titles and filenames (#101)
Merge pull request 101
1 parent 2143ad2 commit 8920930

File tree

4 files changed

+577
-1
lines changed

4 files changed

+577
-1
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Listed in help you will see new commands available to you:
3030
publish # Moves a draft into the _posts directory and sets the date
3131
unpublish # Moves a post back into the _drafts directory
3232
page # Creates a new page with the given NAME
33+
rename # Moves a draft to a given NAME and sets the title
3334
compose # Creates a new file with the given NAME
3435
```
3536

@@ -78,6 +79,17 @@ Create your new draft using:
7879
$ bundle exec jekyll compose "My new draft" --collection "drafts"
7980
```
8081

82+
Rename your draft using:
83+
84+
```sh
85+
$ bundle exec jekyll rename _drafts/my-new-draft.md "My Renamed Draft"
86+
```
87+
88+
```sh
89+
# or rename it back
90+
$ bundle exec jekyll rename _drafts/my-renamed-draft.md "My new draft"
91+
```
92+
8193
Publish your draft using:
8294

8395
```sh
@@ -91,6 +103,22 @@ Publish your draft using:
91103
$ bundle exec jekyll publish _drafts/my-new-draft.md --timestamp-format "%Y-%m-%d %H:%M:%S %z"
92104
```
93105

106+
Rename your post using:
107+
108+
```sh
109+
$ bundle exec jekyll rename _posts/2014-01-24-my-new-draft.md "My New Post"
110+
```
111+
112+
```sh
113+
# or specify a specific date
114+
$ bundle exec jekyll rename _posts/2014-01-24-my-new-post.md "My Old Post" --date "2012-03-04"
115+
```
116+
117+
```sh
118+
# or specify the current date
119+
$ bundle exec jekyll rename _posts/2012-03-04-my-old-post.md "My New Post" --now
120+
```
121+
94122
Unpublish your post using:
95123

96124
```sh

lib/jekyll-compose.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ module Compose
1818
end
1919
end
2020

21-
%w(draft post publish unpublish page compose).each do |file|
21+
%w(draft post publish unpublish page rename compose).each do |file|
2222
require File.expand_path("jekyll/commands/#{file}.rb", __dir__)
2323
end

lib/jekyll/commands/rename.rb

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# frozen_string_literal: true
2+
3+
module Jekyll
4+
module Commands
5+
class Rename < Command
6+
def self.init_with_program(prog)
7+
prog.command(:rename) do |c|
8+
c.syntax "rename PATH NAME"
9+
c.description "Moves a file to a given NAME and sets the title and date"
10+
11+
options.each { |opt| c.option(*opt) }
12+
13+
c.action { |args, options| process(args, options) }
14+
end
15+
end
16+
17+
def self.options
18+
[
19+
["force", "-f", "--force", "Overwrite a post if it already exists"],
20+
["config", "--config CONFIG_FILE[,CONFIG_FILE2,...]", Array, "Custom configuration file"],
21+
["date", "-d DATE", "--date DATE", "Specify the date"],
22+
["now", "--now", "Specify the date as now"],
23+
]
24+
end
25+
26+
def self.process(args = [], options = {})
27+
config = configuration_from_options(options)
28+
params = RenameArgParser.new(args, options, config)
29+
params.validate!
30+
31+
movement = RenameMovementInfo.new(params)
32+
33+
mover = RenameMover.new(movement, params.force?, params.source)
34+
mover.move
35+
end
36+
end
37+
38+
class RenameArgParser < Compose::ArgParser
39+
def validate!
40+
raise ArgumentError, "You must specify current path and the new title." if args.length < 2
41+
42+
if options.values_at("date", "now").compact.length > 1
43+
raise ArgumentError, "You can only specify one of --date DATE or --now."
44+
end
45+
end
46+
47+
def current_path
48+
@current_path ||= args[0]
49+
end
50+
51+
def path
52+
File.join(source, current_path).sub(%r!\A/!, "")
53+
end
54+
55+
def dirname
56+
@dirname ||= File.dirname(current_path)
57+
end
58+
59+
def basename
60+
@basename ||= File.basename(current_path)
61+
end
62+
63+
def title
64+
@title ||= args.drop(1).join(" ")
65+
end
66+
67+
def touch?
68+
!!options["date"] || options["now"]
69+
end
70+
71+
def date
72+
@date ||= if options["now"]
73+
Time.now
74+
elsif options["date"]
75+
Date.parse(options["date"])
76+
end
77+
end
78+
79+
def date_from_filename
80+
Date.parse(Regexp.last_match(1)) if basename =~ Jekyll::Document::DATE_FILENAME_MATCHER
81+
end
82+
83+
def post?
84+
dirname == "_posts"
85+
end
86+
87+
def draft?
88+
dirname == "_drafts"
89+
end
90+
end
91+
92+
class RenameMovementInfo < Compose::FileInfo
93+
attr_reader :params
94+
def initialize(params)
95+
@params = params
96+
end
97+
98+
def from
99+
params.path
100+
end
101+
102+
def resource_type
103+
if @params.post?
104+
"post"
105+
elsif @params.draft?
106+
"draft"
107+
else
108+
"file"
109+
end
110+
end
111+
112+
def to
113+
if @params.post?
114+
File.join(@params.dirname, "#{date_stamp}-#{file_name}")
115+
else
116+
File.join(@params.dirname, file_name)
117+
end
118+
end
119+
120+
def front_matter(data)
121+
data["title"] = params.title
122+
data["date"] = time_stamp if @params.touch?
123+
data
124+
end
125+
126+
private
127+
128+
def date_stamp
129+
if @params.touch?
130+
@params.date.strftime Jekyll::Compose::DEFAULT_DATESTAMP_FORMAT
131+
else
132+
@params.date_from_filename.strftime Jekyll::Compose::DEFAULT_DATESTAMP_FORMAT
133+
end
134+
end
135+
136+
def time_stamp
137+
@params.date.strftime Jekyll::Compose::DEFAULT_TIMESTAMP_FORMAT
138+
end
139+
end
140+
141+
class RenameMover < Compose::FileMover
142+
def resource_type_from
143+
@movement.resource_type
144+
end
145+
alias_method :resource_type_to, :resource_type_from
146+
end
147+
end
148+
end

0 commit comments

Comments
 (0)