require 'cgi' require 'tmpdir' require 'zip/zip' module Ruport class Formatter::ODS < Formatter BLANK_ODS = File.join(Ruport::Util::BASEDIR, 'example', 'data', 'blank.ods') renders :ods, :for => [ Controller::Row, Controller::Table] def prepare_table output << %{ } end def build_table_header if @options.show_table_headers table_row{ build_cells(data.column_names) } end end def build_table_body data.each { |r| build_row(r) } end def build_row(row=data) table_row{ build_cells(row.to_a) } end def table_row output << %{ \n} yield output << %{ \n} end def build_cells(values) values.each do |value| value = CGI.escapeHTML(value.to_s) output << %{ \n} output << %{ #{value}\n} output << %{ \n} end end def finalize_table output << %{ } @tempfile = Tempfile.new('output.ods') File.open(BLANK_ODS){|bo| @tempfile.print(bo.read(1024)) until bo.eof? } @tempfile.close zip = Zip::ZipFile.open(@tempfile.path) zip.get_output_stream('content.xml') do |cxml| cxml.write(output) end zip.close options.io = if options.tempfile @tempfile else File.read(@tempfile.path) end end end end