Class: Peml::Loader
- Inherits:
-
Object
- Object
- Peml::Loader
- Defined in:
- lib/peml/loader.rb
Constant Summary collapse
- WHITESPACE_PATTERN =
~ Constants .….….….….….….….….….….….….….….……
"\u0000\u0009\u000A\u000B\u000C\u000D\u0020\u00A0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u200B\u2028\u2029\u202F\u205F\u3000\uFEFF"
- SLUG_BLACKLIST =
"#{WHITESPACE_PATTERN}\u005B\u005C\u005D\u007B\u007D\u003A"
- START_KEY =
/^([^#{Regexp.escape(SLUG_BLACKLIST)}]+)[ \t\r]*:(((\S)\4{2,})|[ \t\r]*)(.*(?:\n|\r|$))/
- COMMENT_LINE =
/^\s*(#.*(?:\n|\r|$))/
- COMMAND_KEY =
/^:[ \t\r]*(endskip|ignore|skip|end)(.*(?:\n|\r|$))/i
- ARRAY_ELEMENT =
/^\s*\*[ \t\r]*(.*(?:\n|\r|$))/
- SCOPE_PATTERN =
/^(\[|\{)[ \t\r]*([\+\.]*)[ \t\r]*([^#{Regexp.escape(SLUG_BLACKLIST)}]*)[ \t\r]*(?:\]|\}).*?(\n|\r|$)/
Instance Method Summary collapse
-
#flush_buffer! ⇒ Object
————————————————————-.
-
#flush_buffer_into(key, options = {}) ⇒ Object
————————————————————-.
-
#format_value(value, type) ⇒ Object
————————————————————- type can be either :replace or :append.
-
#increment_array_element(key) ⇒ Object
————————————————————-.
-
#initialize(options = {}) ⇒ Loader
constructor
————————————————————-.
-
#load(stream, options = {}) ⇒ Object
————————————————————-.
-
#parse_array_element(value) ⇒ Object
————————————————————-.
-
#parse_command_key(command) ⇒ Object
————————————————————-.
-
#parse_scope(scope_type, flags, scope_key) ⇒ Object
————————————————————-.
-
#parse_start_key(key, quote, rest_of_line) ⇒ Object
————————————————————-.
-
#parse_text(text) ⇒ Object
————————————————————-.
Constructor Details
#initialize(options = {}) ⇒ Loader
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/peml/loader.rb', line 21 def initialize( = {}) @data = @scope = {} @stack = [] @stack_scope = nil @buffer_scope = @buffer_key = nil @buffer_string = '' @quote_string = '' @is_skipping = false @is_quoted = false @done_parsing = false @depth = 0 @default_options = { comments: false }.merge() end |
Instance Method Details
#flush_buffer! ⇒ Object
280 281 282 283 284 285 286 |
# File 'lib/peml/loader.rb', line 280 def flush_buffer! result = @buffer_string.dup # puts " flushed content = #{result.inspect}" @buffer_string = '' @buffer_key = nil return result end |
#flush_buffer_into(key, options = {}) ⇒ Object
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/peml/loader.rb', line 290 def flush_buffer_into(key, = {}) existing_buffer_key = @buffer_key value = self.flush_buffer! if [:replace] if @is_quoted @buffer_string = value else value = self.format_value(value, :replace).sub(/^\s*/, '') @buffer_string = value.match(/\s*\Z/)[0] end @buffer_key = existing_buffer_key else value = self.format_value(value, :append) end if !@is_quoted value = value.sub(/\s*\Z/, '') end # puts " flushed content = #{value.inspect}" if key.class == Array key[key.length - 1] = '' if [:replace] key[key.length - 1] += value else key_bits = key.split('.') @buffer_scope = @scope key_bits[0...-1].each do |bit| @buffer_scope[bit] = {} if @buffer_scope[bit].class == String # reset @buffer_scope = @buffer_scope[bit] ||= {} end @buffer_scope[key_bits.last] = '' if [:replace] @buffer_scope[key_bits.last] += value end end |
#format_value(value, type) ⇒ Object
type can be either :replace or :append. If it’s :replace, then the string is assumed to be the first line of a value, and no escaping takes place. If we’re appending to a multi-line string, escape special punctuation by prepending the line with a backslash. (:, [, {, *, ) surrounding the first token of any line.
336 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/peml/loader.rb', line 336 def format_value(value, type) # backslash-escaped leading characters have been removed in favor of # quoted values. # # if type == :append # value.gsub!(/^(\s*)\\/, '\1') # end # puts " after formatting = #{value.inspect}" value end |
#increment_array_element(key) ⇒ Object
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/peml/loader.rb', line 255 def increment_array_element(key) # Special handling for arrays. If this is the start of the array, remember # which key was encountered first. If this is a duplicate encounter of # that key, start a new object. if @stack_scope && @stack_scope[:array] # If we're within a simple array, ignore @stack_scope[:array_type] ||= :complex return if @stack_scope[:array_type] == :simple # array_first_key may be either another key, or nil if @stack_scope[:array_first_key] == nil || @stack_scope[:array_first_key] == key @stack_scope[:array] << (@scope = {}) end if (@stack_scope[:flags].match(/\+/)) @scope[:type] = key # key = 'content' else @stack_scope[:array_first_key] ||= key end end end |
#load(stream, options = {}) ⇒ Object
43 44 45 46 47 48 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 |
# File 'lib/peml/loader.rb', line 43 def load(stream, = {}) @options = @default_options.merge() stream.each_line do |line| return @data if @done_parsing if @is_quoted if line.match(/^#{@quote_string}(?:\n|\r|$)/) # end quote self.parse_command_key('end') @is_quoted = false @quote_string = '' else self.parse_text(line) end elsif match = line.match(COMMENT_LINE) # just skip it! elsif match = line.match(COMMAND_KEY) self.parse_command_key(match[1].downcase) elsif @skipping # should we just ignore this text, instead of parsing it? self.parse_text(line) elsif (match = line.match(START_KEY)) && (!@stack_scope || @stack_scope[:array_type] != :simple) self.parse_start_key(match[1], match[3], match[5] || '') elsif (match = line.match(ARRAY_ELEMENT)) && @stack_scope && @stack_scope[:array] && (@stack_scope[:array_type] != :complex ) && !@stack_scope[:flags].match(/\+/) self.parse_array_element(match[1]) elsif match = line.match(SCOPE_PATTERN) self.parse_scope(match[1], match[2], match[3]) else # just plain text self.parse_text(line) end end # Treat all keys as multi-line self.parse_command_key('end') self.flush_buffer! return @data end |
#parse_array_element(value) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/peml/loader.rb', line 119 def parse_array_element(value) # Treat all keys as multi-line self.parse_command_key('end') self.flush_buffer! @stack_scope[:array_type] ||= :simple @stack_scope[:array] << '' @buffer_key = @stack_scope[:array] @buffer_string = value self.flush_buffer_into(@stack_scope[:array], replace: true) end |
#parse_command_key(command) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/peml/loader.rb', line 135 def parse_command_key(command) if @is_skipping && !%w(endskip ignore).include?(command) return self.flush_buffer! end case command when "end" self.flush_buffer_into(@buffer_key, replace: false) if @buffer_key @buffer_key = nil return when "ignore" # If this occurs in the middle of a multi-line value, save what # has been accumulated so far self.flush_buffer_into(@buffer_key, replace: false) if @buffer_key return @done_parsing = true when "skip" # If this occurs in the middle of a multi-line value, save what # has been accumulated so far self.flush_buffer_into(@buffer_key, replace: false) if @buffer_key @is_skipping = true when "endskip" @is_skipping = false end self.flush_buffer! end |
#parse_scope(scope_type, flags, scope_key) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/peml/loader.rb', line 167 def parse_scope(scope_type, flags, scope_key) # Treat all keys as multi-line self.parse_command_key('end') self.flush_buffer! if scope_key == '' last_stack_item = @stack.pop @scope = (last_stack_item ? last_stack_item[:scope] : @data) || @data @stack_scope = @stack.last elsif %w([ {).include?(scope_type) nesting = false key_scope = @data if flags.match(/^\./) self.increment_array_element(scope_key) nesting = true key_scope = @scope if @stack_scope else @scope = @data @stack = [] end # Within freeforms, the `type` of nested objects and arrays is taken # verbatim from the `keyScope`. if @stack_scope && @stack_scope[:flags].match(/\+/) parsed_scope_key = scope_key # Outside of freeforms, dot-notation interpreted as nested data. else key_bits = scope_key.split('.') key_bits[0...-1].each do |bit| key_scope = key_scope[bit] ||= {} end parsed_scope_key = key_bits.last end # Content of nested scopes within a freeform should be stored under "value." if (@stack_scope && @stack_scope[:flags].match(/\+/) && flags.match(/\./)) if scope_type == '[' parsed_scope_key = 'value' elsif scope_type == '{' @scope = @scope[:value] = {} end end stack_scope_item = { array: nil, array_type: nil, array_first_key: nil, flags: flags, scope: @scope } if scope_type == '[' stack_scope_item[:array] = key_scope[parsed_scope_key] = [] stack_scope_item[:array_type] = :freeform if flags.match(/\+/) if nesting @stack << stack_scope_item else @stack = [stack_scope_item] end @stack_scope = @stack.last elsif scope_type == '{' if nesting @stack << stack_scope_item else @scope = key_scope[parsed_scope_key] = key_scope[parsed_scope_key].is_a?(Hash) ? key_scope[parsed_scope_key] : {} @stack = [stack_scope_item] end @stack_scope = @stack.last end end end |
#parse_start_key(key, quote, rest_of_line) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/peml/loader.rb', line 96 def parse_start_key(key, quote, rest_of_line) # Treat all keys as multi-line self.parse_command_key('end') self.flush_buffer! self.increment_array_element(key) key = 'value' if (@stack_scope && @stack_scope[:flags].match(/\+/)) @buffer_key = key @buffer_string = rest_of_line self.flush_buffer_into(key, replace: true) if !quote.nil? @is_quoted = true @quote_string = quote end end |
#parse_text(text) ⇒ Object
245 246 247 248 249 250 251 |
# File 'lib/peml/loader.rb', line 245 def parse_text(text) if @stack_scope && @stack_scope[:flags].match(/\+/) && text.match(/[^\n\r\s]/) @stack_scope[:array] << { "type" => "text", "value" => text.gsub(/(^\s*)|(\s*$)/, '') } else @buffer_string += text end end |