module Jekyll::DocumentWriter
Public Class Methods
included(base)
click to toggle source
# File lib/jekyll/document_writer.rb 7 def self.included(base) 8 base.class_eval do 9 # Writes changes to the file and adds it to git staged changes 10 def save 11 return unless data.fetch('save', true) 12 13 Jekyll.logger.debug "Writing #{relative_path}" 14 15 file = File.open(path, File::RDWR | File::CREAT, 0o640) do |f| 16 f.flock(File::LOCK_EX) 17 f.rewind 18 19 f.write(sanitized_data.to_yaml) 20 f.write("---\n\n") 21 f.write(content) 22 23 f.flush 24 f.truncate(f.pos) 25 end 26 27 site.staged_files << relative_path if file.zero? 28 29 file.zero? 30 end 31 32 # Prepares the data for dumping, by excluding some keys and 33 # transforming other Documents replaced by jekyll-linked-posts. 34 # 35 # @return [Hash] 36 def sanitized_data 37 data.reject do |k, _| 38 excluded_attributes.include? k 39 end.transform_values do |value| 40 case value 41 when Jekyll::Document 42 value.data['uuid'] 43 when Jekyll::Convertible 44 value.data['uuid'] 45 when Array 46 value.map do |v| 47 v.respond_to?(:data) ? v.data['uuid'] : v 48 end 49 when Hash 50 value.transform_values do |v| 51 v.respond_to?(:data) ? v.data['uuid'] : v 52 end 53 else 54 value 55 end 56 end 57 end 58 59 def excluded_attributes 60 @excluded_attributes ||= %w[slug ext date excerpt] 61 end 62 end 63 end
Public Instance Methods
excluded_attributes()
click to toggle source
# File lib/jekyll/document_writer.rb 59 def excluded_attributes 60 @excluded_attributes ||= %w[slug ext date excerpt] 61 end
sanitized_data()
click to toggle source
Prepares the data for dumping, by excluding some keys and transforming other Documents replaced by jekyll-linked-posts.
@return [Hash]
# File lib/jekyll/document_writer.rb 36 def sanitized_data 37 data.reject do |k, _| 38 excluded_attributes.include? k 39 end.transform_values do |value| 40 case value 41 when Jekyll::Document 42 value.data['uuid'] 43 when Jekyll::Convertible 44 value.data['uuid'] 45 when Array 46 value.map do |v| 47 v.respond_to?(:data) ? v.data['uuid'] : v 48 end 49 when Hash 50 value.transform_values do |v| 51 v.respond_to?(:data) ? v.data['uuid'] : v 52 end 53 else 54 value 55 end 56 end 57 end
save()
click to toggle source
Writes changes to the file and adds it to git staged changes
# File lib/jekyll/document_writer.rb 10 def save 11 return unless data.fetch('save', true) 12 13 Jekyll.logger.debug "Writing #{relative_path}" 14 15 file = File.open(path, File::RDWR | File::CREAT, 0o640) do |f| 16 f.flock(File::LOCK_EX) 17 f.rewind 18 19 f.write(sanitized_data.to_yaml) 20 f.write("---\n\n") 21 f.write(content) 22 23 f.flush 24 f.truncate(f.pos) 25 end 26 27 site.staged_files << relative_path if file.zero? 28 29 file.zero? 30 end