Class: String

Inherits:
Object show all
Extended by:
Icss::Meta::StringSchema
Defined in:
lib/icss/type/simple_types.rb,
lib/icss/serialization/zaml.rb

Direct Known Subclasses

Binary, Icss::St::Url, ZAML::Comment

Instance Method Summary collapse

Methods included from Icss::Meta::StringSchema

fullname, receive

Methods included from Icss::Meta::PrimitiveSchema

#doc, #doc=, #to_schema

Instance Method Details

#classify_for_zamlObject



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/icss/serialization/zaml.rb', line 313

def classify_for_zaml
  unless RUBY_VERSION >= "1.9"
    return :escaped if (self =~ ZAML::HI_BIT_CHARS_RE)
  end
  case
  when self == ''
    :bare
  when (self =~ ZAML::EXTENDED_CHARS_RE)
    :escaped
  when (
      (self =~ ZAML::SIMPLE_STRING_RE) or
      (self =~ /\A\n* /) or
      (self =~ /[ \r]\s*\z/) or
      (self =~ /^[>|][-+\d]*\s/i)
      )
    :escaped
  when self =~ /\n/
    :complex
  when (
      (self[-1..-1] =~ /\s/) or
      (self =~ /[\s:]$/) or
      (self =~ /[,\[\]\{\}\r\t]|:\s|\s#/) or
      (self =~ /\A([-:?!#&*'"]|<<|%.+:.)/)
      )
    :escaped
  else
    :simple
  end
end

#escaped_for_zamlObject



299
300
301
302
303
# File 'lib/icss/serialization/zaml.rb', line 299

def escaped_for_zaml
  gsub( /\x5C/, "\\\\\\" ).  # Demi-kludge for Maglev/rubinius; the regexp should be /\\/ but parsetree chokes on that.
    gsub( /"/, "\\\"" ).
    gsub( /([\x00-\x1F])/ ){|x| ZAML::ZAML_ESCAPES[ x.unpack("C")[0] ] }
end

#to_zaml(z = ZAML.new) ⇒ Object



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/icss/serialization/zaml.rb', line 343

def to_zaml(z=ZAML.new)
  z.first_time_only(self) {
    case cl = classify_for_zaml
    when :bare         then z.emit('""')
    when :escaped      then z.emit("\"#{escaped_for_zaml}\"")
    when :simple       then z.emit(self)
    when :complex
      lines = split("\n",-1)
      self =~ /(\s+)\z/
      if    $1.nil?    then z.emit('|-') ; lastline = ""
      elsif $1 == "\n" then z.emit('|')  ; lastline = "\n" ; lines.pop
      else                  z.emit('|+') ; lastline = ""   ; lines.pop ; end
      z.nested{ lines.each{|line| z.nl; z.emit(line) } }
      z.emit(lastline)
      z.nl
    else raise("Misclassified string: #{cl}!")
    end
  }
end