lib/nanoc/data_sources/filesystem.rb in nanoc-4.8.18 vs lib/nanoc/data_sources/filesystem.rb in nanoc-4.8.19

- old
+ new

@@ -377,116 +377,15 @@ else false end end - # @return [ParseResult] def parse(content_filename, meta_filename) - if meta_filename - parse_with_separate_meta_filename(content_filename, meta_filename) - else - parse_with_frontmatter(content_filename) - end + parser = Parser.new(config: @config) + parser.call(content_filename, meta_filename) end - - # @return [ParseResult] - def parse_with_separate_meta_filename(content_filename, meta_filename) - content = content_filename ? read(content_filename) : '' - meta_raw = read(meta_filename) - meta = parse_metadata(meta_raw, meta_filename) - ParseResult.new(content: content, attributes: meta, attributes_data: meta_raw) - end - - SEPARATOR = /(-{5}|-{3})/.source - - # @return [ParseResult] - def parse_with_frontmatter(content_filename) - data = read(content_filename) - - if data !~ /\A#{SEPARATOR}\s*$/ - return ParseResult.new(content: data, attributes: {}, attributes_data: '') - end - - pieces = data.split(/^#{SEPARATOR}[ \t]*\r?\n?/, 3) - if pieces.size < 4 - raise Errors::InvalidFormat.new(content_filename) - end - - meta = parse_metadata(pieces[2], content_filename) - content = pieces[4] - - ParseResult.new(content: content, attributes: meta, attributes_data: pieces[2]) - end - - # @return [Hash] - def parse_metadata(data, filename) - begin - meta = YAML.load(data) || {} - rescue => e - raise Errors::UnparseableMetadata.new(filename, e) - end - - verify_meta(meta, filename) - - meta - end - - class ParseResult - attr_reader :content - attr_reader :attributes - attr_reader :attributes_data - - def initialize(content:, attributes:, attributes_data:) - @content = content - @attributes = attributes - @attributes_data = attributes_data - end - end - - def verify_meta(meta, filename) - return if meta.is_a?(Hash) - - raise Errors::InvalidMetadata.new(filename, meta.class) - 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 Errors::FileUnreadable.new(filename, e) - end - - # Set original encoding, if any - if @config && @config[:encoding] - original_encoding = Encoding.find(@config[:encoding]) - data.force_encoding(@config[:encoding]) - else - original_encoding = data.encoding - end - - # Set encoding to UTF-8 - begin - data.encode!('UTF-8') - rescue - raise Errors::InvalidEncoding.new(filename, original_encoding) - end - - # Verify - unless data.valid_encoding? - raise Errors::InvalidEncoding.new(filename, original_encoding) - end - - # Remove UTF-8 BOM (ugly) - data.delete!("\xEF\xBB\xBF") - - data - end end end require_relative 'filesystem/tools' require_relative 'filesystem/errors' +require_relative 'filesystem/parser'