Class: Deb822::Emitter

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

Overview

Low-level generator for deb822 document

Instance Method Summary collapse

Constructor Details

#initialize(output) ⇒ Emitter

Returns a new instance of Emitter.



6
7
8
9
10
11
# File 'lib/deb822/emitter.rb', line 6

def initialize(output)
  @output = output

  @at_beginning = true
  @in_paragraph = false
end

Instance Method Details

#emit_continuation_line(line) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/deb822/emitter.rb', line 51

def emit_continuation_line(line)
  if /\A(?:\.|\Z)/.match?(line)
    @output << ' .' << line
  else
    @output << ' ' << line
  end
  @output << "\n" unless line.end_with?("\n")
  self
end

#emit_field(name, value) ⇒ 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
# File 'lib/deb822/emitter.rb', line 13

def emit_field(name, value)
  name = Deb822.FieldName(name)

  if value.respond_to?(:each_line)
    enum = value.each_line
  else
    enum = value.to_s.each_line
  end

  if !@at_beginning && !@in_paragraph
    @output << "\n"
  end
  @at_beginning = false
  @in_paragraph = true

  @output << name.to_s << ': '

  begin
    line = enum.next
    @output << line
    @output << "\n" unless line.end_with?("\n")
  rescue StopIteration
    # nop
  else
    loop do
      emit_continuation_line(enum.next)
    end
  end

  self
end

#emit_fields(pairs) ⇒ Object



45
46
47
48
49
# File 'lib/deb822/emitter.rb', line 45

def emit_fields(pairs)
  pairs.each_pair do |name, value|
    emit_field(name, value)
  end
end

#start_paragraphObject Also known as: end_paragraph



61
62
63
64
# File 'lib/deb822/emitter.rb', line 61

def start_paragraph
  @in_paragraph = false
  self
end