Class: Nanoc::DataSources::Filesystem::Parser Private

Inherits:
Object
  • Object
show all
Defined in:
lib/nanoc/data_sources/filesystem/parser.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Defined Under Namespace

Classes: ParseResult

Constant Summary collapse

SEPARATOR =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

/(-{5}|-{3})/.source

Instance Method Summary collapse

Constructor Details

#initialize(config:) ⇒ Parser

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Parser.



20
21
22
# File 'lib/nanoc/data_sources/filesystem/parser.rb', line 20

def initialize(config:)
  @config = config
end

Instance Method Details

#call(content_filename, meta_filename) ⇒ ParseResult

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



25
26
27
28
29
30
31
# 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

#frontmatter?(filename) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


73
74
75
76
# 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

#parse_metadata(data, filename) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Hash)


61
62
63
64
65
66
67
68
69
70
71
# File 'lib/nanoc/data_sources/filesystem/parser.rb', line 61

def (data, filename)
  begin
    meta = Nanoc::Core::YamlLoader.load(data) || {}
  rescue => e
    raise Errors::UnparseableMetadata.new(filename, e)
  end

  verify_meta(meta, filename)

  meta
end

#parse_with_frontmatter(content_filename) ⇒ ParseResult

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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 = (pieces[2], content_filename)
  content = pieces[4].sub(/\A\n/, '')

  ParseResult.new(content:, attributes: meta, attributes_data: pieces[2])
end

#parse_with_separate_meta_filename(content_filename, meta_filename) ⇒ ParseResult

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



34
35
36
37
38
39
# 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 = (meta_raw, meta_filename)
  ParseResult.new(content:, attributes: meta, attributes_data: meta_raw)
end

#verify_meta(meta, filename) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



78
79
80
81
82
# 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