Class: Tefil::LineSplitter

Inherits:
TextFilterBase show all
Defined in:
lib/tefil/linesplitter.rb

Instance Method Summary collapse

Methods inherited from TextFilterBase

#filter

Constructor Details

#initialize(separators:, indent_mode: :no, except_words: [], options: {}) ⇒ LineSplitter

Returns a new instance of LineSplitter.



4
5
6
7
8
9
10
11
# File 'lib/tefil/linesplitter.rb', line 4

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

Instance Method Details

#process_stream(in_io, out_io) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/tefil/linesplitter.rb', line 13

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