module Pandocomatic::PandocomaticYAML
PandocomaticYAML
is a wrapper around ruby’s YAML library that replaces occurrences of “$(X)$” in the YAML by the value of the evironment variable
-
If variable X cannot be found, an exception is thrown.
Constants
- PERMITTED_YAML_CLASSES
List of classes that are permitted in YAML
- VAR_PATTERN
Regular expression representing environment variables in templates
Public Class Methods
Load a string, substitute any variables, and parse as YAML.
@param str [String] String to parse as YAML @param path [Path|Nil] Path
of the source of the string, if any @return [Hash] @raise [TemplateError] when environment variable does not exist.
# File lib/pandocomatic/pandocomatic_yaml.rb, line 43 def self.load(str, path = nil) YAML.safe_load substitute_variables(str, path), permitted_classes: PERMITTED_YAML_CLASSES end
Load a text file, substitute any variables, and parse as YAML.
@param path [String] Path
to text file to parse as YAML @return [Hash] @raise [TemplateError] when environment variable does not exist.
# File lib/pandocomatic/pandocomatic_yaml.rb, line 52 def self.load_file(path) self.load File.read(path), path end
Substitute all environment variables in the str with the values of the corresponding environment variables.
@raise [TemplateError] when environment variable does not exist.
# File lib/pandocomatic/pandocomatic_yaml.rb, line 60 def self.substitute_variables(str, path = nil) str.gsub(VAR_PATTERN) do |_match| key = Regexp.last_match(1) raise TemplateError.new(:environment_variable_does_not_exist, { key:, path: }) unless ENV.key? key ENV.fetch(key, nil) end end