Module: Middleman::Util::Data
Constant Summary
Constants included from Contracts
Class Method Summary collapse
- .build_regex(frontmatter_delims) ⇒ Object
- .parse(file, frontmatter_delims, known_type = nil) ⇒ Object
- .parse_json(content, full_path) ⇒ Object
- .parse_toml(content, full_path) ⇒ Object
- .parse_yaml(content, full_path) ⇒ Object
-
.String ⇒ Hash
Parse JSON frontmatter out of a string.
- .symbolize_recursive(value) ⇒ Object
Methods included from Contracts
Class Method Details
.build_regex(frontmatter_delims) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/middleman-core/util/data.rb', line 97 def build_regex(frontmatter_delims) start_delims, stop_delims = frontmatter_delims .values .flatten(1) .transpose .map(&::Regexp.method(:union)) / \A(?:[^\r\n]*coding:[^\r\n]*\r?\n)? (?<start>#{start_delims})[ ]*\r?\n (?<frontmatter>.*?)[ ]*\r?\n? ^(?<stop>#{stop_delims})[ ]*\r?\n? \r?\n? (?<additional_content>.*) /mx end |
.parse(file, frontmatter_delims, known_type = nil) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/middleman-core/util/data.rb', line 49 def parse(file, frontmatter_delims, known_type=nil) full_path = file[:full_path] return [{}, nil] if ::Middleman::Util.binary?(full_path) || file[:types].include?(:binary) # Avoid weird race condition when a file is renamed begin content = file.read rescue EOFError, IOError, ::Errno::ENOENT return [{}, nil] end match = build_regex(frontmatter_delims).match(content) || {} unless match[:frontmatter] case known_type when :yaml return [parse_yaml(content, full_path), nil] when :json return [parse_json(content, full_path), nil] when :toml return [parse_toml(content, full_path), nil] end end case [match[:start], match[:stop]] when *frontmatter_delims[:yaml] [ parse_yaml(match[:frontmatter], full_path), match[:additional_content] ] when *frontmatter_delims[:json] [ parse_json("{#{match[:frontmatter]}}", full_path), match[:additional_content] ] when *frontmatter_delims[:toml] [ parse_toml(match[:frontmatter], full_path), match[:additional_content] ] else [ {}, content ] end end |
.parse_json(content, full_path) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/middleman-core/util/data.rb', line 165 def parse_json(content, full_path) c = begin ::Middleman::Util.instrument 'parse.json' do ::JSON.parse(content) end rescue StandardError => error warn "JSON Exception parsing #{full_path}: #{error.}" {} end c ? symbolize_recursive(c) : {} end |
.parse_toml(content, full_path) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/middleman-core/util/data.rb', line 145 def parse_toml(content, full_path) c = begin ::Middleman::Util.instrument 'parse.toml' do ::TOML.load(content) end rescue StandardError # TOML parser swallows useful error, so we can't warn about it. # https://github.com/jm/toml/issues/47 warn "TOML Exception parsing #{full_path}" {} end c ? symbolize_recursive(c) : {} end |
.parse_yaml(content, full_path) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/middleman-core/util/data.rb', line 119 def parse_yaml(content, full_path) permitted_classes = [Date, Time, DateTime, Symbol, Regexp] c = begin ::Middleman::Util.instrument 'parse.yaml' do allowed_parameters = ::YAML.method(:safe_load).parameters if allowed_parameters.include? [:key, :permitted_classes] ::YAML.safe_load(content, permitted_classes: permitted_classes, aliases: true) elsif allowed_parameters.include? [:key, :whitelist_classes] ::YAML.safe_load(content, whitelist_classes: permitted_classes, aliases: true) else ::YAML.safe_load(content, permitted_classes, [], true) end end rescue StandardError, ::Psych::SyntaxError => error warn "YAML Exception parsing #{full_path}: #{error.}" {} end c ? symbolize_recursive(c) : {} end |
.String ⇒ Hash
Parse JSON frontmatter out of a string
118 |
# File 'lib/middleman-core/util/data.rb', line 118 Contract String, Pathname => Hash |
.symbolize_recursive(value) ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/middleman-core/util/data.rb', line 179 def symbolize_recursive(value) case value when Hash value.map do |k, v| key = k.is_a?(String) ? k.to_sym : k [key, symbolize_recursive(v)] end.to_h when Array value.map { |v| symbolize_recursive(v) } else value end end |