Class: HikiDoc::LineInput

Inherits:
Object
  • Object
show all
Defined in:
lib/hikidoc.rb

Instance Method Summary collapse

Constructor Details

#initialize(f) ⇒ LineInput

Returns a new instance of LineInput.



765
766
767
768
769
770
# File 'lib/hikidoc.rb', line 765

def initialize(f)
  @input = f
  @buf = []
  @lineno = 0
  @eof_p = false
end

Instance Method Details

#eachObject



844
845
846
847
848
# File 'lib/hikidoc.rb', line 844

def each
  while line = gets()
    yield line
  end
end

#eof?Boolean

Returns:

  • (Boolean)


776
777
778
# File 'lib/hikidoc.rb', line 776

def eof?
  @eof_p
end

#getblock(term_re) ⇒ Object



900
901
902
903
904
905
906
# File 'lib/hikidoc.rb', line 900

def getblock(term_re)
  buf = []
  until_terminator(term_re) do |line|
    buf.push line
  end
  buf
end

#getlines_until(re) ⇒ Object Also known as: break



882
883
884
885
886
887
888
# File 'lib/hikidoc.rb', line 882

def getlines_until(re)
  buf = []
  until_match(re) do |line|
    buf.push line
  end
  buf
end

#getlines_while(re) ⇒ Object Also known as: span



861
862
863
864
865
866
867
# File 'lib/hikidoc.rb', line 861

def getlines_while(re)
  buf = []
  while_match(re) do |line|
    buf.push line
  end
  buf
end

#getsObject



784
785
786
787
788
789
790
791
792
793
794
795
# File 'lib/hikidoc.rb', line 784

def gets
  unless @buf.empty?
    @lineno += 1
    return @buf.pop
  end
  return nil if @eof_p   # to avoid ARGF blocking.
  line = @input.gets
  line = line.sub(/\r\n/, "\n") if line
  @eof_p = line.nil?
  @lineno += 1
  line
end

#gets_if(re) ⇒ Object



826
827
828
829
830
831
832
833
# File 'lib/hikidoc.rb', line 826

def gets_if(re)
  line = gets()
  if not line or not (re =~ line)
    ungets line
    return nil
  end
  line
end

#gets_unless(re) ⇒ Object



835
836
837
838
839
840
841
842
# File 'lib/hikidoc.rb', line 835

def gets_unless(re)
  line = gets()
  if not line or re =~ line
    ungets line
    return nil
  end
  line
end

#inspectObject



772
773
774
# File 'lib/hikidoc.rb', line 772

def inspect
  "\#<#{self.class} file=#{@input.inspect} line=#{lineno()}>"
end

#linenoObject



780
781
782
# File 'lib/hikidoc.rb', line 780

def lineno
  @lineno
end

#next?Boolean

Returns:

  • (Boolean)


810
811
812
# File 'lib/hikidoc.rb', line 810

def next?
  peek() ? true : false
end

#peekObject



804
805
806
807
808
# File 'lib/hikidoc.rb', line 804

def peek
  line = gets()
  ungets line if line
  line
end

#skip_blank_linesObject



814
815
816
817
818
819
820
821
822
823
824
# File 'lib/hikidoc.rb', line 814

def skip_blank_lines
  n = 0
  while line = gets()
    unless line.strip.empty?
      ungets line
      return n
    end
    n += 1
  end
  n
end

#ungets(line) ⇒ Object



797
798
799
800
801
802
# File 'lib/hikidoc.rb', line 797

def ungets(line)
  return unless line
  @lineno -= 1
  @buf.push line
  line
end

#until_match(re) ⇒ Object



871
872
873
874
875
876
877
878
879
880
# File 'lib/hikidoc.rb', line 871

def until_match(re)
  while line = gets()
    if re =~ line
      ungets line
      return
    end
    yield line
  end
  nil
end

#until_terminator(re) ⇒ Object



892
893
894
895
896
897
898
# File 'lib/hikidoc.rb', line 892

def until_terminator(re)
  while line = gets()
    return if re =~ line   # discard terminal line
    yield line
  end
  nil
end

#while_match(re) ⇒ Object



850
851
852
853
854
855
856
857
858
859
# File 'lib/hikidoc.rb', line 850

def while_match(re)
  while line = gets()
    unless re =~ line
      ungets line
      return
    end
    yield line
  end
  nil
end