晋太元中,武陵人捕鱼为业。缘溪行,忘路之远近。忽逢桃花林,夹岸数百步,中无杂树,芳草鲜美,落英缤纷。渔人甚异之,复前行,欲穷其林。   林尽水源,便得一山,山有小口,仿佛若有光。便舍船,从口入。初极狭,才通人。复行数十步,豁然开朗。土地平旷,屋舍俨然,有良田、美池、桑竹之属。阡陌交通,鸡犬相闻。其中往来种作,男女衣着,悉如外人。黄发垂髫,并怡然自乐。   见渔人,乃大惊,问所从来。具答之。便要还家,设酒杀鸡作食。村中闻有此人,咸来问讯。自云先世避秦时乱,率妻子邑人来此绝境,不复出焉,遂与外人间隔。问今是何世,乃不知有汉,无论魏晋。此人一一为具言所闻,皆叹惋。余人各复延至其家,皆出酒食。停数日,辞去。此中人语云:“不足为外人道也。”(间隔 一作:隔绝)   既出,得其船,便扶向路,处处志之。及郡下,诣太守,说如此。太守即遣人随其往,寻向所志,遂迷,不复得路。   南阳刘子骥,高尚士也,闻之,欣然规往。未果,寻病终。后遂无问津者。 sh-3ll

HOME


sh-3ll 1.0
DIR:/opt/alt/ruby19/lib64/ruby/1.9.1/syck/
Upload File :
Current File : //opt/alt/ruby19/lib64/ruby/1.9.1/syck/baseemitter.rb
#
# BaseEmitter
#

require 'syck/constants'
require 'syck/encoding'
require 'syck/error'

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

    def options=( opt )
      @options = opt
    end

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

    #
    # Emit plain, normal flowing text
    #
    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

    #
    # Emit a simple, unqouted string
    #
    def simple( value )
      @seq_map = false
      self << value.to_s
    end

    #
    # Emit double-quoted string
    #
    def double( value )
      "\"#{Syck.escape( value )}\""
    end

    #
    # Emit single-quoted string
    #
    def single( value )
      "'#{value}'"
    end

    #
    # Write a text block with the current indent
    #
    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

    #
    # Write a current indent
    #
    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

    #
    # Add indent to the buffer
    #
    def indent!
      self << indent
    end

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

    #
    # Quick mapping
    #
    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

    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

    #
    # Quick sequence
    #
    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
  end

  #
  # Emitter helper classes
  #
  class Mapping < Array
    def add( k, v )
      push [k, v]
    end
  end

  class Sequence < Array
    def add( v )
      push v
    end
  end
end