File: file_editor.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 (46 lines) | stat: -rw-r--r-- 1,118 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
# frozen_string_literal: true

#
# This class is aimed to open the created file in the selected editor.
# To use this feature specify at Jekyll config:
#
# ```
#  jekyll_compose:
#    auto_open: true
# ```
#
# And make sure, that you have JEKYLL_EDITOR, VISUAL, or EDITOR environment variables set up.
# This will allow to open the file in your default editor automatically.

module Jekyll
  module Compose
    class FileEditor
      class << self
        attr_reader  :compose_config
        alias_method :jekyll_compose_config, :compose_config

        def bootstrap(config)
          @compose_config = config["jekyll_compose"] || {}
        end

        def open_editor(filepath)
          run_editor(post_editor, File.expand_path(filepath)) if post_editor
        end

        def run_editor(editor_name, filepath)
          system("#{editor_name} #{filepath}")
        end

        def post_editor
          return unless auto_open?

          ENV["JEKYLL_EDITOR"] || ENV["VISUAL"] || ENV["EDITOR"]
        end

        def auto_open?
          compose_config["auto_open"]
        end
      end
    end
  end
end