Module: Middleman::Util::Data

Extended by:
Memoist
Includes:
Contracts
Defined in:
middleman-core/lib/middleman-core/util/data.rb

Constant Summary

Constants included from Contracts

Contracts::ImmutableSortedSetOf, Contracts::PATH_MATCHER

Class Method Summary collapse

Methods included from Contracts

#Contract

Class Method Details

.build_regex(frontmatter_delims) ⇒ Object


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'middleman-core/lib/middleman-core/util/data.rb', line 92

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(source_file, frontmatter_delims, known_type = nil) ⇒ Object


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
# File 'middleman-core/lib/middleman-core/util/data.rb', line 50

def parse(source_file, frontmatter_delims, known_type = nil)
  full_path = source_file[:full_path]

  return [{}, nil] if ::Middleman::Util.binary?(full_path) || source_file[:types].include?(:binary)

  # Avoid weird race condition when a file is renamed
  begin
    content = source_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]
    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]
    ]
  else
    [
      {},
      content
    ]
  end
end

.parse_json(content, full_path) ⇒ Object


132
133
134
135
136
137
138
139
140
141
142
143
# File 'middleman-core/lib/middleman-core/util/data.rb', line 132

def parse_json(content, full_path)
  c = begin
    ::Middleman::Util.instrument 'parse.json' do
      ::JSON.parse(content)
    end
      rescue StandardError => e
        warn "JSON Exception parsing #{full_path}: #{e.message}"
        {}
  end

  c ? symbolize_recursive(c) : {}
end

.parse_yaml(content, full_path) ⇒ Object


114
115
116
117
118
119
120
121
122
123
124
125
# File 'middleman-core/lib/middleman-core/util/data.rb', line 114

def parse_yaml(content, full_path)
  c = begin
    ::Middleman::Util.instrument 'parse.yaml' do
      ::YAML.load(content)
    end
      rescue StandardError, ::Psych::SyntaxError => e
        warn "YAML Exception parsing #{full_path}: #{e.message}"
        {}
  end

  c ? symbolize_recursive(c) : {}
end

.StringHash

Parse JSON frontmatter out of a string

Parameters:

Returns:

  • (Hash)

113
# File 'middleman-core/lib/middleman-core/util/data.rb', line 113

Contract String, Pathname => Hash

.symbolize_recursive(value) ⇒ Object


146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'middleman-core/lib/middleman-core/util/data.rb', line 146

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