Class: Embulk::Formatter::HtmlTable

Inherits:
FormatterPlugin
  • Object
show all
Defined in:
lib/embulk/formatter/html_table.rb

Constant Summary collapse

NEWLINES =
{
  'CRLF' => "\r\n",
  'LF' => "\n",
  'CR' => "\r",
  'NUL' => "\0"
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.join_texts(inits,last, opt = {}) ⇒ Object



13
14
15
16
17
# File 'lib/embulk/formatter/html_table.rb', line 13

def self.join_texts((*inits,last), opt = {})
  delim = opt[:delimiter] || ', '
  last_delim = opt[:last_delimiter] || ' or '
  [inits.join(delim),last].join(last_delim)
end

.transaction(config, schema) {|task| ... } ⇒ Object

Yields:

  • (task)


19
20
21
22
23
24
25
26
27
28
# File 'lib/embulk/formatter/html_table.rb', line 19

def self.transaction(config, schema, &control)
  task = {
    'encoding' => config.param('encoding', :string, default: 'UTF-8'),
    'newline' => config.param('newline', :string, default: 'LF'),
    'to_br' => config.param('to_br', :bool, default: true),
    'timestamp_format' => config.param('timestamp_format', :string, default: '%Y-%m-%d %H-%M-%S'),
  }

  yield(task)
end

Instance Method Details

#add(page) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/embulk/formatter/html_table.rb', line 43

def add(page)
  page.each do |record|
    if @current_file == nil
      @current_file = file_output.next_file
      @current_file_size = 0
    elsif @current_file_size > 32*1024
      @current_file.write '</table>'.encode(@encoding)
      @current_file = file_output.next_file
      @current_file_size = 0
    end

    if @header_print
      @current_file.write "<table>#{@newline}".encode(@encoding)
      header  = "<tr>#{page.schema.map(&:name).map { |name| "<th>#{name}</th>"}.join('')}</tr>#{@newline}"
      @current_file.write header.encode(@encoding)
      @header_print = false
    end

    row = "<tr>#{record.map { |col| format(col) }.join('')}</tr>#{@newline}"
    @current_file.write row.encode(@encoding)
  end
end

#closeObject



40
41
# File 'lib/embulk/formatter/html_table.rb', line 40

def close
end

#finishObject



74
75
76
77
# File 'lib/embulk/formatter/html_table.rb', line 74

def finish
  @current_file.write '</table>'.encode(@encoding)
  file_output.finish
end

#format(column) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/embulk/formatter/html_table.rb', line 66

def format(column)
  if @to_br
    column = column.strftime(@timestamp_format) if column.is_a? Time
    column = column.to_s.split(@newline).join('<BR>') if @to_br
  end
  "<td>#{column}</td>"
end

#initObject



30
31
32
33
34
35
36
37
38
# File 'lib/embulk/formatter/html_table.rb', line 30

def init
  @header_print = true
  @encoding = task['encoding'].upcase
  @newline = NEWLINES[task['newline'].upcase]
  @to_br = task['to_br']
  @timestamp_format = task['timestamp_format']
  @current_file = nil
  @current_file_size = 0
end