Class: HikiDoc::LineInput
- Inherits:
-
Object
- Object
- HikiDoc::LineInput
- Defined in:
- lib/misc/hikidoc.rb
Instance Method Summary collapse
- #each ⇒ Object
- #eof? ⇒ Boolean
- #getblock(term_re) ⇒ Object
- #getlines_until(re) ⇒ Object (also: #break)
- #getlines_while(re) ⇒ Object (also: #span)
- #gets ⇒ Object
- #gets_if(re) ⇒ Object
- #gets_unless(re) ⇒ Object
-
#initialize(f) ⇒ LineInput
constructor
A new instance of LineInput.
- #inspect ⇒ Object
- #lineno ⇒ Object
- #next? ⇒ Boolean
- #peek ⇒ Object
- #skip_blank_lines ⇒ Object
- #ungets(line) ⇒ Object
- #until_match(re) ⇒ Object
- #until_terminator(re) ⇒ Object
- #while_match(re) ⇒ Object
Constructor Details
#initialize(f) ⇒ LineInput
Returns a new instance of LineInput.
759 760 761 762 763 764 |
# File 'lib/misc/hikidoc.rb', line 759 def initialize(f) @input = f @buf = [] @lineno = 0 @eof_p = false end |
Instance Method Details
#each ⇒ Object
838 839 840 841 842 |
# File 'lib/misc/hikidoc.rb', line 838 def each while line = gets() yield line end end |
#eof? ⇒ Boolean
770 771 772 |
# File 'lib/misc/hikidoc.rb', line 770 def eof? @eof_p end |
#getblock(term_re) ⇒ Object
894 895 896 897 898 899 900 |
# File 'lib/misc/hikidoc.rb', line 894 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
876 877 878 879 880 881 882 |
# File 'lib/misc/hikidoc.rb', line 876 def getlines_until(re) buf = [] until_match(re) do |line| buf.push line end buf end |
#getlines_while(re) ⇒ Object Also known as: span
855 856 857 858 859 860 861 |
# File 'lib/misc/hikidoc.rb', line 855 def getlines_while(re) buf = [] while_match(re) do |line| buf.push line end buf end |
#gets ⇒ Object
778 779 780 781 782 783 784 785 786 787 788 789 |
# File 'lib/misc/hikidoc.rb', line 778 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
820 821 822 823 824 825 826 827 |
# File 'lib/misc/hikidoc.rb', line 820 def gets_if(re) line = gets() if not line or not (re =~ line) ungets line return nil end line end |
#gets_unless(re) ⇒ Object
829 830 831 832 833 834 835 836 |
# File 'lib/misc/hikidoc.rb', line 829 def gets_unless(re) line = gets() if not line or re =~ line ungets line return nil end line end |
#inspect ⇒ Object
766 767 768 |
# File 'lib/misc/hikidoc.rb', line 766 def inspect "\#<#{self.class} file=#{@input.inspect} line=#{lineno()}>" end |
#lineno ⇒ Object
774 775 776 |
# File 'lib/misc/hikidoc.rb', line 774 def lineno @lineno end |
#next? ⇒ Boolean
804 805 806 |
# File 'lib/misc/hikidoc.rb', line 804 def next? peek() ? true : false end |
#peek ⇒ Object
798 799 800 801 802 |
# File 'lib/misc/hikidoc.rb', line 798 def peek line = gets() ungets line if line line end |
#skip_blank_lines ⇒ Object
808 809 810 811 812 813 814 815 816 817 818 |
# File 'lib/misc/hikidoc.rb', line 808 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
791 792 793 794 795 796 |
# File 'lib/misc/hikidoc.rb', line 791 def ungets(line) return unless line @lineno -= 1 @buf.push line line end |
#until_match(re) ⇒ Object
865 866 867 868 869 870 871 872 873 874 |
# File 'lib/misc/hikidoc.rb', line 865 def until_match(re) while line = gets() if re =~ line ungets line return end yield line end nil end |
#until_terminator(re) ⇒ Object
886 887 888 889 890 891 892 |
# File 'lib/misc/hikidoc.rb', line 886 def until_terminator(re) while line = gets() return if re =~ line # discard terminal line yield line end nil end |
#while_match(re) ⇒ Object
844 845 846 847 848 849 850 851 852 853 |
# File 'lib/misc/hikidoc.rb', line 844 def while_match(re) while line = gets() unless re =~ line ungets line return end yield line end nil end |