lib/nanoc3/data_sources/filesystem.rb in nanoc3-3.1.9 vs lib/nanoc3/data_sources/filesystem.rb in nanoc3-3.2.0a1

- old
+ new

@@ -84,12 +84,11 @@ is_binary = !!(content_filename && !@site.config[:text_extensions].include?(File.extname(content_filename)[1..-1])) if is_binary && klass == Nanoc3::Item meta = (meta_filename && YAML.load_file(meta_filename)) || {} content_or_filename = content_filename else - meta, content_or_filename = parse( - content_filename, meta_filename, kind) + meta, content_or_filename = parse(content_filename, meta_filename, kind) end # Get attributes attributes = { :filename => content_filename, @@ -122,14 +121,19 @@ mtime = content_mtime else raise RuntimeError, "meta_mtime and content_mtime are both nil" end + # Get checksum + meta_checksum = meta_filename ? Nanoc3::Checksummer.checksum_for(meta_filename) : nil + content_checksum = content_filename ? Nanoc3::Checksummer.checksum_for(content_filename) : nil + checksum = [ meta_checksum, content_checksum ].compact.join('-') + # Create layout object klass.new( content_or_filename, attributes, identifier, - :binary => is_binary, :mtime => mtime + :binary => is_binary, :mtime => mtime, :checksum => checksum ) end end # Finds all items/layouts/... in the given base directory. Returns a hash @@ -227,21 +231,21 @@ # element a hash with the file's metadata, and with its second element the # file content itself. def parse(content_filename, meta_filename, kind) # Read content and metadata from separate files if meta_filename - content = content_filename ? read(content_filename) : '' - meta = YAML.load(read(meta_filename)) || {} + content = content_filename ? File.read(content_filename) : '' + meta = YAML.load_file(meta_filename) || {} return [ meta, content ] end # Read data - data = read(content_filename) + data = File.read(content_filename) # Check presence of metadata section - if data !~ /\A-{3,5}\s*$/ + if data[0, 3] != '-'*3 && data[0, 5] != '-'*5 return [ {}, data ] end # Split data pieces = data.split(/^(-{5}|-{3})\s*$/) @@ -255,42 +259,9 @@ meta = YAML.load(pieces[2]) || {} content = pieces[4..-1].join.strip # Done [ meta, content ] - end - - # Reads the content of the file with the given name and returns a string - # in UTF-8 encoding. The original encoding of the string is derived from - # the default external encoding, but this can be overridden by the - # “encoding” configuration attribute in the data source configuration. - def read(filename) - # Read - begin - data = File.read(filename) - rescue => e - raise RuntimeError.new("Could not read #{filename}: #{e.inspect}") - end - - # Fix - if data.respond_to?(:encode!) - if @config && @config[:encoding] - original_encoding = Encoding.find(@config[:encoding]) - data.force_encoding(@config[:encoding]) - else - original_encoding = data.encoding - end - - data.encode!('UTF-8') rescue raise_encoding_error(filename, original_encoding) - raise_encoding_error(filename, original_encoding) if !data.valid_encoding? - end - - data - end - - # Raises an invalid encoding error for the given filename and encoding. - def raise_encoding_error(filename, encoding) - raise RuntimeError.new("Could not read #{filename} because the file is not valid #{encoding}.") end end end