Module: Syck::BaseEmitter

Defined in:
lib/syck/baseemitter.rb

Instance Method Summary collapse

Instance Method Details

#binary_base64(value) ⇒ Object

Emit binary data


26
27
28
29
# File 'lib/syck/baseemitter.rb', line 26

def binary_base64( value )
  self << "!binary "
  self.node_text( [value].pack("m"), '|' )
end

#double(value) ⇒ Object

Emit double-quoted string


86
87
88
# File 'lib/syck/baseemitter.rb', line 86

def double( value )
  "\"#{Syck.escape( value )}\""
end

#fold(value) ⇒ Object

Folding paragraphs within a column


131
132
133
134
135
# File 'lib/syck/baseemitter.rb', line 131

def fold( value )
  value.gsub( /(^[ \t]+.*$)|(\S.{0,#{options(:BestWidth) - 1}})(?:[ \t]+|(\n+(?=[ \t]|\Z))|$)/ ) do
    $1 || $2 + ( $3 || "\n" )
  end
end

#indent(mod = nil) ⇒ Object

Write a current indent


110
111
112
113
114
115
116
117
118
119
# File 'lib/syck/baseemitter.rb', line 110

def indent( mod = nil )
  #p [ self.id, level, mod, :INDENT ]
  if level <= 0
    mod ||= 0
  else
    mod ||= options(:Indent)
    mod += ( level - 1 ) * options(:Indent)
  end
  return " " * mod
end

#indent!Object

Add indent to the buffer


124
125
126
# File 'lib/syck/baseemitter.rb', line 124

def indent!
  self << indent
end

#indent_text(text, mod, first_line = true) ⇒ Object

Write a text block with the current indent


100
101
102
103
104
105
# File 'lib/syck/baseemitter.rb', line 100

def indent_text( text, mod, first_line = true )
  return "" if text.to_s.empty?
  spacing = indent( mod )
  text = text.gsub( /\A([^\n])/, "#{ spacing }\\1" ) if first_line
  return text.gsub( /\n^([^\n])/, "\n#{spacing}\\1" )
end

#map(type, &e) ⇒ Object

Quick mapping


140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/syck/baseemitter.rb', line 140

def map( type, &e )
  val = Mapping.new
  e.call( val )
  self << "#{type} " if type.length.nonzero?

  #
  # Empty hashes
  #
  if val.length.zero?
    self << "{}"
    @seq_map = false
  else
    # FIXME
    # if @buffer.length == 1 and options(:UseHeader) == false and type.length.zero?
    #     @headless = 1
    # end

    defkey = @options.delete( :DefaultKey )
    if defkey
      seq_map_shortcut
      self << "= : "
      defkey.to_yaml( :Emitter => self )
    end

    #
    # Emit the key and value
    #
    val.each { |v|
      seq_map_shortcut
      if v[0].is_complex_yaml?
        self << "? "
      end
      v[0].to_yaml( :Emitter => self )
      if v[0].is_complex_yaml?
        self << "\n"
        indent!
      end
      self << ": "
      v[1].to_yaml( :Emitter => self )
    }
  end
end

#node_text(value, block = nil) ⇒ Object

Emit plain, normal flowing text


34
35
36
37
38
39
40
41
42
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
# File 'lib/syck/baseemitter.rb', line 34

def node_text( value, block = nil )
  @seq_map = false
  valx = value.dup
  unless block
    block =
      if options(:UseBlock)
        '|'
      elsif not options(:UseFold) and valx =~ /\n[ \t]/ and not valx =~ /#{ESCAPE_CHAR}/
        '|'
      else
        '>'
      end
    indt = $&.to_i if block =~ /\d+/
    if valx =~ /(\A\n*[ \t#]|^---\s+)/
      indt = options(:Indent) unless indt.to_i > 0
      block += indt.to_s
    end

    block +=
      if valx =~ /\n\Z\n/
        "+"
      elsif valx =~ /\Z\n/
        ""
      else
        "-"
      end
  end
  block += "\n"
  if block[0] == ?"
    esc_skip = ( "\t\n" unless valx =~ /^[ \t]/ ) || ""
    valx = fold( Syck.escape( valx, esc_skip ) + "\"" ).chomp
    self << '"' + indent_text( valx, indt, false )
  else
    if block[0] == ?>
      valx = fold( valx )
    end    #p [block, indt]

    self << block + indent_text( valx, indt )
  end
end

#options(opt = nil) ⇒ Object


11
12
13
14
15
16
17
# File 'lib/syck/baseemitter.rb', line 11

def options( opt = nil )
  if opt
    @options[opt] || DEFAULTS[opt]
  else
    @options
  end
end

#options=(opt) ⇒ Object


19
20
21
# File 'lib/syck/baseemitter.rb', line 19

def options=( opt )
  @options = opt
end

#seq(type, &e) ⇒ Object

Quick sequence


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
# File 'lib/syck/baseemitter.rb', line 197

def seq( type, &e )
  @seq_map = false
  val = Sequence.new
  e.call( val )
  self << "#{type} " if type.length.nonzero?

  #
  # Empty arrays
  #
  if val.length.zero?
    self << "[]"
  else
    # FIXME
    # if @buffer.length == 1 and options(:UseHeader) == false and type.length.zero?
    #     @headless = 1
    # end

    #
    # Emit the key and value
    #
    val.each { |v|
      self << "\n"
      indent!
      self << "- "
      @seq_map = true if v.class == Hash
      v.to_yaml( :Emitter => self )
    }
  end
end

#seq_map_shortcutObject


183
184
185
186
187
188
189
190
191
192
# File 'lib/syck/baseemitter.rb', line 183

def seq_map_shortcut  # FIXME: seq_map needs to work with the new anchoring system
  # if @seq_map
  #     @anchor_extras[@buffer.length - 1] = "\n" + indent
  #     @seq_map = false
  # else

  self << "\n"
  indent!  # end

end

#simple(value) ⇒ Object

Emit a simple, unqouted string


78
79
80
81
# File 'lib/syck/baseemitter.rb', line 78

def simple( value )
  @seq_map = false
  self << value.to_s
end

#single(value) ⇒ Object

Emit single-quoted string


93
94
95
# File 'lib/syck/baseemitter.rb', line 93

def single( value )
  "'#{value}'"
end