PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/IronPython_Main/External.LCA_RESTRICTED/Languages/Ruby/redist-libs/ruby/1.9.1/syck/baseemitter.rb

#
Ruby | 242 lines | 161 code | 21 blank | 60 comment | 20 complexity | b4ca8cede890dde02a556ab9a09579ab MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  1. #
  2. # BaseEmitter
  3. #
  4. require 'syck/constants'
  5. require 'syck/encoding'
  6. require 'syck/error'
  7. module Syck
  8. module BaseEmitter
  9. def options( opt = nil )
  10. if opt
  11. @options[opt] || DEFAULTS[opt]
  12. else
  13. @options
  14. end
  15. end
  16. def options=( opt )
  17. @options = opt
  18. end
  19. #
  20. # Emit binary data
  21. #
  22. def binary_base64( value )
  23. self << "!binary "
  24. self.node_text( [value].pack("m"), '|' )
  25. end
  26. #
  27. # Emit plain, normal flowing text
  28. #
  29. def node_text( value, block = nil )
  30. @seq_map = false
  31. valx = value.dup
  32. unless block
  33. block =
  34. if options(:UseBlock)
  35. '|'
  36. elsif not options(:UseFold) and valx =~ /\n[ \t]/ and not valx =~ /#{ESCAPE_CHAR}/
  37. '|'
  38. else
  39. '>'
  40. end
  41. indt = $&.to_i if block =~ /\d+/
  42. if valx =~ /(\A\n*[ \t#]|^---\s+)/
  43. indt = options(:Indent) unless indt.to_i > 0
  44. block += indt.to_s
  45. end
  46. block +=
  47. if valx =~ /\n\Z\n/
  48. "+"
  49. elsif valx =~ /\Z\n/
  50. ""
  51. else
  52. "-"
  53. end
  54. end
  55. block += "\n"
  56. if block[0] == ?"
  57. esc_skip = ( "\t\n" unless valx =~ /^[ \t]/ ) || ""
  58. valx = fold( Syck.escape( valx, esc_skip ) + "\"" ).chomp
  59. self << '"' + indent_text( valx, indt, false )
  60. else
  61. if block[0] == ?>
  62. valx = fold( valx )
  63. end
  64. #p [block, indt]
  65. self << block + indent_text( valx, indt )
  66. end
  67. end
  68. #
  69. # Emit a simple, unqouted string
  70. #
  71. def simple( value )
  72. @seq_map = false
  73. self << value.to_s
  74. end
  75. #
  76. # Emit double-quoted string
  77. #
  78. def double( value )
  79. "\"#{Syck.escape( value )}\""
  80. end
  81. #
  82. # Emit single-quoted string
  83. #
  84. def single( value )
  85. "'#{value}'"
  86. end
  87. #
  88. # Write a text block with the current indent
  89. #
  90. def indent_text( text, mod, first_line = true )
  91. return "" if text.to_s.empty?
  92. spacing = indent( mod )
  93. text = text.gsub( /\A([^\n])/, "#{ spacing }\\1" ) if first_line
  94. return text.gsub( /\n^([^\n])/, "\n#{spacing}\\1" )
  95. end
  96. #
  97. # Write a current indent
  98. #
  99. def indent( mod = nil )
  100. #p [ self.id, level, mod, :INDENT ]
  101. if level <= 0
  102. mod ||= 0
  103. else
  104. mod ||= options(:Indent)
  105. mod += ( level - 1 ) * options(:Indent)
  106. end
  107. return " " * mod
  108. end
  109. #
  110. # Add indent to the buffer
  111. #
  112. def indent!
  113. self << indent
  114. end
  115. #
  116. # Folding paragraphs within a column
  117. #
  118. def fold( value )
  119. value.gsub( /(^[ \t]+.*$)|(\S.{0,#{options(:BestWidth) - 1}})(?:[ \t]+|(\n+(?=[ \t]|\Z))|$)/ ) do
  120. $1 || $2 + ( $3 || "\n" )
  121. end
  122. end
  123. #
  124. # Quick mapping
  125. #
  126. def map( type, &e )
  127. val = Mapping.new
  128. e.call( val )
  129. self << "#{type} " if type.length.nonzero?
  130. #
  131. # Empty hashes
  132. #
  133. if val.length.zero?
  134. self << "{}"
  135. @seq_map = false
  136. else
  137. # FIXME
  138. # if @buffer.length == 1 and options(:UseHeader) == false and type.length.zero?
  139. # @headless = 1
  140. # end
  141. defkey = @options.delete( :DefaultKey )
  142. if defkey
  143. seq_map_shortcut
  144. self << "= : "
  145. defkey.to_yaml( :Emitter => self )
  146. end
  147. #
  148. # Emit the key and value
  149. #
  150. val.each { |v|
  151. seq_map_shortcut
  152. if v[0].is_complex_yaml?
  153. self << "? "
  154. end
  155. v[0].to_yaml( :Emitter => self )
  156. if v[0].is_complex_yaml?
  157. self << "\n"
  158. indent!
  159. end
  160. self << ": "
  161. v[1].to_yaml( :Emitter => self )
  162. }
  163. end
  164. end
  165. def seq_map_shortcut
  166. # FIXME: seq_map needs to work with the new anchoring system
  167. # if @seq_map
  168. # @anchor_extras[@buffer.length - 1] = "\n" + indent
  169. # @seq_map = false
  170. # else
  171. self << "\n"
  172. indent!
  173. # end
  174. end
  175. #
  176. # Quick sequence
  177. #
  178. def seq( type, &e )
  179. @seq_map = false
  180. val = Sequence.new
  181. e.call( val )
  182. self << "#{type} " if type.length.nonzero?
  183. #
  184. # Empty arrays
  185. #
  186. if val.length.zero?
  187. self << "[]"
  188. else
  189. # FIXME
  190. # if @buffer.length == 1 and options(:UseHeader) == false and type.length.zero?
  191. # @headless = 1
  192. # end
  193. #
  194. # Emit the key and value
  195. #
  196. val.each { |v|
  197. self << "\n"
  198. indent!
  199. self << "- "
  200. @seq_map = true if v.class == Hash
  201. v.to_yaml( :Emitter => self )
  202. }
  203. end
  204. end
  205. end
  206. #
  207. # Emitter helper classes
  208. #
  209. class Mapping < Array
  210. def add( k, v )
  211. push [k, v]
  212. end
  213. end
  214. class Sequence < Array
  215. def add( v )
  216. push v
  217. end
  218. end
  219. end