Class: DTC::Utils::Text::LineWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/dtc/utils/text/line_writer.rb

Overview

Helper class for writting lines of text, with some indentation processing.

Blocks of text can be indented and/or captured.

Output is written to an array in ‘lines` by `push_raw`. Other methods use `split_lines` and/or `indent_lines` to preprocess input.

Get the result by

Direct Known Subclasses

HTML::Writer

Instance Method Summary collapse

Instance Method Details

#begin_captureObject



20
21
22
23
24
25
26
27
# File 'lib/dtc/utils/text/line_writer.rb', line 20

def begin_capture
  (@lines_stack ||= []) << @lines
  @lines = []
  if block_given?
    yield
    end_capture
  end
end

#current_indentObject



46
47
48
# File 'lib/dtc/utils/text/line_writer.rb', line 46

def current_indent
  @indents && @indents.last
end

#end_captureObject



28
29
30
31
32
# File 'lib/dtc/utils/text/line_writer.rb', line 28

def end_capture
  result = @lines
  @lines = @lines_stack.pop
  result
end

#linesObject



16
# File 'lib/dtc/utils/text/line_writer.rb', line 16

def lines ; @lines || [] end

#pop_indentObject



43
44
45
# File 'lib/dtc/utils/text/line_writer.rb', line 43

def pop_indent
  @indents.pop
end

#push(*lines) ⇒ Object Also known as: <<



49
50
51
52
53
54
55
# File 'lib/dtc/utils/text/line_writer.rb', line 49

def push *lines
  if (indent = current_indent)
    push_raw(indent_lines(split_lines(*lines), indent))
  else
    push_raw *lines
  end
end

#push_indent(*indent, &blk) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/dtc/utils/text/line_writer.rb', line 36

def push_indent *indent, &blk
  (@indents ||= []) << indent
  if block_given?
    yield
    pop_indent
  end
end

#push_raw(*raw_lines) ⇒ Object



33
34
35
# File 'lib/dtc/utils/text/line_writer.rb', line 33

def push_raw *raw_lines
  @lines = lines + raw_lines.flatten
end

#to_s(sep = "\n") ⇒ Object



17
18
19
# File 'lib/dtc/utils/text/line_writer.rb', line 17

def to_s sep = "\n"
  lines.join(sep)
end