File: unpublish.rb

package info (click to toggle)
ruby-jekyll-compose 0.12.0-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 108 kB
  • sloc: ruby: 712; makefile: 3
file content (75 lines) | stat: -rw-r--r-- 1,690 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# frozen_string_literal: true

module Jekyll
  module Commands
    class Unpublish < Command
      def self.init_with_program(prog)
        prog.command(:unpublish) do |c|
          c.syntax "unpublish POST_PATH"
          c.description "Moves a post back into the _drafts directory"

          options.each { |opt| c.option(*opt) }

          c.action { |args, options| process(args, options) }
        end
      end

      def self.options
        [
          ["config", "--config CONFIG_FILE[,CONFIG_FILE2,...]", Array, "Custom configuration file"],
          ["force", "-f", "--force", "Overwrite a draft if it already exists"],
        ]
      end

      def self.process(args = [], options = {})
        config = configuration_from_options(options)
        params = UnpublishArgParser.new(args, options, config)
        params.validate!

        movement = PostMovementInfo.new(params)

        mover = PostMover.new(movement, params.force?, params.source)
        mover.move
      end
    end

    class UnpublishArgParser < Compose::MovementArgParser
      def resource_type
        "post"
      end

      def name
        File.basename(path).sub %r!\d{4}-\d{2}-\d{2}-!, ""
      end
    end

    class PostMovementInfo
      attr_reader :params
      def initialize(params)
        @params = params
      end

      def from
        params.path
      end

      def to
        "_drafts/#{params.name}"
      end

      def front_matter(data)
        data.reject { |key, _value| key == "date" }
      end
    end

    class PostMover < Compose::FileMover
      def resource_type_from
        "post"
      end

      def resource_type_to
        "draft"
      end
    end
  end
end