Sha256: 4bffc69ea8930eedcfc938c17a75637eedd88ee232ecc12827a248409125f6a5

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

# coding: utf-8
class Tefil::LineSplitter < Tefil::TextFilterBase

  def initialize(separators: , indent_mode: :no, except_words: [], options: {})
    options[:smart_filename] = true
    @minimum = options[:minimum]
    @separators = separators
    @except_words = except_words
    @indent_mode = indent_mode
    super(options)
  end

  def process_stream(in_io, out_io)
    results = []
 
    # prepare substitute info to get back to original for except rule.
    sub_except_words = Marshal.load(Marshal.dump(@except_words))
    @separators.each do |str|
      sub_except_words.map do |word|
        word.gsub!(str, str + "\n")
      end
    end

    in_io.read.split("\n").each do |line|
      #if @indent_mode == :indent
      #  /^( *)/ =~ line
      #  head_spaces = $1
      #end
      @separators.each do |str|
        line.gsub!(str, str + "\n")
      end
      @except_words.each_with_index do |word, index|
        line.gsub!(sub_except_words[index], word)
      end
      # 行の頭と末尾の空白 strip
      if @indent_mode == :strip
        line.gsub!(/\n  */, "\n")
        line.strip!
        line.gsub!(/  */, " ")
      end
      #if @indent_mode == :indent
      #  results << head_spaces + line
      #end
      results << line
    end
    out_io.puts results.join("\n")
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tefil-0.1.5 lib/tefil/linesplitter.rb