|
| 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