Module: Jekyll::DocumentWriter

Defined in:
lib/jekyll/document_writer.rb

Constant Summary collapse

EXCLUDED_ATTRIBUTES =
%w[slug ext date excerpt].freeze
SUTTY_FIELDS =
%w[permalink alternate_permalinks layout uuid render_with_liquid usuaries created_at last_modified_at].freeze

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/jekyll/document_writer.rb', line 11

def self.included(base)
  base.class_eval do
    # Writes changes to the file and adds it to git staged changes
    #
    # @return [Bool,nil]
    def save
      return unless data.fetch('save', true)

      if File.exist?(path)
        file_digest = Digest::SHA2.file(path).hexdigest
        content_digest = Digest::SHA2.hexdigest(writable_content)

        return if file_digest == content_digest

        Jekyll.logger.debug 'Writing:', real_path
      else
        Jekyll.logger.debug 'Creating:', path
      end

      file = File.open(path, File::RDWR | File::CREAT, 0o640) do |f|
        f.flock(File::LOCK_EX)
        f.rewind
        f.write(writable_content)
        f.flush
        f.truncate(f.pos)
      end

      site.staged_files << real_path if file.zero?

      file.zero?
    end

    # Prepares the data for dumping, by excluding some keys and
    # transforming other Documents replaced by jekyll-linked-posts.
    #
    # @return [Hash]
    def sanitized_data
      data.slice(*attributes).transform_values do |value|
        case value
        when Jekyll::Document, Jekyll::Convertible
          value.data['uuid']
        when Array, Set
          value.map do |v|
            v.respond_to?(:data) ? v.data['uuid'] : v
          end
        when Hash
          value.transform_values do |v|
            v.respond_to?(:data) ? v.data['uuid'] : v
          end
        else
          value
        end
      end
    end

    private

    # Resolves the real path for the document
    #
    # @return [String]
    def real_path
      @real_path ||= Pathname.new(relative_path).realpath.relative_path_from(site.source).to_s
    rescue Errno::ENOENT
      relative_path
    end

    # @return [Array<String>]
    def attributes
      @attributes ||= data.keys - EXCLUDED_ATTRIBUTES
    end

    def layout?
      layout.is_a? Hash
    end

    # @return [Hash,nil]
    def layout
      @layout ||= site.data.dig('layouts', data['layout'])
    end

    def fields
      @fields ||= (layout.keys + SUTTY_FIELDS).uniq
    end

    # @return [String]
    def writable_content
      @writable_content ||=
        begin
          dumpable_data = sanitized_data
          dumpable_data = dumpable_data.slice(*fields) if layout?

          <<~CONTENT
            #{dumpable_data.to_yaml.chomp}
            ---

            #{content.chomp}
          CONTENT
        end
    end
  end
end