Sha256: 7acbbe6fab7e99085ff5355567e6a6d5392e32f8177442113a9b9aedcc5bd7a5

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

module Jekyll
  module Commands
    # Jekyll subcommand <zettel>
    class Zettel < Command
      class << self

        def init_with_program(prog)
          prog.command(:zettel) do |c|
            c.syntax 'zettel'
            c.description 'Creates a new Zettel within subdir zettel'
            c.action do |_args, _options|
              uuid = new_zettel
              new_page(uuid, "zettel/#{uuid}/index.md")

              Jekyll.logger.info '✓', "Created new Zettel with UUID: `#{uuid}`"
            end
          end
        end

        def new_zettel
          uuid = SecureRandom.uuid
          dir = "zettel/#{uuid}"
          return new_zettel if File.directory?(dir)

          FileUtils.mkdir_p(dir)
          uuid
        end

        def new_page(uuid, file)
          File.open(file, 'w') { |out|
            out.write evaluate_template(uuid, 'index.md')
          }
        end

        # rubocop:disable Style/EvalWithLocation, Security/Eval, Lint/UnusedMethodArgument
        def evaluate_template(uuid, file)
          template = File.read(File.expand_path("../stubs/#{file}", __dir__))
          eval("\"#{template}\"")
        end
        # rubocop:enable Style/EvalWithLocation, Security/Eval, Lint/UnusedMethodArgument

      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jekyll-zettel-0.1.0 lib/jekyll/zettel/commands/zettel.rb