class Nanoc::DataSources::Filesystem::Parser
Constants
- SEPARATOR
Public Class Methods
Source
# File lib/nanoc/data_sources/filesystem/parser.rb, line 20 def initialize(config:) @config = config end
Public Instance Methods
Source
# File lib/nanoc/data_sources/filesystem/parser.rb, line 25 def call(content_filename, meta_filename) if meta_filename parse_with_separate_meta_filename(content_filename, meta_filename) else parse_with_frontmatter(content_filename) end end
@return [ParseResult]
Source
# File lib/nanoc/data_sources/filesystem/parser.rb, line 73 def frontmatter?(filename) data = Tools.read_file(filename, config: @config) /\A#{SEPARATOR}\s*$/.match?(data) end
Source
# File lib/nanoc/data_sources/filesystem/parser.rb, line 61 def parse_metadata(data, filename) begin meta = Nanoc::Core::YamlLoader.load(data) || {} rescue => e raise Errors::UnparseableMetadata.new(filename, e) end verify_meta(meta, filename) meta end
@return [Hash]
Source
# File lib/nanoc/data_sources/filesystem/parser.rb, line 42 def parse_with_frontmatter(content_filename) data = Tools.read_file(content_filename, config: @config) unless /\A#{SEPARATOR}\s*$/.match?(data) 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].sub(/\A\n/, '') ParseResult.new(content:, attributes: meta, attributes_data: pieces[2]) end
@return [ParseResult]
Source
# File lib/nanoc/data_sources/filesystem/parser.rb, line 34 def parse_with_separate_meta_filename(content_filename, meta_filename) content = content_filename ? Tools.read_file(content_filename, config: @config) : '' meta_raw = Tools.read_file(meta_filename, config: @config) meta = parse_metadata(meta_raw, meta_filename) ParseResult.new(content:, attributes: meta, attributes_data: meta_raw) end
@return [ParseResult]
Source
# File lib/nanoc/data_sources/filesystem/parser.rb, line 78 def verify_meta(meta, filename) return if meta.is_a?(Hash) raise Errors::InvalidMetadata.new(filename, meta.class) end