-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.rb
35 lines (30 loc) · 1013 Bytes
/
parser.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# frozen_string_literal: true
require 'liquid'
require 'yaml'
module Frise
# Provides a static parse method for reading a config from a YAML file applying the required
# transformations.
module Parser
class << self
def parse(file, symbol_table = nil)
return nil unless File.file? file
YAML.safe_load(parse_as_text(file, symbol_table), aliases: true) || {}
end
def parse_as_text(file, symbol_table = nil)
return nil unless File.file? file
content = File.read(file)
template = Liquid::Template.parse(content, error_mode: :strict)
if symbol_table
content = template.render!(with_internal_vars(file, symbol_table), {
strict_filters: true
})
end
content
end
private
def with_internal_vars(file, symbol_table)
symbol_table.merge('_file_dir' => File.expand_path(File.dirname(file)))
end
end
end
end